在模板之外使用Blade指令 [英] Using Blade directives outside of templates

查看:68
本文介绍了在模板之外使用Blade指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Laravel 5.1:我在BladeServiceProvider内部定义了一些自定义指令(以下示例).现在,我想在视图模板之外使用它们来格式化字符串(我正在使用PHPExcel在自定义ExportService类中编写EXCEL文件).是否可以重用我的指令?

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 '-';
         }
         ?>";
    });

推荐答案

BladeCompiler具有compileString方法,该方法允许您在视图外部使用Blade指令. :)

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)');

这篇关于在模板之外使用Blade指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆