public function testFloats() { $this->setLocale(LC_ALL, 'de_DE', 'de_DE.UTF-8', 'deu', 'german'); $template = new ezcTemplate(); $file = "float.ezt"; // The number 3.14 should not be translated to 3,14. The array size should be one, not two. file_put_contents($this->templatePath . "/" . $file, "{array_count(array(3.14))}"); $this->assertEquals(1, $template->process($file), "Number 3.14 is internally translated to 3,14 when the de_DE locale is used."); }
/** * Writes the given $schema to $dir using $template. * * Iterates through all tables in $schema, sends each of them to a {@link * ezcTemplate} with $template and writes the result to $dir with the file * name returned by the template. * * @param ezcDbSchema $schema * @param string $template * @param mixed $dir */ public function write(ezcDbSchema $schema, $template, $dir) { $tplConf = ezcTemplateConfiguration::getInstance(); $tplConf->templatePath = $this->properties['options']->templatePath; $tplConf->compilePath = $this->properties['options']->templateCompilePath; $tpl = new ezcTemplate(); $tpl->send->classPrefix = $this->properties['options']->classPrefix; foreach ($schema->getSchema() as $tableName => $tableSchema) { $tpl->send->schema = $tableSchema; $tpl->send->tableName = $tableName; $content = $tpl->process($template); $fileName = $dir . '/' . $tpl->receive->fileName; if (!$this->properties['options']->overwrite && file_exists($fileName)) { throw new ezcPersistentObjectSchemaOverwriteException($fileName); } file_put_contents($fileName, $content); } }
$new = $dir . "/" . $file; if (is_file($new)) { if (!$onlyWithExtension || substr($file, -$extensionLength - 1) == ".{$onlyWithExtension}") { $total[] = $new; } } elseif (is_dir($new)) { readDirRecursively($new, $total, $onlyWithExtension); } } } } $directories = array(); $regressionDir = dirname(__FILE__) . "/regression_tests"; readDirRecursively($regressionDir, $directories, "in"); foreach ($directories as $directory) { $template = new ezcTemplate(); $dir = dirname($directory); $base = basename($directory); $template->configuration = new ezcTemplateConfiguration($dir, "/tmp/template_crap"); $template->configuration->addExtension("TestBlocks"); $template->configuration->addExtension("LinksCustomBlock"); $template->configuration->addExtension("cblockTemplateExtension"); $template->configuration->addExtension("Sha1CustomBlock"); if (preg_match("#^(\\w+)@(\\w+)\\..*\$#", $base, $match)) { $contextClass = "ezcTemplate" . ucfirst(strtolower($match[2])) . "Context"; $template->configuration->context = new $contextClass(); } else { $template->configuration->context = new ezcTemplateNoContext(); } $send = substr($directory, 0, -3) . ".send"; if (file_exists($send)) {
/** * Finds the compiled file based on the stream path and template options. * * Returns the compiled code object which can be used for execution or queried for more info. * * @param string $location The stream path of the requested template file. * @param ezcTemplateOutputContext $context The current output context handler. * @param ezcTemplateManager $template The template which contains the current settings. * @return ezcTemplateCompiledCode */ public static function findCompiled($location, ezcTemplateOutputContext $context, ezcTemplate $template) { $options = 'ezcTemplate::options(' . false . '-' . false . ')'; $identifier = md5('ezcTemplateCompiled(' . $location . ')'); $name = basename($location, '.ezt'); $path = $template->configuration->compilePath . DIRECTORY_SEPARATOR . $template->configuration->compiledTemplatesPath . DIRECTORY_SEPARATOR . $context->identifier() . '-' . $template->generateOptionHash() . '/' . $name . '-' . $identifier . ".php"; return new ezcTemplateCompiledCode($identifier, $path, $context, $template); }
<?php require_once 'tutorial_autoload.php'; $t = new ezcTemplate(); $t->send->a = 2; $t->send->b = 3; // Process it. $t->process("tutorial_variable_send_receive.ezt"); // Retrieve the $answer variable from the template. echo "Answer: " . $t->receive->answer . "\n"; // Show the output. echo $t->output;
function getBody($example, $form) { $c = ezcTemplateConfiguration::getInstance(); $c->templatePath = dirname(__FILE__) . "/dir"; $c->compilePath = "/tmp"; $t = new ezcTemplate(); $t->send->form = $form; $t->process($example . '.ezt'); return $t->output; }
<?php // Autoload. require_once 'tutorial_autoload.php'; $config = ezcTemplateConfiguration::getInstance(); $config->templatePath = "/usr/share/templates"; $config->compilePath = "/tmp/compiled_templates"; $config->context = new ezcTemplateXhtmlContext(); // Is already the default, though. $t = new ezcTemplate(); $t->process("hello_world.ezt");
protected function renderViewTemplate($controller) { $debug = ezcDebug::getInstance(); $tplConfig = new ezcTemplateConfiguration(SITE_ROOT . "/app", SITE_ROOT . "/tmp"); $tplConfig->addExtension("CustomDate"); $tplConfig->addExtension("CustomMath"); $tplConfig->addExtension("MoneyFormat"); $tplConfig->addExtension("UrlCreator"); $tplConfig->disableCache = true; $tplConfig->checkModifiedTemplates = true; $tpl = new ezcTemplate(); $tpl->configuration = $tplConfig; $send = new ezcTemplateVariableCollection($controller->vars); $tpl->send = $send; $receive = $tpl->receive; //var_dump($controller->vars); $result = $tpl->process($controller->templateFile . '.ezt'); //echo $result; $merged = array_merge($send->getVariableArray(), $receive->getVariableArray(), array('result' => $result)); $layoutTpl = new ezcTemplate(); $layoutTpl->configuration = $tplConfig; $layoutTpl->send = new ezcTemplateVariableCollection($merged); $path = 'layouts/' . $controller->layout . '.ezt'; echo $layoutTpl->process($path); //$output = $debug->generateOutput(); //echo $output; }
public function testCacheBlock() { $t = new ezcTemplate(); $t->configuration->addExtension("Fetch"); $r = $t->process("show_users_cache_block.ezt"); $this->assertEquals("\n\n\n\n1 Raymond sunRay\n\n2 Derick Tiger\n\n3 Jan Amos\n", $r); // Update a single user. $db = ezcDbInstance::get(); $db->exec('UPDATE user SET nickname="bla" WHERE id=1'); // Still cached. $r = $t->process("show_users_cache_block.ezt"); $this->assertEquals("\n\n\n\n1 Raymond sunRay\n\n2 Derick Tiger\n\n3 Jan Amos\n", $r); // Send a update signal to the configuration manager. ezcTemplateConfiguration::getInstance()->cacheManager->update("user", 1); $r = $t->process("show_users_cache_block.ezt"); $this->assertEquals("\n\n\n\n1 Raymond bla\n\n2 Derick Tiger\n\n3 Jan Amos\n", $r); }
} $set = new ezcMailVariableSet($msg); $mail = $parser->parseMail($set); $mails[] = $mail[0]; } // Create some debug information (how many miliseconds the parsing took) $end = microtime(true); $debug = sprintf("Execution time (without template): %.0f ms", ($end - $start) * 1000) . "\n"; // Create a template configuration object based on $options $templateConfig = ezcTemplateConfiguration::getInstance(); $templateConfig->templatePath = $options["templatePath"]; $templateConfig->compilePath = $options["compilePath"]; $templateConfig->context = new ezcTemplateXhtmlContext(); $templateConfig->addExtension("PagingLinks"); // Create a template object based on $templateConfig $template = new ezcTemplate(); $template->configuration = $templateConfig; // Assign the template variables with the script variables $template->send->debug = $debug; $template->send->mailbox = $options["mailbox"]; $template->send->mailboxes = $mailboxes; $template->send->selected = $options["currentPage"]; $template->send->pageSize = $options["pageSize"]; $template->send->mailCount = count($messages); $template->send->numberOfPages = $numberOfPages; // Create an array to be passed to the template, which holds the headers the mails // in currentPage and other useful information like mail IDs $mailListing = array(); for ($i = 0; $i < count($mails); $i++) { $mailListing[$i] = array('number' => $messages[$i], 'id' => $mailIDs[$i], 'from' => $mails[$i]->from, 'subject' => $mails[$i]->subject, 'size' => $sizes[$i], 'received' => $mails[$i]->timestamp); }
public function testRunRegression($directory) { $template = new ezcTemplate(); $dir = dirname($directory); $base = basename($directory); $template->configuration = new ezcTemplateConfiguration($dir, $this->getTempDir()); $template->configuration->targetCharset = "Latin1"; $template->configuration->translation = ezcTemplateTranslationConfiguration::getInstance(); $template->configuration->translation->locale = 'en_us'; $backend = new ezcTranslationTsBackend(dirname(__FILE__) . '/translations'); $backend->setOptions(array('format' => '[LOCALE].xml')); $manager = new ezcTranslationManager($backend); $template->configuration->translation->manager = $manager; // $template->configuration->cachePath = $this->getTempDir() . "/cached"; // $template->configuration->cachePath = "/tmp/cache"; /* if ( !is_dir( $template->configuration->cachePath ) ) { mkdir( $template->configuration->cachePath); } */ $template->configuration->addExtension("TestBlocks"); $template->configuration->addExtension("LinksCustomBlock"); $template->configuration->addExtension("cblockTemplateExtension"); $template->configuration->addExtension("Sha1CustomBlock"); if (preg_match("#^(\\w+)@(\\w+)\\..*\$#", $base, $match)) { $contextClass = "ezcTemplate" . ucfirst(strtolower($match[2])) . "Context"; $template->configuration->context = new $contextClass(); } else { $template->configuration->context = new ezcTemplateNoContext(); } $send = substr($directory, 0, -3) . ".send"; if (file_exists($send)) { $template->send = (include $send); } $out = ""; $exceptionText = ""; try { $out = $template->process($base); } catch (Exception $e) { $out = $e->getMessage(); $exceptionText = get_class($e) . "(" . $e->getCode() . "):\n" . $out; // Begin of the error message contains the full path. We replace this with 'mock' so that the // tests work on other systems as well. if (strncmp($out, $directory, strlen($directory)) == 0) { $out = "mock" . substr($out, strlen($directory)); } $exceptionText .= "\n" . $e->getTraceAsString(); } $expected = substr($directory, 0, -3) . ".out"; if (!file_exists($expected)) { $help = "The out file: '{$expected}' could not be found."; if (!self::$skipMissingTests && $this->interactiveMode) { echo "\n", $help, "\n"; self::interact($template, $directory, false, $out, $expected, $help, $exceptionText); return; } else { throw new PHPUnit_Framework_ExpectationFailedException($help); } } $expectedText = file_get_contents($expected); try { $this->assertEquals($expectedText, $out, "In: <{$expected}>\nOut: <{$directory}>"); } catch (PHPUnit_Framework_ExpectationFailedException $e) { $help = "The evaluated template <" . $this->regressionDir . "/current.tmp> differs "; $help .= "from the expected output: <{$expected}>."; if ($this->interactiveMode) { // Touch the file. It will be run first, next time. if (isset($_ENV['EZC_TEST_TEMPLATE_SORT']) && $_ENV['EZC_TEST_TEMPLATE_SORT'] == 'mtime') { touch($directory); } $exceptionText = get_class($e) . "(" . $e->getCode() . "):\n" . $e->getMessage(); $exceptionText .= "\n" . $e->getTraceAsString(); echo "\n", $help, "\n"; self::interact($template, $directory, file_get_contents($expected), $out, $expected, $help, $exceptionText); return; } // Rethrow with new and more detailed message throw new PHPUnit_Framework_ExpectationFailedException($help, $e->getComparisonFailure()); } // check the receive variables. $receive = substr($directory, 0, -3) . ".receive"; if (file_exists($receive)) { $expectedVar = (include $receive); $foundVar = $template->receive; $this->assertEquals($expectedVar, $foundVar, "Received variables does not match"); } }
<?php require '../include/ezc-setup.php'; require '../include/template_function_call.php'; $tpl = new ezcTemplate(); $tpl->configuration->addExtension("TemplateFunctionCall"); $tpl->send->listing = array(1 => 'Item 1', 2 => 'Item 2'); echo $tpl->process('oneonly.ezt');
public function testCompileOnly() { file_put_contents($this->templatePath . "/test.ezt", '{use $myVar}{$myVar}'); $tc = ezcTemplateConfiguration::getInstance(); $tc->executeTemplate = false; self::assertEquals(0, count(glob($tc->compilePath . "/compiled_templates/xhtml-*/*.php"))); $template = new ezcTemplate(); $out = $template->process("test.ezt"); // Should work, because we compile the template only. $template->process("test.ezt"); self::assertEquals(1, count(glob($tc->compilePath . "/compiled_templates/xhtml-*/*.php"))); }
public function testDisableCachingWithCacheBlock() { $t = new ezcTemplate(); $t->send->user = new TestUser("Bernard", "Black"); $out = $t->process("cache_block.tpl"); $this->assertEquals(<<<EOM Before: [Bernard Black] Cache block 0: [Bernard Black] Between cb 0 and 1: [Bernard Black] Cache block 1: [Bernard Black] After: [Bernard Black] EOM , $out); $t->configuration->disableCache = true; $t->send->user = new TestUser("Roger", "Rabbit"); $out = $t->process("cache_block.tpl"); $this->assertEquals(<<<EOM Before: [Roger Rabbit] Cache block 0: [Roger Rabbit] Between cb 0 and 1: [Roger Rabbit] Cache block 1: [Roger Rabbit] After: [Roger Rabbit] EOM , $out); $t->send->user = new TestUser("Bernard", "Black"); $out = $t->process("cache_block.tpl"); $this->assertEquals(<<<EOM Before: [Bernard Black] Cache block 0: [Bernard Black] Between cb 0 and 1: [Bernard Black] Cache block 1: [Bernard Black] After: [Bernard Black] EOM , $out); }
/** * Generate an Agavi route. * * @param ezcTemplate The current template object instance. * @param string The name of the route to generate. * @param array An array of route parameters. * @param array An array of gen options. * * @return string The generated route. * * @author Felix Weis <*****@*****.**> * @since 0.11.0 */ public static function route(ezcTemplate $obj, $name, array $params = array(), $options = array()) { return $obj->getContext()->getRouting()->gen($name, $params, $options); }
<?php require_once 'tutorial_autoload.php'; $t = new ezcTemplate(); // Set the template configuration for printer templates. $c = ezcTemplateConfiguration::getInstance("printer"); $c->templatePath = "printer"; // ./printer directory; $c->context = new ezcTemplateNoContext(); // Use a context that doesn't do anything. $c = ezcTemplateConfiguration::getInstance(); $c->templatePath = "html"; // ./html directory. // And another way to configure your template engine. $pdfConf = new ezcTemplateConfiguration("pdf", ".", new ezcTemplateNoContext()); try { // Uses the default configuration. $t->process("hello_world.ezt"); } catch (Exception $e) { echo $e->getMessage() . "\n\n"; } try { // Uses the printer configuration $t->process("hello_world.ezt", ezcTemplateConfiguration::getInstance("printer")); } catch (Exception $e) { echo $e->getMessage() . "\n\n"; } try { // Uses the PDF configuration. $t->configuration = $pdfConf; $t->process("hello_world.ezt");