How to include values in the same Section without overwriting the previous one?

Asked

Viewed 222 times

0

I have a view which includes another view . I want these same views to include a javascript in view main call javascripts.

Thus:

#layout.blade.php

<head>@yield('javascripts')</head>
<div id="container">@yield('container')</div>

In my view where I use to register users have the following:

#usuario/cadastra.blade.php
@extends('layout')
@section('container')

<form>
@include('usuarios._form', ['botao' => 'Cadastrar');
</form>

@stop

@section('javascripts')
{{ HTML::script('js/usuarios/cadastra.js') }}
@stop

This is another view, which is used in include, which is usuarios._form.

#users. _form.blade.php

<!-- Meu formulário aqui -->

@section('javascripts')
{{ HTML::script('js/usuarios/_form.js') }}
@stop

I need the two javascript included in @section('javascripts') appear together, but only the last one appears, which is the view usuarios/cadastra.blade.

How can I get the two of you to show up together?

2 answers

1

Simply and directly, all you have to do is stop using the @stop at the end of his statements and move to using @append.

Although I didn’t see anything in the documentation about it, the class BladeCompiler has some method that are preceded by the word "Compile" before, whose functionality is to compile Quile is captured after the @.

I mean, there’s the method compileAppend. So you can do it this way in your views:

@section('javascripts')
{{ HTML::script('js/jquery.js') }}
@append


@section('javascripts')
{{ HTML::script('js/undescore.js') }}
@append

In the end everything is compiled for the @yield('javascripts').

In that case, when doing things like that, I always recommend that you use @append instead of @stop, except in the case of @section('content').

1


At Laravel 5.2 we have the @stack to do this, changing few things:

#layout.blade.php

<head>@stack('javascripts')</head>
<div id="container">@yield('container')</div>

#usuario/cadastra.blade.php
@extends('layout')
@section('container')

<form>
@include('usuarios._form', ['botao' => 'Cadastrar');
</form>

@stop

@push('javascripts')
  {{ HTML::script('js/usuarios/cadastra.js') }}
@endpush

#usuarios._form.blade.php

<!-- Meu formulário aqui -->

@push('javascripts')
  {{ HTML::script('js/usuarios/_form.js') }}
@endpush

In your case you can also do with @parent

#layout.blade.php

<head>@yield('javascripts')</head>
<div id="container">@yield('container')</div>

#usuario/cadastra.blade.php
@extends('layout')
@section('container')

<form>
@include('usuarios._form', ['botao' => 'Cadastrar');
</form>

@stop

@section('javascripts')
  {{ HTML::script('js/usuarios/cadastra.js') }}
@endsection

#usuarios._form.blade.php

<!-- Meu formulário aqui -->

@section('javascripts')
  @parent
  {{ HTML::script('js/usuarios/_form.js') }}
@endsection
  • Hehe, cool... This one I didn’t know.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.