Is there a difference - use native twig-functions to block and include other views or call the yii-class class View this.render and this.beginBlock/endBlock ?

 {% include 'subtemplate.twig '%} {% block pagedata %} {% endblock %} 

can yii - methods

`

 {{ this.render('subtemplate.twig') |raw }} {{ void(this.beginBlock('pagedata')) }} {{ void(this.endBlock()) }} 

`

    1 answer 1

    I think that the use of template engines depends solely on the development team. Not everyone can work with template engines, which causes development difficulties. And so it’s your business. There is no strong difference. In the case of Twig, there is no waste of resources - the template is translated into the same PHP code. Templates are needed to separate business logic and display logic.

    Example number 1: the same core is installed on different sites. If standardization is used, updating the engine is reduced to a simple code fill. If standardization is not used, then for each site core must be edited separately.

    Example # 2: the same application can return both HTML and JSON. If a template was used for the first, then you can add the second without touching the code. Otherwise, the code will have to be rewritten (and duplicated).

    Also dug up an example of Native vs Twig (Yii2):

    Native yii2

     <? echo $user->percent?$user->percent:0; ?> <? echo $user->getTotalLikes(); ?> <? if ($user->countComments > 0): ?> <? $this->renderPartial('//user/_comments', array('comments'=>$user->comments)); ?> <? else: ?> <div style="padding-left: 35px;"><? echo Yii::t('user', 'Π’Ρ‹ Π½Π΅ добавляли ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠ΅Π² ΠΊ постам'); ?>.</div> <? endif; ?> <p><? if (!empty($ct['parent']->image)): ?><img src="<?php echo $ct['parent']->getImageUrl('small'); ?>"><? endif; ?> <? echo Yii::app()->createUrl('user/register', array('ref'=>$user->id)); ?> 

    Twig yii2

     {{ user.percent|default(0) }} {{ user.getTotalLikes() }} {% if user.countComments %} {% include "views/user/_comments.twig" %} {% else %} <div style="padding-left: 35px;">{{ Yii.t('user', 'Π’Ρ‹ Π½Π΅ добавляли ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠ΅Π² ΠΊ постам') }}.</div> {% endif %} <p>{% if comment.getImageUrl %}<img src="{{ comment.getImageUrl('small') }}">{% endif %} /user/register?ref={{ user.id }} 

    PS You might also like Smarty. The choice is individual for everyone. Successful coding ...

    • Thank you, not quite what I would like to hear. Using twig is an already resolved issue. Simply in tweig, you can enclose and use blocks in the native Twig-way `` `{% include 'subtemplate.twig'%} {% block pagedata%} {% endblock%}` `` and you can yii - using the `` `{{ this.render ('subtemplate.twig') | raw}} {{void (this.beginBlock ('pagedata'))}} {{void (this.endBlock ())}} `` ` - Lenny