在 Laravel 中将刀片的变量大写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34547146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Capitalized a blade's variable in Laravel
提问by Baldráni
I used to use smarty a lot and now moved on to Laravel but I'm missing something that was really useful. The modification in the template of you're variable.
我曾经经常使用 smarty,现在转向 Laravel,但我错过了一些非常有用的东西。你在模板中的修改是可变的。
Let say I have a variable assign as {{$var}}
. Is there a way in Laravel to set it to upper case ? Something like: {{$var|upper}}
假设我有一个变量分配为{{$var}}
。Laravel 中有没有办法将其设置为大写?就像是:{{$var|upper}}
I sadly haven't found any documentation on it.
遗憾的是,我还没有找到任何关于它的文档。
回答by Alexandre Beaudet
Only first character :
只有第一个字符:
You could use UCFirst, a PHP function
你可以使用 UCFirst,一个 PHP 函数
{{ucfirst(trans('text.blabla'))}}
For the doc : http://php.net/manual/en/function.ucfirst.php
对于文档:http: //php.net/manual/en/function.ucfirst.php
Whole word
全字
Str::upper($value)
Also this page might have handy things : http://cheats.jesse-obrien.ca/
这个页面也可能有方便的东西:http: //cheats.jesse-obrien.ca/
回答by mujuonly
PHP Native functions can be used here
这里可以使用 PHP Native 函数
{{ strtoupper($currency) }}
{{ ucfirst($interval) }}
Tested OK
测试正常
回答by AlmostPitt
You can also create a custom Blade directive within the AppServiceProvider.php
您还可以在 AppServiceProvider.php 中创建自定义 Blade 指令
Example:
例子:
public function boot() {
Blade::directive('lang_u', function ($s) {
return "<?php echo ucfirst(trans($s)); ?>";
});
}
Don't forget to import Blade at the top of the AppServiceProvider
不要忘记在 AppServiceProvider 顶部导入 Blade
use Illuminate\Support\Facades\Blade;
Then use it like this within your blade template:
然后在您的刀片模板中像这样使用它:
@lang_u('index.section_h2')
Source: How to capitalize first letter in Laravel Blade
For further information:
了解更多信息:
回答by rüff0
works double quoted:
作品双引号:
{{ucfirst(trans("$var"))}}