Source of file View.php
Size: 4,674 Bytes - Last Modified: 2017-11-08T13:54:24+00:00
/home/travis/build/bluzphp/framework/src/View/View.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 | <?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ declare(strict_types=1); namespace Bluz\View; use Bluz\Auth\AbstractRowEntity; use Bluz\Common\Container; use Bluz\Common\Helper; use Bluz\Common\Options; use Bluz\Proxy\Logger; use Bluz\Response\ResponseTrait; /** * View - simple template engine with native PHP syntax * * @package Bluz\View * @author Anton Shevchuk * @author ErgallM * @link https://github.com/bluzphp/framework/wiki/View * * @see \Bluz\View\Helper\ * * @method string ahref(string $text, mixed $href, array $attributes = []) * @method string api(string $module, string $method, $params = []) * @method string attributes(array $attributes = []) * @method string baseUrl(string $file = null) * @method string checkbox($name, $value = null, $checked = false, array $attributes = []) * @method string|bool controller(string $controller = null) * @method string|View dispatch($module, $controller, $params = []) * @method string exception(\Exception $exception) * @method string gravatar($email, $size = 80, $default = 'mm', $rate = 'g') * @method bool hasModule(string $module) * @method string|null headScript(string $src = null, array $attributes = []) * @method string|null headScriptBlock(string $code = null) * @method string|null headStyle(string $href = null, string $media = 'all') * @method string|bool module(string $module = null) * @method string partial($__template, $__params = []) * @method string partialLoop($template, $data = [], $params = []) * @method string radio($name, $value = null, $checked = false, array $attributes = []) * @method string redactor($selector, array $settings = []) * @method string script(string $src, array $attributes = []) * @method string scriptBlock(string $code) * @method string select($name, array $options = [], $selected = null, array $attributes = []) * @method string style(string $href, $media = 'all') * @method string styleBlock(string $code, $media = 'all') * @method string|null url(string $module, string $controller, array $params = [], bool $checkAccess = false) * @method AbstractRowEntity|null user() * @method void widget($module, $widget, $params = []) */ class View implements ViewInterface, \JsonSerializable { use Container\Container; use Container\JsonSerialize; use Container\MagicAccess; use Options; use Helper; use ResponseTrait; /** * @var string base url */ protected $baseUrl; /** * @var string path to template */ protected $path; /** * @var array paths to partial */ protected $partialPath = []; /** * @var string template name */ protected $template; /** * Create view instance, initial default helper path * * @throws \Bluz\Common\Exception\CommonException */ public function __construct() { // initial default helper path $this->addHelperPath(__DIR__ . '/Helper/'); } /** * Render like string * * @return string */ public function __toString() { ob_start(); try { if (!file_exists($this->path . DIRECTORY_SEPARATOR . $this->template) || !is_file($this->path . DIRECTORY_SEPARATOR . $this->template) ) { throw new ViewException("Template `{$this->template}` not found"); } extract($this->container, EXTR_SKIP); include $this->path . DIRECTORY_SEPARATOR . $this->template; } catch (\Exception $e) { // save error to log Logger::exception($e); // clean output ob_clean(); } return ob_get_clean(); } /** * {@inheritdoc} * * @return string */ public function getPath() { return $this->path; } /** * {@inheritdoc} * * @param string $path * * @return void */ public function setPath($path) { $this->path = $path; } /** * {@inheritdoc} * * @return string */ public function getTemplate() { return $this->template; } /** * {@inheritdoc} * * @param string $file * * @return void */ public function setTemplate($file) { $this->template = $file; } /** * Add partial path for use inside partial and partialLoop helpers * * @param string $path * * @return void */ public function addPartialPath($path) { $this->partialPath[] = $path; } } |