27

I'm trying to set an attribute using @yield and @section, but how? I tried to use

<html @yield('mainApp')>

and

@section('mainApp','id="myid"')

but it returns id=&quot;myid&quot; instead of id="myid"

I know that I can manage it with a default id but I don't like this way, and also what if I need to use a custom attribute?

Vixed
  • 3,429
  • 5
  • 37
  • 68
  • Perfect!! work in my project and based on larvel 5.3. but if used Laravel 5.4, inline content passed to a section is automatically escaped then used @section('mainApp', {!! 'id="myid"' !!}) – swaroop suthar Apr 21 '17 at 07:40

17 Answers17

16

Laravel escapes HTML by default. I therefore see that you have two choices.

  1. Expose the value to the view as a variable in your controller.

    view()->share('mainApp', sprintf('id="%s"', 'myid'));

Then output the value unescaped.

<html {!! $mainApp !!}
  1. Only yield the id attribute value, not the entire attribute.

    @section('mainApp') myid @stop

    <html id="@yield('mainApp', '')">

fubar
  • 16,918
  • 4
  • 37
  • 43
  • Thanks for your answer, but as I said, I already know the things you posted. Anyway, I'll vote up for the controller solution. – Vixed Feb 12 '17 at 23:00
  • No worries. I used the controller solution when using Laravel to render pages with embedded Angular apps. And sorry, I didn't realise that you meant default `id` being an empty value. – fubar Feb 12 '17 at 23:23
  • Option #2 FTW! ! – Harry Bosh Dec 24 '19 at 05:09
9

On your controller you do something like:

return view('my_page')->with('myid', 'myid');

and on your view/layout you do something like:

<html {{ $myid or '' }}>
...

You don't need to yield for such a task.

Waiyl Karim
  • 2,810
  • 21
  • 25
8

If you check the Upgrade Guide from the link below you will see few lines below under Blade: https://laravel.com/docs/5.4/upgrade

    @section Escaping

In Laravel 5.4, inline content passed to a section is automatically escaped:

    @section('title', $content)

If you would like to render unescaped content in a section, you must declare the section using the traditional "long form" style:

    @section('title')
        {!! $content !!}
    @stop

So the get the result for for your query you need to as below:

    @section('mainApp', {!! 'id="myid"' !!})
Prashant Barve
  • 4,105
  • 2
  • 33
  • 42
5

Which Laravel version you are used? In the Laravel 5.4, there's an update about this:

Blade

@section Escaping

In Laravel 5.4, inline content passed to a section is automatically escaped:

@section('title', $content)

If you would like to render unescaped content in a section, you must declare the section using the traditional "long form" style:

@section('title') {!! $content !!} @stop

https://laravel.com/docs/5.4/upgrade

Long short story, try to use Laravel 5.4 and then use this code

@section('mainApp')
    id="myid"
@stop
Alief
  • 459
  • 3
  • 12
4

Why don't you use like this:

@section('mainApp') id='myid' @endsection

This will not escaping the character.

Manish
  • 3,443
  • 1
  • 21
  • 24
3

So a better way to do this if i understand you, is using @stack and @push

<html class="no-js" lang="en" @stack('mainApp')>

And what ever page you want to send the id use @push

@push('mainApp')
 id='myid'
@endpush

https://laravel.com/docs/5.2/blade#stacks

can even add variables or what every you like in there and it will go where the @stack is

rchatburn
  • 732
  • 4
  • 13
2

Maybe something like this would help:

{!! $__env->yieldContent('mainApp') !!}

That said, using laravel 5.4, your way works fine for me.. which version are you using?

Nicolas
  • 884
  • 5
  • 13
2

if used Laravel 5.4, inline content passed to a section is automatically escaped.

then remove automatically escaped in section for used

@section('mainApp', {!! 'id="myid"' !!})
swaroop suthar
  • 632
  • 3
  • 19
2

The section default content is escaped using the e() helper. Since Laravel 5.1 you can use Illuminate\Support\HtmlString, this class html content it's not escaped but directly rendered by the Laravel e() helper function, i.e.:

@section('mainApp', new Illuminate\Support\HtmlString('id="myid"'))
dparoli
  • 8,891
  • 1
  • 30
  • 38
1

If you want to achieve <html id="myid"> you can do that this way :

in your html tag add this:

<html @yield('mainApp')>

and in your template :

@section('mainApp', "id=myid") // this will give you <html id="myid">

enter image description here

Amr Aly
  • 3,871
  • 2
  • 16
  • 33
0

I've just tested it in a laravel 5.4 and what is working for me is:

This is in my layout

<html lang="en" @yield('mainApp')> 

And this in my extending view ( removed quotes wrapping myid )

@section('mainApp','id=myid')

The result is in the attached image

enter image description here

Frnak
  • 6,601
  • 5
  • 34
  • 67
0

This is how I do this in Laravel 5.4:

HTML template (template.html):

<!DOCTYPE html>
<html id="{{ $html_id or '' }}" lang="{{ $html_locale or 'en' }}">
    <head>
        ...

        @yield('head')

        ...
    </head>

    <body>
        ...

        @yield('body')

        ...
    </body>
</html>

A base template (template.main)

@component('template.html')
    @section('head')

       ...

    @stop


    @section('body')
        ...

        @yield('contents')

        ...
    @stop
@endcomponent

A page template wich will define the HTML #ID

@extends('template.main')


@slot('html_id')
    app-html-id
@endslot


@section('contents')
  ... 
@stop
Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
0

Define your master layout with @yield

<html lang="{{ config('app.locale') }}" @yield('hello')>

Extend that master

@extends('layouts.app')

Start section hello

@section('hello') 
   id="myId"
@endsection

Output of following is like this here id="myId" is dynamic part.

<html id="myId" lang="en">
Bhaumik Pandhi
  • 2,655
  • 2
  • 21
  • 38
0

I'm not sure how you are assigning the value of myId but if it is just a PHP variable, have you tried passing it to your partials like this?

@yield('mainApp', [
    'id' => $myid
])

@section('mainApp', [
    'id' => $myid
])

You should be able to pass any variables from one template to another and map them to distinct variable names using a associative array of key => value pairs

Spholt
  • 3,724
  • 1
  • 18
  • 29
0

There is a HtmlString class you can use for this kind of things, Laravel uses it for things like csrf_field() and method_field().

You could inline it if you'd like:

@section('mainApp', new \Illuminate\Support\HtmlString('id="myid"'))

However, I would personally create a custom helper for this so I can access it a lot easier, i.e.:

@section('mainApp', html_string('id="myid"'))

The helper should look something like..

function html_string($string) {
    return new \Illuminate\Support\HtmlString($string);
}

If you need help creating a custom helper, check out this answer https://stackoverflow.com/a/28290359/7844646

Community
  • 1
  • 1
Saeed Prez
  • 728
  • 3
  • 8
0

I haven't tried it, but maybe you could do something like this :

<html {!! View::getSections()['mainApp'] !!}>

If I am right, View::getSections() returns all the defined sections.

However, I don't know if it will work because maybe getSections will be called before the section is defined. But it's still worth a shot.

Alex
  • 1,368
  • 10
  • 20
-1

Let me tell you in one line.

Start Section Name should be yield('Section Name')

Heartbeat
  • 142
  • 9