示例#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);
 }
示例#2
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);