About me

Monday, September 3, 2018

python django (Django Template Language)


Django Template Language

The template language as we explained earlier here we are going to learn more about template language Django template system is not simply python embedded into html this is by design the template system is meant to express presentation not programe logic the django template system provides tags which function similarly to some programming constructs an if tag for boolean tests a for tag for looping etc but these are not simply executed as the corresponding python code and the template system will not execute arbitrary python expressions

Templates
A template is simply a text file it can generate any text base format (HTML,XML, CSV,etc)
which contains variables which get replaced with values when the template is evaluated and tags which control the logic of the template

Here is an example
{%extends "base_generic.html"%}
{%block title %}{{section .title}}{%endblock%}
{%block content%}
<h1>{{section.title}}</h1>
{%for story in story_list%}
<h2><a href="{{story.get_absolute_url}}">{{story.headline|upper}}</a></h2>
<p>{{story.tease|truncatewords:"100"}}</p>
{%endfor%}
{%endblock%}

Variables
Variables look like this {{variable}} when the template engine encounters a variable it evaluates that variable and replaces it with the result variable names consist of any combination of alphanumeric characters and the underscore ("_") but may not start with an underscore The dot (".") also appears in variable sections although that has a special meaning as indiacated below importantly but we cannot have spaces or punctuations characters in variable names the dot are used to access attributes of a variable the above example {{section.title}} will be replaced with the title attribute of the section object
If you use a variable that doesnt exist the template system will insert the value of the string_if_invalid option which is set to ' ' (the empty string) by default note that var in a template expression like {{foo.bar}} will be intterpreted as a literal string and not using the value of the variable bar if one exists in the tempate context

Filters

the filters are used to modify variables to display here is an example for filter like this
{{name|upper}} this displays the value of the{{name}} variable after being filtered through the upper filter which converts text to uppercase by using a pipe (|) to apply a filter filters can be chained
the output of one filter is applied to the next {{text|escape|linebreaks}} is a common idion for escaping text contents then converting line breaks to <p> tags

some filters take arguments a filter argument looks like this {{bio|truncatewords:30}} this will display the first 30 words of the bio variable
there are about sixty builtin template filters you can read that in this link  built-in filter reference
some most commonly used filters are following
default
if a variable is false or empty use given default otherwise use the value of the example:


{{value|default :"nothing"}}
if value is not provided or is empty the above will display nothing
length
returns the length of the value this works for both strings and lists for example:

{{value|length}}
if value is ['a','b','c','d'] the output will be 4
filesizeformat
formats the value like a human readable file size (i.e '13 KB' , '4.1 MB','102 bytes',etc ) for example


{{value|filesizeformat}}
if value is 123456789, the output would be 117.7 MB you can also build custom template tags which will be explained in other posts

Tags

Tags look like this : {% tag %} tags are more complex than variables some create text in the output some control flow by performing loops or logic and some load external information into the template to be used by later variables 
some tags require beginning and ending tags {% tag %} ....... tag contents.....{%endtag%} 
there are many template tags you can find it in the given link as built-in tag reference
there are some commonly used tags which will be explained on next post thanks for viewing my post



MY friends i kindly expect your feedback to improve my posts please share and follow my blog if any doubts please comment 



No comments:

Post a Comment