问题
I was curious about how @include
works in Laravel Blade, I mean if we use it in a loop like this
@foreach($posts as $post)
@include('parts.post')
@endforeach
will we load this file x
times where x
is amount of posts or we load this file once and use it x
times?
Thanks
回答1:
The blade template engine works by turning blade-html files into php-html files. @include
will be replaced only once e.g.
<!-- parts/post.blade.php -->
<p>This is my post: {{$post}} </p>
<!-- some-template.blade.php -->
@foreach($posts as $post)
@include('parts.post')
@endforeach
Will be rendered into the following php-html code and saved into a view file (see storage/framework/views
if you want to see this):
<?php for($posts as post){ ?>
<p>This is my post: <?php echo($post); ?> </p>
<?php } ?>
来源:https://stackoverflow.com/questions/33977759/how-include-works-in-blade-templating-in-laravel