How to capitalize first letter in Laravel Blade

后端 未结 4 824
無奈伤痛
無奈伤痛 2021-02-06 21:28

I\'m using laravel (5.1) blade template engine with the localization feature.

There is a language file messages.php within the /resources/lang/en/

相关标签:
4条回答
  • 2021-02-06 21:50

    Add a blade directive to the app/Providers/AppServiceProvider's boot() function:

    public function boot() {
    
        Blade::directive('lang_u', function ($s) {
            return "<?php echo ucfirst(trans($s)); ?>";
        });
    
    }
    

    This way you can use the following in your blade files:

    @lang_u('messages.welcome')
    

    which outputs: Welcome

     

    You're @lang_u('messages.welcome') :)

    0 讨论(0)
  • 2021-02-06 22:00

    Use PHP's native ucfirst function:

    {{ ucfirst(trans('messages.welcome')) }}
    
    0 讨论(0)
  • 2021-02-06 22:06

    I think that the best option is use CSS text-transform property

    In your CSS file:

    .lowercase {
        text-transform: lowercase;
    }
    .uppercase {
        text-transform: uppercase;
    }
    .capitalize {
        text-transform: capitalize;
    }
    

    Your blade (html) file:

    <p class="lowercase">{{ trans('messages.welcome') }}</p> <!-- This will display welcome -->
    <p class="uppercase">{{ trans('messages.welcome') }}</p> <!-- This will display WELCOME -->
    <p class="capitalize">{{ trans('messages.welcome') }}</p><!-- This will display Welcome -->
    

    Or, the best option for me, use bootstrap

    <p class="text-lowercase">{{ trans('messages.welcome') }}</p><!-- This will display welcome -->
    <p class="text-uppercase">{{ trans('messages.welcome') }}</p><!-- This will display WELCOME -->
    <p class="text-capitalize">{{ trans('messages.welcome') }}</p><!-- This will display Welcome -->
    
    0 讨论(0)
  • 2021-02-06 22:10

    Another way to make capitalize first letter using PHP and blade.

    Controller

    return view('stock.uk-lse', ['name' => 'djan']);
    

    View

    <h1>{{ ucfirst($name) }}</h1>
    
    0 讨论(0)
提交回复
热议问题