Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why using django TemplateTag tag?

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 ?

like image 779
user1946989 Avatar asked Oct 17 '25 04:10

user1946989


1 Answers

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.

like image 50
Daniel Roseman Avatar answered Oct 18 '25 20:10

Daniel Roseman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!