Baldráni
Baldráni

Reputation: 5640

Capitalized a blade's variable in Laravel

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.

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}}

I sadly haven't found any documentation on it.

Upvotes: 8

Views: 38748

Answers (4)

Brad Ahrens
Brad Ahrens

Reputation: 5168

You can also create a custom Blade directive within the AppServiceProvider.php

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

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:

https://laravel.com/docs/5.8/blade#extending-blade

Upvotes: 1

mujuonly
mujuonly

Reputation: 11861

PHP Native functions can be used here

{{ strtoupper($currency) }} 

{{ ucfirst($interval) }}

Tested OK

Upvotes: 12

r&#252;ff0
r&#252;ff0

Reputation: 943

works double quoted:

{{ucfirst(trans("$var"))}}

Upvotes: 0

Alexandre Beaudet
Alexandre Beaudet

Reputation: 2844

Only first character :

You could use UCFirst, a PHP function

{{ucfirst(trans('text.blabla'))}}

For the doc : 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/

Upvotes: 14

Related Questions