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

0

I have a view that includes another view . I want these same views to include a javascript in the section of view main called javascripts .

So:

#layout.blade.php

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

In my view where I use to register the users I 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 .

# usuario._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') to appear together, but only the last one, which is the view usuarios/cadastra.blade , appears.

How can I make the two appear together?

    
asked by anonymous 26.04.2016 / 23:00

2 answers

1

In Laravel 5.2 we have @stack to do this by changing a 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
    
29.04.2016 / 03:45
1

Simply and simply, you stop using @stop at the end of your declarations and start using @append .

Although you have not seen anything in the documentation about it, the BladeCompiler class has some methods that are preceded by the word "compile" before, whose functionality is to compile aquile is captured after @ .

That is, there exists the compileAppend method. So, you can do this in your views:

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


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

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

In this case, when doing things like this, I always recommend using @append instead of @stop , except for @section('content') .

    
27.04.2016 / 20:54