Reputation: 273
I have a problem with my inheritance using blade template. So I have my main template: /layouts/main.blade.php
@section('oneNews')
<div class="row">
<div class="large-12 columns pad_0">
<div class="container">
<div id="da-slider" class="da-slider">
@foreach($aTopNews as $topNews)
<div class="da-slide">
<h2>{{ $topNews['title'] }}</h2>
<p>{{ $topNews['content'] }}.</p>
<a href="/news/view/{{ $topNews['id'] }}" class="da-link">Read</a>
<div class="da-img">{{ HTML::image($topNews['image']) }}</div>
</div>
@endforeach
<nav class="da-arrows">
<span class="da-arrows-prev"></span>
<span class="da-arrows-next"></span>
</nav>
</div>
</div>
</div>
</div>
Now, I want to inherit this template in another template
@extends('layouts.main')
@section('oneNews')
<div class="row">
<div class="large-12 columns pad_0 marg-top-20">
Registru on-line
</div>
</div>
@stop
Controller :
public function getIndex()
{
return View::make('layouts.main')
->with('aNews', \News::where('type', '=', '0')->get())
->with('aTopNews', \News::where('type', '=', '1')->get())
->with('aNotice', \News::where('type', '=', '2')->get());
}
But I get the error : "Undefined variable: aTopNews", Help me please
Upvotes: 1
Views: 2804
Reputation: 44526
First of all, with template inheritance you don't return the layout view, you return the content view and Laravel's Blade engine will take care of fetching and rendering the correct layout: that's the purpose of @extends
at the begining of the file. So in your controller you should have:
return View::make('content'); // assuming content.blade.php contains the code in your example
One thing about template inheritance is that it doesn't pass along variables from the child (content) view to the layout view. So doing View::make('content')->with('aTopNews', $aTopNews)
will only make aTopNews
available for the content
view, and not the layouts.main
view which it extends. To make it available within the layout view you can use View::share()
which makes the variable available for all views:
View::share('aTopNews', \News::where('type', '=', '1')->get());
Also, for section inheritance to work, in your layout file you need to end @section('oneNews')
with @show
.
The Laravel Documentation explains very well how Blade Template Inheritance works.
Upvotes: 1