Example #1
0
 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);
 }
Example #2
0
 function testRenderSectionNoCache()
 {
     $m = $this->m;
     $m->enableCache = false;
     $res = $m->renderTemplate("section1");
     $this->assertEqual($res, "\n");
     $res = $m->renderTemplate("section1", array("foo" => array("bla" => "bla")));
     $this->assertEqual($res, "bla\n");
     $res = $m->renderTemplate("section1", array("foo" => array(array("bla" => "1 "), array("bla" => "2 "), array("bla" => "3 "), array("bla" => "4 "), array("bla" => "5 "))));
     $this->assertEqual($res, "1 2 3 4 5 \n");
     /* test reloading cached stuff */
     $m = new Proust\Proust(array("templatePath" => dirname(__FILE__) . "/files/"));
     $res = $m->renderTemplate("section1");
     $this->assertEqual($res, "\n");
     $res = $m->renderTemplate("section1", array("foo" => array("bla" => "bla")));
     $this->assertEqual($res, "bla\n");
     $res = $m->renderTemplate("section1", array("foo" => array(array("bla" => "1 "), array("bla" => "2 "), array("bla" => "3 "), array("bla" => "4 "), array("bla" => "5 "))));
     $this->assertEqual($res, "1 2 3 4 5 \n");
 }
Example #3
0
     $compilerOptions["includePartialCode"] = true;
 }
 if (_getopt($opts, "--disable-lambdas")) {
     $compilerOptions["disableLambdas"] = true;
 }
 if (_getopt($opts, "--disable-indentation")) {
     $compilerOptions["disableIndentation"] = true;
 }
 if (_getopt($opts, "--beautify")) {
     $compilerOptions["beautify"] = true;
 }
 if (_getopt($opts, "p")) {
     $options["templatePath"] = _getopt($opts, "p");
 }
 $options["compilerOptions"] = $compilerOptions;
 $m = new Proust\Proust($options);
 $methods = array();
 $code = "";
 foreach ($files as $file) {
     $tpl = file_get_contents($file);
     if (_getopt($opts, "c")) {
         /* store method name and code */
         array_push($methods, array(filenameToFunctionName($file), $tpl));
     } 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";
Example #4
0
<?php

/* simple hello world */
require_once dirname(__FILE__) . "/../Proust.php";
$p = new Proust\Proust();
echo $p->render("Hello {{planet}}\n", array("planet" => "World"));
Example #5
0
<?php

/* example with partials, caching and some compiler options */
require_once dirname(__FILE__) . "/../Proust.php";
$p = new Proust\Proust(array("enableCache" => true, "cacheDir" => dirname(__FILE__) . "/.proust.cache/", "templatePath" => dirname(__FILE__) . "/templates/", "disableObjects" => "true", "compilerOptions" => array("disableLambdas" => true, "includePartialCode" => true)));
$data = array("foo" => array("x" => 1, "y" => 2, "z" => array(1, 2, 3, 4)));
echo $p->renderTemplate("section1", $data);
echo "\nIndentation disabled:\n";
$p->compilerOptions["disableIndentation"] = true;
echo $p->renderTemplate("section1", $data);
echo "\nWith explicit partials:\n";
$p->compilerOptions["disableIndentation"] = false;
$p->partials = array("partial2" => "{{foo.y}}");
echo $p->renderTemplate("section1", $data);
echo "\nShow caching in effect:\n";
$p->partials = array("partial2" => "NEW VERSION: {{foo.y}}");
echo $p->renderTemplate("section1", $data);
echo "\nAfter clearCache:\n";
$p->clearCache();
echo $p->renderTemplate("section1", $data);
Example #6
0
<?php

/* compile some templates */
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}}");
$tpl = <<<'EOT'
{{#foo}}{{bla}}{{/foo}}
{{>partial}}
EOT;
echo "\n\n\nCode:\n-----\n\n";
echo $p->compile($tpl);
echo "\n\n";
$p->compilerOptions = array("includePartialCode" => true);
echo "\n\n\nCode with included partials:\n----------------------------\n\n";
echo $p->compile($tpl);
echo "\n\n";
$p->compilerOptions = array("disableLambdas" => true);
echo "\n\n\nCode with disabled lambdas:\n---------------------------\n\n";
echo $p->compile($tpl);
echo "\n\n";
$p->compilerOptions = array("disableIndentation" => true);
echo "\n\n\nCode with disabled indentation:\n-------------------------------\n\n";
echo $p->compile($tpl);
echo "\n\n";
Example #7
0
<?php

/* canonical mustache template */
require_once dirname(__FILE__) . "/../Proust.php";
$p = new Proust\Proust();
$tpl = <<<'EOD'
Hello {{name}}
You have just won ${{value}}!
{{#in_ca}}
Well, ${{taxed_value}}, after taxes.
{{/in_ca}}

EOD;
class Chris
{
    public $name = "Chris";
    public $value = 10000;
    public $in_ca = true;
    public function taxed_value()
    {
        return $this->value - $this->value * 0.4;
    }
}
echo $p->render($tpl, new Chris());
Example #8
0
<?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")));