Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practice for Context Processors vs. Template Tags?

In which cases is it better to create template tags (and load them into the template), than creating a context processor (which fills the request automatically)?

e.g. I have a dynamic menu that has to be included into all templates, so I'm putting it into my base.html. What is the preferred usage: context processor or custom template tag? And why?

like image 809
mawimawi Avatar asked May 07 '10 21:05

mawimawi


People also ask

Which tag is used for template inheritance?

extends tag is used for inheritance of templates in django. One needs to repeat the same code again and again. Using extends we can inherit templates as well as variables.

What is a context processor?

A context processor is a Python function that takes the request object as an argument and returns a dictionary that gets added to the request context. They come in handy when you need to make something available globally to all templates.

What are Django template tags?

Django Code The template tags are a way of telling Django that here comes something else than plain HTML. The template tags allows us to to do some programming on the server before sending HTML to the client.


1 Answers

Context processors is for putting data (information, content, objects) into the context used to render page.

Template tags are for formatting or processing that content.

A template tag that makes up new data is confusing. Not impossible or wrong, but very confusing.

like image 161
S.Lott Avatar answered Sep 21 '22 17:09

S.Lott