我有一个包含很多数字的表格,我想对所有数字使用数字格式。所以现在我有这个:
<tbody>
@foreach($table['float']['chips_amount'] as $float)
<tr>
<td class="no-border"></td>
<td class="text-right chip-width">{{ number_format($float['chips']['value'], 0, ' ', ' ') }}</td>
<td class="text-right count-width">{{ $float['count'] }}</td>
<td class="text-right">{{ number_format($float['chips']['value'] * $float['count'], 0, ' ', ' ') }}</td>
</tr>
@endforeach
<tr>
<td class="no-border" colspan="3"></td>
<td class="text-right value-width bold-border">{{ number_format($table['float']['amount'], 0, ' ', ' ') }}</td>
</tr>
</tbody>
但我只是重复相同的函数 number_format() ,当有人决定格式不同时可能会出现问题。然后我必须更改表中的所有格式。我对 Nette 框架有一些经验,并且存在一个选项,我可以拥有自定义帮助程序,然后在模板中使用它,即:{{ $anyNumber|myCustomFormat }}
在管道之后我有自己的自定义助手。 Laravel Blade 模板中有类似的东西吗?我在文档中没有找到任何与此相关的内容。
您可以创建自己的刀片帮助文件。
创建您的文件(例如应用程序/Helpers/bladeHelpers.php)并添加代码。例如;
<?php
if (! function_exists('my_custom_number_formt')) {
/**
* Format number
*
* @param $value
* @param $attribute
* @param $data
* @return boolean
*/
function my_custom_number_formt($value)
{
return number_format($value, 0, ' ', ' ');
}
}
然后将此文件添加到您的作曲家.json in the autoload部分(请记住在 psr4 声明中根据您的项目设置命名空间);
{
... rest of file
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"MyApp\\Custom\\": "src/"
},
"files": [
"app/Helpers/bladeHelpers.php"
]
},
... rest of file
}
注意:此时您可能需要清除缓存。
然后在你的刀片文件中使用;
<td class="text-right chip-width">{{ my_custom_number_formt($float['chips']['value']) }}</td>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)