/** * Template constructor. * * @param Container $container */ public function __construct(Container $container) { $settings = $container->get('settings'); $compiledPath = $settings['storagePath'] . DIRECTORY_SEPARATOR . 'views'; $resolver = new EngineResolver(); $resolver->register('blade', function () use($compiledPath, &$settings) { $bladeCompiler = new BladeCompiler(new Filesystem(), $compiledPath); // Add the @webroot directive. $bladeCompiler->directive('webroot', function ($expression) use(&$settings) { $segments = explode(',', preg_replace("/[\\(\\)\\\"\\']/", '', $expression)); $path = rtrim($settings['webrootBasePath'], '/') . '/' . ltrim($segments[0], '/'); $path = str_replace("'", "\\'", $path); return "<?= e('{$path}') ?>"; }); // Add the @route directive. $bladeCompiler->directive('route', function ($expression) use(&$settings) { $segments = explode(',', preg_replace("/[\\(\\)\\\"\\']/", '', $expression)); $path = rtrim($settings['routeBasePath'], '/') . '/' . ltrim($segments[0], '/'); $path = str_replace("'", "\\'", $path); return "<?= e('{$path}') ?>"; }); return new CompilerEngine($bladeCompiler); }); $finder = new FileViewFinder(new Filesystem(), [$settings['templatePath']]); $factory = new ViewFactory($resolver, $finder, new Dispatcher()); $this->factory = $factory; $this->container = $container; }
/** * Add extra directives to the blade templating compiler. * * @param BladeCompiler $blade The compiler to extend * * @return void */ public static function registerDirectives(BladeCompiler $blade) { $keywords = ["namespace", "use"]; foreach ($keywords as $keyword) { $blade->directive($keyword, function ($parameter) use($keyword) { $parameter = trim($parameter, "()"); return "<?php {$keyword} {$parameter} ?>"; }); } $assetify = function ($file, $type) { $file = trim($file, "()"); if (in_array(substr($file, 0, 1), ["'", '"'], true)) { $file = trim($file, "'\""); } else { return "{{ {$file} }}"; } if (substr($file, 0, 1) !== "/") { $file = "/{$type}/{$file}"; } if (substr($file, (strlen($type) + 1) * -1) !== ".{$type}") { $file .= ".{$type}"; } return $file; }; $blade->directive("css", function ($parameter) use($assetify) { $file = $assetify($parameter, "css"); return "<link rel='stylesheet' type='text/css' href='{$file}'>"; }); $blade->directive("js", function ($parameter) use($assetify) { $file = $assetify($parameter, "js"); return "<script type='text/javascript' src='{$file}'></script>"; }); }
public function extendTo(BladeCompiler $blade) { $blade->directive('wploop', $this->start()); $blade->directive('wpempty', $this->ifempty()); $blade->directive('endwploop', $this->end()); $blade->directive('wpthe', $this->the()); }
public function register() { $this->app->instance(__CLASS__, $this); $this->app->alias(__CLASS__, $this->containerName); $this->blade->directive('include_layout', function ($expr) { return "<?php echo app('{$this->containerName}')->renderLayout{$expr} ?>"; }); }
/** * Add basic syntax highlighting. * * This just adds markup that is picked up by a javascript highlighting library. * * @param BladeCompiler $compiler */ protected function allowHighlightTag(BladeCompiler $compiler) { $compiler->directive('highlight', function ($expression) { if (is_null($expression)) { $expression = "('php')"; } return "<?php \$__env->startSection{$expression}; ?>"; }); $compiler->directive('endhighlight', function () { return <<<'HTML' <?php $last = $__env->stopSection(); echo '<pre><code class="language-', $last, '">', trim($__env->yieldContent($last)), '</code></pre>'; ?> HTML; }); }
/** * Register. * * @param \Illuminate\View\Compilers\BladeCompiler $compiler * * @return void */ public static function register(BladeCompiler $compiler) { $compiler->directive('money', function ($expression) { if (Str::startsWith($expression, '(')) { $expression = substr($expression, 1, -1); } return "<?php echo money({$expression}); ?>"; }); $compiler->directive('currency', function ($expression) { if (Str::startsWith($expression, '(')) { $expression = substr($expression, 1, -1); } return "<?php echo currency({$expression}); ?>"; }); }
/** * Register the custom Blade directive * * @param \Illuminate\View\Compilers\BladeCompiler $compiler */ private function registerBladeDirective($compiler) { $compiler->directive('notifications', function () { $expression = 'Gloudemans\\Notify\\Notifications\\Notification'; return "<?php echo app('{$expression}')->render(); ?>"; }); }
/** * extend * * @param BladeCompiler $blade * @param string $name * @param callable $closure * * @return void */ public static function extend(BladeCompiler $blade, $name, $closure) { // For 5.0 after if (is_callable(array($blade, 'directive'))) { $blade->directive($name, $closure); return; } // For 4.x before $blade->extend(function ($view, BladeCompiler $compiler) use($name, $closure) { $pattern = $compiler->createMatcher($name); return preg_replace_callback($pattern, function ($matches) use($closure) { if (empty($matches[2])) { return $matches[0]; } return $matches[1] . $closure($matches[2]); }, $view); }); return; }
/** * Register a handler for custom directives. * * @param string $name * @param callable $handler * @return void * @static */ public static function directive($name, $handler) { \Illuminate\View\Compilers\BladeCompiler::directive($name, $handler); }
/** * @shortcode * * @param BladeCompiler $compiler The blade compiler to extend */ private function registerDoShortcode($compiler) { $compiler->directive('shortcode', function ($expression) { return "<?php do_shortcode(with{$expression}); ?>"; }); }
/** * Register Blade syntax for a specific plugin. * * @param string $method * @return void */ protected function registerTag($method) { $this->blade->directive($method, function ($expression) use($method) { return "Plugin::{$method}{$expression}"; }); }
/** * @wpend * * @param BladeCompiler $compiler The blade compiler to extend */ private function registerEndLoop($compiler) { $compiler->directive('endwpquery', function ($expression) { return "<?php endif; wp_reset_postdata(); ?>"; }); }
/** * Register Blade syntax for a specific component. * * @param string $method * @param string $namespace * @return void */ protected function registerTag($method, $namespace = '') { $this->blade->directive('component_' . $method, function ($view) use($method, $namespace) { return '<?php echo ' . $namespace . $method . "{$view}; ?>"; }); }
/** * Register the `@criticalCss($uri)` Blade directive. * * @return void */ public static function registerBladeDirective(BladeCompiler $blade) { $blade->directive('criticalCss', [static::class, 'parseBladeDirective']); }
}, 'template' => function (Container $container) { return new TemplateRenderer($container); }, 'validator' => function (Container $container) { return new ValidationFactory($container['translator']); }, 'translator' => function (Container $container) { return new Translator($container['settings']['locale']); }, 'viewFactory' => function (Container $container) { $settings = $container->get('settings'); $compiledPath = $settings['storagePath'] . DIRECTORY_SEPARATOR . 'views'; $resolver = new EngineResolver(); $resolver->register('blade', function () use($compiledPath, &$settings) { $bladeCompiler = new BladeCompiler(new Filesystem(), $compiledPath); // Add the @webroot directive. $bladeCompiler->directive('webroot', function ($expression) use(&$settings) { $segments = explode(',', preg_replace("/[\\(\\)\\\"\\']/", '', $expression)); $path = rtrim($settings['webrootBasePath'], '/') . '/' . ltrim($segments[0], '/'); $path = str_replace("'", "\\'", $path); return "<?= e('{$path}') ?>"; }); // Add the @route directive. $bladeCompiler->directive('route', function ($expression) use(&$settings) { $segments = explode(',', preg_replace("/[\\(\\)\\\"\\']/", '', $expression)); $path = rtrim($settings['routeBasePath'], '/') . '/' . ltrim($segments[0], '/'); $path = str_replace("'", "\\'", $path); return "<?= e('{$path}') ?>"; }); return new CompilerEngine($bladeCompiler); }); $finder = new FileViewFinder(new Filesystem(), [$settings['templatePath']]); return new ViewFactory($resolver, $finder, new Dispatcher()); }];