首页 > 解决方案 > Laravel 在 php 块中使用刀片指令

问题描述

我使用 Laravel 5.7,我创建了一个 Blade 指令:

// create blade direct for prices
Blade::directive('price', function ($money) {
    return "<?php echo number_format($money, 2, ',', ' ') .' €'; ?>";
});

我可以在我的刀片模板中使用它,如下所示:

<span>@price($product->price)</span>

但是我怎样才能在 php 方法中使用它呢?我有这个代码:

@include('emails.table', [
     __('order.reference') => $order->reference,
     __('order.total') => @price($order->total),
])

但它不起作用:

调用未定义的函数 price()

我试过没有@但没有成功。我必须为此使用一种 php 助手吗?

标签: laravel

解决方案


您将变量传递给视图并@price在该视图中使用刀片指令。

@include('emails.table', [
     'reference' => $order->reference,
     'total' => $order->total,
])

并在emails.table您执行以下操作:

<div>@trans('order.total'): @price($total)</div>

推荐阅读