2

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

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

1 回答 1

2

BladeCompiler一个compileString方法,它允许您在视图之外使用 Blade 指令。:)

因此,您可以执行以下操作:

$timestamp = '2015-11-10 17:41:53';
$result = Blade::compileString('@appFormatDate($timestamp)');
于 2015-11-12T17:32:57.337 回答