About me

Wednesday, September 26, 2018

python django (django template language 2)

Django Template Language

Tags
for
The for loop over each item in an array for example to display a list of athletes provided in athlete_list:

<ul>
{%for athlete in athlete_list%}
<li>{{athlete.name}}</li>
{%endfor%}
</ul>
if ,elif,and else
Evaluates a variable and if that variable is true the contents of the block are displayed


{% if athlete_list %}
    Number of athletes: {{ athlete_list|length }}
{% elif athlete_in_locker_room_list %}
    Athletes should be out of the locker room soon!
{% else %}
    No athletes.
{% endif %}

in the above if athlete_list is not empty the number of athletes will be displayed by the {{athlete_list|length}} variable . Otherwise if athlete_in_locker_room_list is not empty the message "Athletes should be out..." will be displayed if both lists are empty "No athletes " will be displayed
you can also use filters and various operators in the if tag:
{% if athlete_list|length > 1 %}
   Team: {% for athlete in athlete_list %} ... {% endfor %}
{% else %}
   Athlete: {{ athlete_list.0.name }}
{% endif %}
most templates will return string so mathematical comparisons using filters will generally not work as you expect Length is an exception
Comments
the comment out part of a line in a template use the comment syntax{# #} for example this template would render as 'hello':
{#greeting#}hello
a comment can contain any template code invalid or not For example


{# {% if foo %}bar{% else %} #}

This syntax can only be used for single-line comments (no newlines are permitted between the {# and #} delimiters). If you need to comment out a multiline portion of the template, see the comment tag.
Template inheritance
The most powerful and thus the most complex - part of djangos template engine is template inheritance . template inheritance allow you to build a base skeleton template that contains all the common elements of your site and defines block that child templates can override

 Comments

 The method to comment out part of a line in a template we can use {# #}
by using this we can comment out for example

{#hello#}greetings
The template would render as 'greeting' a comment can contain any code invalid or not for example
{# {% if foo %}bar{% else %} #}
This syntax can only be used for single line comments (no newlines are permitted between the {#and#} delimiters  ) its used only for single line comments but to comment out multiline line comment we use {%comment%}and{%endcomment%} an optional note can be inserted in the first tag this is useful when commenting out code for documentint why the code was disabled .

<p>Rendered text with {{ pub_date|date:"c" }}</p>
{% comment "Optional note" %}
    <p>Commented out text with {{ create_date|date:"c" }}</p>
{% endcomment %}
comment tags cannot be nested

Template inheritance 

 as inheritance in other languages the template inheritance allows us to build a base skeleton template that contains all the common elements of our site and defines blocks that child templates can override
 its easiest to understand template inheritance y starting with an example

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="style.css">
    <title>{% block title %}My amazing site{% endblock %}</title>
</head>

<body>
    <div id="sidebar">
        {% block sidebar %}
        <ul>
            <li><a href="/">Home</a></li>
            <li><a href="/blog/">Blog</a></li>
        </ul>
        {% endblock %}
    </div>

    <div id="content">
        {% block content %}{% endblock %}
    </div>
</body>
</html>
this template will be our base class which is base.html its a simple html skeleton document that you might use for a simple two coloumn page its the job of child templates to fill the empty blocks with content
In this example the block tag defines three blocks that child templates can fill in all the block tag does is to tell the template engine that a child template may override those portions of the template
A child template might look like this :

{% extends "base.html" %}

{% block title %}My amazing blog{% endblock %}

{% block content %}
{% for entry in blog_entries %}
    <br />
<h2>
{{ entry.title }}</h2>
{{ entry.body }}<br />
{% endfor %}
{% endblock %}


More on template inheritance and django template language will be included in next pages till then thanks for reading this blog and kindly expecting your feed backs please reply on comment section and follow let me know your suggestions and follow me and share this blog





9 comments:

  1. Hi! Thank you for the share this information. This is very useful information for online blog review readers. Keep it up such a nice posting like this.
    oneplus mobile service centre
    oneplus service center near me
    oneplus service

    ReplyDelete
  2. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definitely interested in this one. Just thought that I would post and let you know. Nice! thank you so much! Thank you for sharing.
    xiaomi service centre chennai
    redmi service center in chennai
    mi service center in chennai

    ReplyDelete
  3. That is a helpful instrument and every individual who utilizes the system and downloads information and riding on the web, needs to utilize this application. Free Malwarebytes License Key

    ReplyDelete
  4. On Sister’s Day, let us recognize the fact that our brothers and sisters can go to any extent to pull us out from trouble Happy Sisters Day Quotes Wishes

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Newspaper interesting federal rock condition beat mother if. Else summer cost paper my break age easy. Race read including recently present tree result.world-news

    ReplyDelete