7

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.

mujuonly
  • 11,370
  • 5
  • 45
  • 75
Baldráni
  • 5,332
  • 7
  • 51
  • 79

4 Answers4

13

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/

Alexandre Beaudet
  • 2,774
  • 2
  • 20
  • 29
  • 1
    Ok I wanted the whole word to be set as capital so the second answer is the good one. Thank you ! – Baldráni Dec 31 '15 at 13:32
  • @Baldráni I'll keep both in the post just in case someone need that, no prob ! :) – Alexandre Beaudet Dec 31 '15 at 13:32
  • 1
    Keep in mind Blade is still just PHP so you should be able to use any PHP or Laravel functions within the `{{ }}` or `{!! !!}` tags. – user1669496 Dec 31 '15 at 14:24
  • this not responds the answer, because the answers says capitalize. capitalizations means only the first letter. so if you use ucfirst function with a variable instead of quoted text youll get a double dot error. – rüff0 Feb 11 '18 at 08:05
11

PHP Native functions can be used here

{{ strtoupper($currency) }} 

{{ ucfirst($interval) }}

Tested OK

mujuonly
  • 11,370
  • 5
  • 45
  • 75
  • Why would you even comment a question answered correctly 4 years later with the same answer :') ? – Baldráni Apr 25 '19 at 07:15
  • 2
    @Baldráni In blade this is not working ( Str::upper($value) ) and you can use PHP core functions. I thought it might be useful to someone - so posted and upvoted – mujuonly Apr 25 '19 at 07:24
1

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

Brad Ahrens
  • 4,864
  • 5
  • 36
  • 47
0

works double quoted:

{{ucfirst(trans("$var"))}}
rüff0
  • 906
  • 1
  • 12
  • 26