public function __construct() { parent::__construct(); $this->specs = array(); $this->tests = array(); $parser = new sfYamlParser(); $m = new Proust\Proust(array("enableCache" => true, "cacheDir" => dirname(__FILE__) . "/spec.cache", "compilerOptions" => array("beautify" => false, "includeDynamicPartials" => true))); $m->clearCache(); $methods = array(); foreach (glob(SPEC_DIR . "*.yml") as $file) { $name = str_replace(".yml", "", basename($file)); $contents = file_get_contents($file); /* hack around sfyaml */ $contents = str_replace("!code", "", $contents); $yaml = $parser->parse($contents); $yaml["name"] = $name; $i = 0; foreach ($yaml["tests"] as &$test) { if (array_key_exists("lambda", $test["data"])) { $code = "return function (\$text = \"\") { " . $test["data"]["lambda"]["php"] . " };"; $test["data"]["lambda"] = eval($code); } $name = preg_replace('/[^a-zA-Z0-9]/', '_', $name); $test["method_name"] = "{$name}" . "_" . $i; array_push($methods, array($test["method_name"], $test["template"])); $this->tests[$name . "_{$i}"] = $test; $i++; } $this->specs[$name] = $yaml; } $classCode = $m->compileClass("Specs", $methods); eval($classCode); $m = new Proust\Proust(array("enableCache" => false)); $this->obj = new Specs($m); }
} else { if (_getopt($opts, "e")) { var_dump($m->render($tpl, $context)); } else { if (_getopt($opts, "t")) { $code .= "Tokens for {$file}:\n" . print_r($m->getTokens($tpl), true) . "\n"; } else { $code .= $m->compile($tpl, null, array("type" => "function", "name" => filenameToFunctionName($file))) . "\n"; } } } } if (_getopt($opts, "c")) { $className = $opts['c']; $method = filenameToFunctionName($files[0]); $code = $m->compileClass($className, $methods); if (_getopt($opts, "e")) { eval($code); $obj = new $className(); print $obj->{$method}($context); die; } } if (!_getopt($opts, 't') && !_getopt($opts, "e")) { $code = "<?php\n\n{$code}\n?>\n"; if (_getopt($opts, 'o') !== null) { file_put_contents($opts['o'], $code); print "Written to " . $opts['o'] . "\n"; } else { print $code; }
<?php /* render and call a template class */ require_once dirname(__FILE__) . "/../Proust.php"; $p = new Proust\Proust(array("templatePath" => dirname(__FILE__) . "/templates/", "compilerOptions" => array("beautify" => true))); $p->partials = array("partial" => "{{#section}}{{bla}}{{/section}}\n"); $tpl = <<<'EOT' {{#foo}}{{bla}}{{/foo}} {{>partial}} EOT; $tpl2 = <<<'EOT' {{#foo}}{{>section1}}{{/foo}} EOT; echo "\n\n\nClass:\n-----\n\n"; $code = $p->compileClass("TestClass", array(array("main", $tpl), array("foobar", $tpl2))); echo $code; echo "\n\n"; eval($code); $test = new TestClass($p); echo "\n\n\nMethod main():\n---------------\n\n"; echo $test->main(array("foo" => array("bla" => "Hello world"), "section" => array("bla" => "Partial hello world"))); echo "\n\n\nMethod foobar():\n----------------\n\n"; echo $test->foobar(array("foo" => array("x" => 1, "y" => 2, "z" => array(1, 2, 3, 4)), "section" => array("bla" => "Partial hello world")));