How to write Jinja Tag in Html file in Django?


JINJA is used to write python code under the template page on Django Framework. we can write Jinja under .txt,.HTML, .XML file, etc.

JINGA Print Statements:-

1)  {{ }}  :-  It is used to display content of 

<p> {{res}} </p>

here res is the key of views method that will print res key data under paragraph

<input type="text" value={{res}}       />

2)   {%     %}    it is jinja expression tag we can write any expression under the template page of Django.

Jinja If Statements:-

{ %  if   condition  %}

<p>   True condition

{%  endif    %}

Jinja If--else  Statements:-

{ %  if   condition  %}

<p>   True condition

{%  else  %}

<p>  False Condition </p>

{%  endif    %}

3)  Jinja loop statement:

{%   for var in list|set|tuple|dictionary   %}

<p>  Statement</p>

{% endfor   %}

4)  Jinja Comments:-

{#      #}

5)  Macros

Macros are comparable with functions in regular programming languages. They are useful to put often used idioms into reusable functions to not repeat yourself (“DRY”).

Here’s a small example of a macro that renders a form element:

{% macro input(name, value='', type='text', size=20) -%}
    <input type="{{ type }}" name="{{ name }}" value="{{
        value|e }}" size="{{ size }}">
{%- endmacro %}
The macro can then be called like a function in the namespace:

<p>{{ input('username') }}</p>
<p>{{ input('password', type='password') }}</p>
If the macro was defined in a different template, you have to import it first.

Post a Comment


POST Answer of Questions and ASK to Doubt

Post a Comment (0)