/** * this is where all the other functions outputs * make a use . according to isBlade it understands * whether it must deal with the blade or not * * @param array $pathArray * @param $data */ public function display(array $pathArray, $data) { if ($pathArray['isBlade']) { $blade = new \Philo\Blade\Blade($this->finder->viewsDirection, $this->bladeCache); echo $blade->view()->make($pathArray['path'], $data); } else { foreach ($data as $name => $value) { ${$name} = $value; } include $pathArray['path']; } }
/** * Renders a template and returns the result as a string * * cannot contain template as a key * * throws RuntimeException if $templatePath . $template does not exist * * @param $template * @param array $data * * @return mixed * * @throws \InvalidArgumentException * @throws \RuntimeException */ public function fetch($template, array $data = []) { if (isset($data['template'])) { throw new \InvalidArgumentException("Duplicate template key found"); } $data = array_merge($this->attributes, $data); $renderer = new \Philo\Blade\Blade($this->viewPaths, $this->cachePath, $this->events); return $renderer->view()->make($template, $data)->render(); }
<?php /** * Test 1: Built the template 1000 times by each time creating a new template engine instance. * Cache: on */ require 'vendor/autoload.php'; // timer start $start = microtime(true); // display the template for ($i = 0; $i < 1000; $i++) { // setup $views = __DIR__ . '/template'; $cache = __DIR__ . '/temp'; $blade = new \Philo\Blade\Blade($views, $cache); // assign variables $tpl = $blade->view()->make('template', ['entries' => include '../entries.php']); echo $tpl->render(); } // timer stop echo 'end time: ' . (microtime(true) - $start);
<?php /** * Blade service provider * @return \Philo\Blade\Blade */ use ZigiPhp\App; return function () { $blade = new \Philo\Blade\Blade(App::getConfig('blade')['viewPath'], App::getConfig('blade')['cachePath']); $blade->getCompiler()->directive('trans', function ($expression) { return '<?php echo \\ZigiPhp\\Helpers\\I18n::__' . $expression . '; ?>'; }); $blade->getCompiler()->directive('form', function ($expression) { return '<?php ob_start(); ?>'; }); $blade->getCompiler()->directive('endform', function () { return '<?php \\ZigiPhp\\Helpers\\Form::create(ob_get_clean()); ?>'; }); return $blade; };
/** * @param $prView * @param array $prArray * @return mixed */ public final function render($prView, $prArray = array()) { /** If cache folder not exists, create this */ if (!is_dir(path . 'app/cache')) { mkdir(path . 'app/cache', 0775, true); } $blade = new \Philo\Blade\Blade(path . 'app/view', path . 'app/cache'); return $blade->view()->make($prView, $prArray)->render(); }