Jinja Templates
Jinja is a powerful templating engine for Python & Dart, commonly used for rendering HTML templates
Jinja Syntax Documentation for HTML Templates
Overview
Jinja is a powerful templating engine for Python & Dart, commonly used for rendering HTML templates. It provides a flexible syntax for integrating logic into your HTML files. Below are key Jinja features and their usage in HTML templates.
Basic Syntax
Printing Variables
To output the value of a variable in a Jinja template, use the following syntax:
Example:
This will render:
Comments
To add comments in a Jinja template that will not appear in the rendered HTML, use:
Example:
Control Structures
If Statements
To include conditional logic in your template, use the if
statement:
Example:
For Loops
To iterate over a collection, use the for
loop:
Example:
Elseif
To handle multiple conditions, use elif
in combination with if
:
Example:
String Formatting
Jinja supports string formatting using its format
method:
Example:
This will render:
Includes
To include other templates within a template, use the include
statement:
Example:
Extends
To extend a base template and override specific blocks, use the extends
and block
statements:
Extending a Template
Defining Blocks
In the extended template, define blocks to be overridden:
Overriding Blocks
In the extending template, override blocks as needed:
Adding Variables to Widgets in Controllers
When developing with the WebApp package in Dart, you might need to pass variables from your controllers to the views or widgets for rendering. This process is similar to how you would handle variables in Jinja or other template engines. Here’s a comprehensive guide on how to accomplish this in your Dart controllers.
1. Using rq.addParam
for Single Variables
If you need to add a single variable to your request, use the addParam
method. This is useful for straightforward cases where only a few variables are involved.
Parameters:
'name'
: The key or name of the variable you want to add.variable
: The value of the variable.
Example Usage:
In this example, the variable userName
is added to the request with the key 'userName'
. You can then access this variable in your template.
2. Using rq.addParams
for Multiple Variables
For adding multiple variables at once, use the addParams
method. This approach is ideal when dealing with multiple variables or a collection of parameters.
Parameters:
'exampleTString'
: The key for the first variable, with a value generated byTString
.'examplePathString'
: A translated string.'exampleTranslateParams'
: A translated string with dynamic parameters.
Example Usage:
In this example, multiple variables are added to the request in one go. The welcomeMessage
, currentYear
, and userDetails
are all available for use in the template.
Rendering Templates
To render a template with the added variables, use the renderTemplate
method. This will compile the template and inject the variables into it.
Parameters:
'title'
: A static string for the title.'year'
: The current year.'user'
: User details fetched from some method.
Summary
Jinja syntax allows you to embed logic and variables into your HTML templates efficiently. By using control structures like if
statements and for
loops, formatting strings, and including or extending templates, you can create dynamic and reusable HTML content. This documentation provides a foundation for using Jinja in your projects, enhancing your ability to manage and render templates effectively.
And in controllers use rq.addParams or getParams
- Use
rq.addParam
for adding single variables. - Use
rq.addParams
for adding multiple variables at once. - Render templates using
renderTemplate
after adding the necessary variables.
This approach helps in efficiently managing and rendering dynamic content in your web application, similar to how you would handle variables in Jinja or other templating systems.