Using Blade directives outside of templates

倖福魔咒の 提交于 2019-12-31 03:04:08

问题


Laravel 5.1: I defined a few custom directives inside a BladeServiceProvider (example below). Now I would like to use them outside of a view template to format strings (I am writing an EXCEL file with PHPExcel in a custom ExportService class). Is it possible to reuse my directives?

 Blade::directive('appFormatDate', function($expression) {
        return "<?php
         if (!is_null($expression)) {
           echo date(\Config::get('custom.dateformat'), strtotime($expression));
         }
         else {
           echo '-';
         }
         ?>";
    });

回答1:


The BladeCompiler has a compileString method, which allows you to use the Blade directives outside the views. :)

So, you can do things like this:

$timestamp = '2015-11-10 17:41:53';
$result = Blade::compileString('@appFormatDate($timestamp)');


来源:https://stackoverflow.com/questions/33667796/using-blade-directives-outside-of-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!