문제

I have been reading some django code recently and the tag Templatetag is heavily used :

  {% templatetag openblock %} block page_title {% templatetag closeblock %}
  Page Title 
  {% templatetag openblock %} endblock page_title {% templatetag closeblock %}

what are the advantages over the shorter syntax below :

  {% block page_title %}Page Title{% endblock %}

documentation says that templatetag can be use for:

openblock   {%
closeblock  %}
openvariable {{;
closevariable }};
openbrace {;
closebrace };
opencomment {#;
closecomment #};

for me, it just make code longer, so in which case should i use it ?

도움이 되었습니까?

해결책

Those aren't the same at all. The templatetag tag outputs the literal characters. So the first one actually renders in the output as {% block page_title %}, whereas the second one interprets the tag and renders the block.

I don't know how your template was used, but looks as though it was dynamically outputting another template that will then be rendered in turn.

다른 팁

It is useful to create project templates, because the pre-processor of the "startproject" command will process these symbols ({%, {{ etc) and remove them incorrectly without this approach.

Example: https://github.com/twoscoops/django-twoscoops-project/blob/develop/project_name/templates/base.html

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top