Laravel 4 - including a “partial” view within a vi

2019-01-14 08:28发布

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?

7条回答
疯言疯语
2楼-- · 2019-01-14 08:36

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

查看更多
虎瘦雄心在
3楼-- · 2019-01-14 08:36

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; ?>
查看更多
SAY GOODBYE
4楼-- · 2019-01-14 08:37

From within a view, just echo the other view:

echo View::make('header'); //This will look for a view called header.php
查看更多
Anthone
5楼-- · 2019-01-14 08:39

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>
查看更多
倾城 Initia
6楼-- · 2019-01-14 08:54

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.

查看更多
Rolldiameter
7楼-- · 2019-01-14 08:57

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.

查看更多
登录 后发表回答