In Laravel 3, I used to do this.
<?php render('partials.header'); ?>
This was done in "PHP" views, without using Laravel's Blade templates.
What's the equivalent of this in version 4?
I tried
<?php @include('partials.header'); ?>
This doesn't work.
If I do
@include('partials.header')
I have to save my file as ".blade.php"
How do I include a "subview" without using the blade template?
I am not sure how many people have been using Laravel 4 in this post, since this post, but if you are looking to include partials or separate your view types you can do it with @includes
for example, if you want a partials folder for your header, footer, sidebar etc
create a directory for the partials under
app/views/partials
Then create a partial
app/views/partials/navigation.blade.php
Then in your master template file add the line
@include('partials.navigation')
That is all it takes.
** Bonus you can also pass data to a partial or include nested partials within a partial
There are different ways to include a view within a view in Laravel 4. Your choice will depend on any one of the outcomes outlined below...
You can compile (render) the partial views in the appropriate Controller, and pass these views to the Main View using the $data['']
array.
This may become tedious as the number of views increase, but hey, at least there's a lot of flexibility :)
See the code below for an example:
...
public function showMyView()
{
/* Header partial view */
$data['header'] = View::make('partials.header');
/* Flexible enough for any kind of partial views you require (e.g. a Header Menu partial view) */
$data['header_menu'] = View::make('partials.header_menu');
/* Footer partial view */
$data['footer'] = View::make('partials.footer');
return View::make('myView', $data);
}
...
You can include the partials above as follows (at any position in your View code):
<html>
<head></head>
<body>
<!-- include partial views -->
<?php echo ($header) ?>
<?php echo ($header_menu) ?>
<div id="main-content-area"></div>
<?php echo ($footer) ?>
</body>
</html>
Your partial views will now be added to your main View.
There's actually a much easier way than using the method above: Simply include this in the html of the view...
<html>
<head></head>
<body>
<!-- include partial view: header -->
<?php echo View::make('partials.header') ?>
<div id="main-content-area">
</div>
<!-- include partial view: footer -->
<?php echo View::make('partials.footer') ?>
</body>
</html>
Make sure that the folder structure for the partials is [views/partials/header.php] in order to provide the correct file-path to the View::make() function of Laravel.
If you try to pass the $data['page_title']
in a controller, the nested views wont receive the data.
To pass data to these nested views you need to do it like this:
<html>
<head></head>
<body>
<?php
/* Pass page title to header partial view */
$data ['page_title'] = "My awesome website";
echo View::make('partials.header', $data);
?>
<div id="main-content-area"></div>
<?php echo View::make('partials.footer') ?>
</body>
</html>
The question clearly stated: "Without using Blade template", so I have made sure to give a solution that does not include any Blade templating code.
You can nest your partials in views try this
View::make('folder.viewFile')->nest('anyname', 'folder.FileName');
Then access the nested view file from your template {{ $anyname }}
this way you don't have to include files in your view and this should work for .php file also.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With