Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why laravel blade @yield adds a new line (line break)

I'm using blade templating with laravel 4.2

I have a little issue with the @yield function which works with the @section function.

Let's say in my layout template layout.blade.php I have the following statement:

<meta name="description" content="@yield('description')">

and in contact.blade.php which extends layout.blade.php I have this:

@section('description')
    this is the contact page
@stop

The output is this:

<meta name="description" content="this is the contact page
">

The problem is the line break added automatically at the end the section rendering.

Do you have an idea how to avoid this unwanted behaviour?

like image 556
Amaynut Avatar asked Oct 28 '14 17:10

Amaynut


People also ask

Why does Laravel use the blade template engine?

Laravel Blade template engine enables the developer to produce HTML based sleek designs and themes. All views in Laravel are usually built in the blade template. Blade engine is fast in rendering views because it caches the view until they are modified. All the files in resources/views have the extension .

How do you insert a line break in Laravel?

You can try: {!! 'first line <br> second line' !!}

What is the advantage of Laravel blade template?

The main advantage of using the blade template is that we can create the master template, which can be extended by other files.

What are the two primary benefits of Laravel blade?

Two of the primary benefits of using Blade are template inheritance and sections. We can define a blade page as a combination of layout and sections. Since most of the general web applications will have the same layout across the web pages.


1 Answers

You can use {{trim(View::yieldContent('description'))}}

The explanation.

I had the same problem. I had a few modal windows on the page, that had a common layout but different bodies, titles and "id" attributes. So, "id" attribute should be yielded without any spaces around.

The @yield statement compiles to echo $__env->yieldContent call (BladeCompiler.php, compileYield method). $_env here is an instance of \Illuminate\View\Factory. So you can use {{trim(View::yieldContent('description'))}} where View is a facade.

like image 127
Pavel L Avatar answered Oct 12 '22 04:10

Pavel L