Concise Syntax:
Twig offers a concise syntax for rendering variables and applying output escaping. This concise syntax enhances template readability and reduces verbosity compared to raw PHP. For instance, using Twig, the syntax for rendering a variable is `{{ var }}`, while in PHP, it would be
<?php echo $var ?> {# Twig syntax for rendering a variable #} {{ var }}
- Double curly braces {{ }} for output
- Curly brace percentage delimiters {% %} for logic
- Curly brace hash delimiters {# #} for comments
IF Condition with Twig
Let's assume you have a variable called gender that indicates whether a user is Male or Female. You want to display a different message based on this condition.
Always use row variable for single user and rows for multiple users
{% if row.gender == 'male' %} <p>Hello, sir! How can we assist you today?</p> {% elseif row.gender == 'female' %} <p>Hello, ma'am! How can we assist you today?</p> {% else %} <p>Hello there! How can we assist you today? </p> {% endif %}
For iterating over an array use below code.
{# Twig shortcut for iterating over an array with a default message #} {% for user in rows %} * {{ user.name }} {% endfor %}
For Example, if you want to list all the employee under unordered list you can use below Twig code.
Imagine you have a collection of multiple users stored within the rows variable. To meticulously process each user individually and showcase their intricate details, consider the following illustrative syntax as an example.
<ul> {% for row_data in rows %} <li>{{row_data.name}}</li> {% endfor %} </ul>
In this example rows is a default variable used for displaying multiple entries from the report.
row_data contains each user details until the last user if you want to access user`s name you use row_data.name same if you want print email you can use row_data.email and so on.
Below is the Example for how to use HTML with twig.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <p> {{ row.name }} is a {{ row. designation }} </p> </body> </html>
Below is the example how you can display the user details from the VIEW in your html.
- Use all the fields details with row and rows variable to access the created view details
- For single user use row. For multiple users use rows.
- You can display all the details which are available in the view.
- Imagine you have Create View with name Employee Master.
- The View contains below fields.
- Username
- Name
- Date Of Birth
- City Name
Should you wish to display the "Username," a simple iteration on the rows keyword will suffice. Just ensure the field name is in lowercase, like username. In case of spaces between field names, replace them with underscores. For instance, "Date Of Birth" becomes date_of_birth, and "City Name" becomes city_name. This approach ensures easy access to every aspect of the data.
Twig: The Modern PHP Template Engine
Twig is a modern template engine designed for PHP that offers several advantages over using raw PHP code for template rendering. It simplifies the process of creating dynamic web pages by providing a clean and concise syntax, security features, and flexibility. In this document, we'll explore some key features that make Twig a preferred choice for template rendering in PHP applications.
Key Features of Twig:
1. Fast Compilation:
Twig compiles templates into optimized PHP code, resulting in minimal overhead compared to using raw PHP. This efficient compilation process ensures that your templates are rendered quickly, contributing to better performance for your web applications.
2. Enhanced Security:
One of Twig's standout features is its sandbox mode, which allows untrusted template code to be evaluated safely. This makes Twig suitable for applications where users may have the ability to modify template designs. The sandbox mode ensures that potential security vulnerabilities are mitigated.
3. Flexibility and Customization:
Twig empowers developers with a flexible laxer and parser, enabling the creation of custom tags, filters, and DSLs (Domain Specific Languages). This flexibility makes it possible to extend Twig to suit even the most complex templating needs.
For More detailed information you can visit to below URL.