/** * {@inheritdoc} */ protected function load($name) { $template = $this->nameParser->parse($name); if (!file_exists($path = $template->getPath())) { throw new InvalidArgumentException(sprintf('The template "%s" does not exist.', $name)); } return parent::load($path); }
<?php require __DIR__ . '/autoload.php'; use Razr\Engine; use Razr\Loader\FilesystemLoader; // simple array $array = array(); $array['title'] = 'I am the walrus'; $array['artist'] = array('name' => 'The Beatles', 'homepage' => 'http://www.thebeatles.com'); // simple object $object = new stdClass(); $object->title = 'I am the walrus'; $object->artist = array('name' => 'The Beatles', 'homepage' => 'http://www.thebeatles.com'); // article object $article = new Article('My article', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.', 'Me'); // render template $razr = new Engine(new FilesystemLoader(__DIR__)); function hello($str) { echo "Hello " . $str; } // $razr->addFunction('hello', 'hello'); echo $razr->render('template.razr', array('name' => 'World', 'pi' => 3.14159265359, 'number' => -5, 'now' => new DateTime(), 'array' => $array, 'object' => $object, 'article' => $article));
/** * @{inheritdoc} */ public function setEngine(Engine $engine) { $this->engine = $engine; $this->parser = $engine->getParser(); }
public function __construct(LoaderInterface $loader, $cachePath) { parent::__construct($loader, $cachePath); $this->addDirective(new StringDirective()); $this->addDirective(new ImageDirective()); }
/** * {@inheritdoc} */ public function initialize(Engine $engine) { // directives $engine->addDirective(new BlockDirective()); $engine->addDirective(new ControlDirective()); $engine->addDirective(new ExtendDirective()); $engine->addDirective(new IncludeDirective()); $engine->addDirective(new RawDirective()); $engine->addDirective(new SetDirective()); // functions $engine->addFunction('e', array($engine, 'escape')); $engine->addFunction('escape', array($engine, 'escape')); $engine->addFunction('block', array($this, 'block')); $engine->addFunction('constant', array($this, 'getConstant')); $engine->addFunction('json', 'json_encode'); $engine->addFunction('upper', 'strtoupper'); $engine->addFunction('lower', 'strtolower'); $engine->addFunction('format', 'sprintf'); $engine->addFunction('replace', 'strtr'); }