Giter Site home page Giter Site logo

learnjinja2's Introduction

Jinja2 is a versatile template engine that can be used for a wide range of templating and string manipulation tasks in Python applications. Here are some of the basic usages of Jinja2:

  1. Variable Substitution:

    Use double curly braces {{ variable_name }} to insert dynamic data into your templates. You can substitute variables with actual values when rendering the template.

    <p>Hello, {{ name }}!</p>

    In Python:

    template = env.get_template('template.html')
    context = {'name': 'John'}
    output = template.render(context)
  2. Filters:

    Filters allow you to modify variable values before they are rendered. For example, you can use the upper filter to convert a string to uppercase.

    <p>{{ text_variable | upper }}</p>
  3. Control Structures:

    Use {% %} to include control structures like loops, conditionals, and blocks in your templates. For example, you can use {% for %} to loop through a list.

    <ul>
    {% for item in items %}
        <li>{{ item }}</li>
    {% endfor %}
    </ul>
  4. Comments:

    You can add comments to your templates using {# #}. Comments are not rendered and are useful for documentation or notes.

    {# This is a comment #}
  5. Inheritance and Template Blocks:

    Jinja2 supports template inheritance, allowing you to define a base template with common structure and then extend or override blocks in child templates.

    Base Template (base_template.html):

    <!DOCTYPE html>
    <html>
    <head>
        <title>{% block title %}Default Title{% endblock %}</title>
    </head>
    <body>
        <div id="content">
            {% block content %}{% endblock %}
        </div>
    </body>
    </html>

    Child Template (child_template.html):

    {% extends "base_template.html" %}
    
    {% block title %}Custom Title{% endblock %}
    
    {% block content %}
        <p>This is custom content.</p>
    {% endblock %}
  6. Macros:

    Macros are reusable templates for commonly used components or HTML snippets. You can define macros and include them in your templates.

    {% macro alert(message) %}
    <div class="alert">
        {{ message }}
    </div>
    {% endmacro %}

    Usage:

    {{ alert("This is an alert!") }}
  7. Template Inclusion:

    You can include other templates within a template using the {% include %} tag.

    {% include "header.html" %}

    This is useful for modularizing your templates.

These are some of the basic usages of Jinja2. Depending on your project, you can use Jinja2 for more advanced tasks such as internationalization, working with complex data structures, and creating custom filters and extensions.

learnjinja2's People

Contributors

prateekrajgautam avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.