The second function will get the current date in d/m/y format. Our first simple tag will count the number of contacts we have. ![]() We are going to do two examples of simple tags. For example I wrote a template tag that subclasses etc) and require you to only implement a function that can accept these arguments (and optionally a context variable to give you access to the template context.Įssentially they're an upgrade from template filters, because instead of accepting only 1 or 2 arguments, you can accept as many as you like (and you can also access the template context). The simple tag is a function that takes one or more arguments, wraps it in a render function, and registers it with the template system. Template tags can change the way the rest of the template is parsed, and have access to anything in the context in which they are used. Use case: You want to use modify one of the variables in the context slightly before printing it. They are limited in that they cannot access the template context, and can only accept a limited number of arguments. They're just functions that take one or two arguments. Template filters can operate on any object (and at most two at once). inclusion tags: allow you to render arbitrary extra templatesĬan someone give an example outlining when I would want to use one of these over another?.simple_tags: take strings and template variables and returns a string, you are passed the value of the template variable rather than the variable itself (when would you ever want the variable itself over the value?).template tags: access to anything you can access in a view, compiled into nodes with a specified render function (it seems like the only advantage is that you can add variables to the context?).template filters: only operate on strings and return strings. ![]() Here is my perception of the distinctions: For example, what can a template tag do that a simple_tag cannot? Is a filter limited to manipulating strings only and is that why the documentation says that template tags are more powerful because they can "do anything"? I just read the documentation page on template tags:īut I'm finding it difficult to know when I should use one variation over another. This is more of a general question about the distinctions between these four different kinds of django tags.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |