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

I am using blade templating with laravel 4.2

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

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 continues with layout.blade.php , I have this:

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

Output:

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

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

Do you have an idea to avoid this unwanted behavior?

+6
source share
3 answers

I'm sure @yield and @section , where it is not intended to be used as a variable, but rather to replace parts of the content according to the needs of each derived page.

To accomplish this, you must either pass a parameter to your view blade from controller , as:

 <meta name="description" content="{{ $page_description }}"> 

or consider replacing all meta tag (s) for this page, i.e.:

layout.blade.php

 <meta name="title" content="This is my page title for all pages"> @yield("additional_meta_tags") 

contact.blade.php (or other pages)

 @section("additional_meta_tags") <meta name="description" content="this is the contact page"> @stop 
+4
source

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

Explanation.

I had the same problem. I had several modal windows on the page that had a common layout, but different bodies, names, and id attributes. Thus, the id attribute should be obtained without any spaces.

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

+9
source

Starting with Laravel 5, the solution I like best is:

 @section('description', 'this is the contact page') 
+5
source

Source: https://habr.com/ru/post/977368/


All Articles