laravel - 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 助手吗?
解决方案
您将变量传递给视图并@price
在该视图中使用刀片指令。
@include('emails.table', [
'reference' => $order->reference,
'total' => $order->total,
])
并在emails.table
您执行以下操作:
<div>@trans('order.total'): @price($total)</div>
推荐阅读
- python - 通过关键字重命名熊猫中的多个列
- android - 导入 com.projectname.R 无法解析
- javascript - Gulp 任务以排除特定 js 文件写入 Index.html 文件
- php - 通过相同的键合并两个关联数组
- macos - 启动后如何重新启动docker-machine?
- apache-camel - 什么队列或队列是骆驼路由监听
- internet-explorer - 您将如何使使用 HTC 的仅 IE5 网站在现代网络浏览器中运行?
- python - 循环遍历 Python 数组导致列表索引超出范围错误
- linux-kernel - PCI MMIO 读取数据延迟的原因是什么?
- javascript - chrome非活动标签中的通知声音