How @include works in blade templating in Laravel

ⅰ亾dé卋堺 提交于 2021-01-28 20:41:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!