简体   繁体   中英

Laravel 4 - including a “partial” view within a view (without using Blade template)

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?

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...

For Flexibility

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:

Controller

...

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);
}

...

View

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.


For Simplicity

There's actually a much easier way than using the method above: Simply include this in the html of the view...

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.

WARNING

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>

NOTE

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.

Good luck :)

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.

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

I know this is a bit of a late answer, but I figured since I didn't see this solution amongst the other answers it was ok.

If you want to include your header and footer on every page I would add them into the before and after filters. Just go to filters.php in your app folder

App::before(function($request)
{
    echo View::make('partials.header');
});

App::after(function($request, $response)
{
    echo View::make('partials.footer');
});

When doing it this way you don't need to add anything in the view files to include them.

You can use View's nest function

View::make('default.layout')->nest('header', 'default.header');

Use the third parameter to pass data to the template

View::make('default.layout')->nest('header', 'default.header', ['name' => 'John Doe', 'test' => 'It works!']);

on your views/default/header.blade.php

<div>hey {{ $name }}! {{ $test }}</div>

I am still pretty new to Laravel, but I think the below is pretty ideal ...

Route::get('/', function()
{
    $data['title'] = 'sample';
    return View::make('page', $data);
});

# /views/partials/header.php
# /views/partials/footer.php
View::composer('page', function($view)
{
    $view->with('header', View::make('partials.header', $view->getData()));
    $view->with('footer', View::make('partials.footer', $view->getData()));
});

See Laravel View Composers .. http://laravel.com/docs/responses#view-composers

/views/page.php

<?php echo $header; ?>
<div>CONTENT</div>
<?php echo $footer; ?>

从一个视图中,只需回显另一个视图:

echo View::make('header'); //This will look for a view called header.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM