/** * Render template. * * @param View $template Template view instance. * @param array|DataCollection $data Data. * @return string * * @throws ConfigurationException */ public function render(View $template, array $data = array()) { if (!empty($this->data)) { // @codeCoverageIgnore $data = array_merge($this->data, $data); // @codeCoverageIgnore } // @codeCoverageIgnore // Path $path = $template->getFullPath(); // Declare all data vars local if possible foreach ($data as $name => $value) { ${$name} = $value; } // Require and stop with OB. ob_start(); require $path; return ob_get_clean(); }
/** * @covers \Arvici\Component\View\View * @covers \Arvici\Component\View\View::template * @covers \Arvici\Component\View\View::body * @covers \Arvici\Component\View\View::bodyPlaceholder * @covers \Arvici\Component\View\View::getPath */ public function testBasicView() { $view = View::template('template'); $this->assertInstanceOf("\\Arvici\\Component\\View\\View", $view); $this->assertEquals(View::PART_TEMPLATE, $view->getType()); $view = View::body('body'); $this->assertInstanceOf("\\Arvici\\Component\\View\\View", $view); $this->assertEquals(View::PART_BODY, $view->getType()); $this->assertEquals('body', $view->getPath()); $view = View::bodyPlaceholder(); $this->assertInstanceOf("\\Arvici\\Component\\View\\View", $view); $this->assertEquals(View::PART_BODY_PLACEHOLDER, $view->getType()); $this->assertEquals(null, $view->getPath()); }
/** * Set the body replacement view. * * @param View $view body view * * @throws RendererException */ public function body(View $view) { if ($view->getType() !== View::PART_BODY) { throw new RendererException("You are replacing the body with a non-body view!"); } if ($this->bodyKey === null) { throw new RendererException("No body is defined in the template!"); } $this->stack[$this->bodyKey] = $view; }
private function clearBuilder() { $this->builder = View::build(); $this->builder->clear(); }
<?php use Arvici\Heart\Config\Configuration; use Arvici\Component\View\View; /** * Template Configuration */ Configuration::define('template', function () { return ['templatePath' => 'Template/Default', 'viewPath' => 'View', 'defaultStack' => [View::template('header'), View::bodyPlaceholder(), View::template('footer')], 'defaultEngine' => 'PhpTemplate', 'stacks' => ['test-sample' => [View::template('testHeader'), View::bodyPlaceholder(), View::body('testContent'), View::template('testFooter')], 'test-basicrender' => [View::template('testHeader'), View::bodyPlaceholder(), View::template('testFooter')]]]; });
/** * Render template. * * @param View $template Template view instance. * @param array|DataCollection $data Data. * * @return string|void * * @throws ConfigurationException * @throws FileNotFoundException * @throws RendererException */ public function render(View $template, array $data = array()) { if (!empty($this->data)) { $data = array_merge($this->data, $data); } // Load file into string $source = file_get_contents($template->getFullPath()); if ($source === false) { throw new FileNotFoundException("View file '" . $template->getFullPath() . "' is not found!"); // @codeCoverageIgnore } return $this->mustache->render($source, $data); }
public function __construct() { parent::__construct(); $this->view = View::build()->defaultStack(); }
/** * Add template part to the stack. * * @param string $path Path to the template, relative to the path you provided in the configuration. * @param array $data Data to pass. * @param string|null $engine Engine, leave null to use default one in config. * * @return Builder */ public function template($path, $data = array(), $engine = null) { $view = new View($path, View::PART_TEMPLATE, $engine); $view->setData($data); $this->render->add($view); return $this; }