symfony - Twig - how to efficiently re-use code -
i have page render details of many users. each user's html quite heavy (tooltips etc.) don't want copy/paste same code. i'd reuse same code on different pages well.
i using include parameters i'm not sure how performance be. better make twig extension , have there functions producing html based on passed user instance? or there another, better approach?
edit:
this kind of code i'm rendering in multiple places on multiple pages.
<span data-tooltip aria-haspopup="true" class="has-tip" title="{{ user.getjob }}, {{ user.getcompanyy.getname }}"> {{ user.getfirstname }}, {{ user.getlastname }} </span>
include solution , fit needs.
you use macros (documentation). try not use include
nor require
in macro because if have error in included template, line of error line called template (not 1 have bug) become difficult debug.
and said, twig extension specifics cases. instance, if want display price, it's better make |price
extension instead of include little template. extensions better highly reusable codes , shorts templates.
by way, worst case using render(controller(...))
in order display template because it's consuming in terms of memory.
answer edit:
here find benchmark: extension seems best solution in case of lot of parameters. in simple case, both solutions good. personnally, prefer use includes when have lot of html , few parameters. it's question of prefer.
you imagine doing both.
- include of html widget
- and extension in order concatenate user firstname , lastname
like this:
<span data-tooltip aria-haspopup="true" class="has-tip" title="{{ user.getjob }}, {{ user.getcompanyy.getname }}"> {{ user|fullname }} </span>
in conclusion, twig provides lot of differents tools , it's choose once fits needs.
Comments
Post a Comment