arkert
arkert

Reputation: 92

Laravel - How I Pass Variable Into Blade's Section In One Line?

how I can pass a variable into the blade command "@section" with the one line approach? I have this:

@section('title', 'Hello')

Now I want something like

@section('title', 'Hello {{ $user->name}} foo') // doesn't work
// or
@section('title', 'Hello ' . {{ $user->name}} . ' foo') // doesn't work
// or
@section('title', 'Hello ' . htmlspecialchars($user->name) . ' foo') // this works
// or
@section('title')Hello {{ $user->name }} foo @endsection // this works

The last two just working fine, but I don't want to use plain php in my blade template and also I don't want to use @endsection for title.

How I can do this? Is there even a possibility or should I go with one of the two solutions?

Upvotes: 2

Views: 1930

Answers (4)

Salim Djerbouh
Salim Djerbouh

Reputation: 11044

Don't use blade echo in blade directives, they're already php code

Remove the double curly braces and wrap it all in double quotes

@section('title', "Hello $user->name foo")

This is as short as you can get

Upvotes: 4

Md Masud
Md Masud

Reputation: 2713

@section('title', 'whatever your string')

all you need to do is format a string that support by blade.

Upvotes: 0

Rouhollah Mazarei
Rouhollah Mazarei

Reputation: 4153

You can remove the curly braces like this:

@section('title', 'Hello ' . $user->name . ' foo') // This should work

Upvotes: 0

Dino Numić
Dino Numić

Reputation: 1452

Maybe this is what you're looking for

@section('title')
 Hello, {{ $user->name}}
@endsection

How about this

@section('title', 'Hello ' . auth()->user()->name )

Upvotes: 0

Related Questions