twigHow to create a template in PHP Twig?
Creating a template in PHP Twig is a simple process.
{% extends "base.html" %}
{% block title %}My Page{% endblock %}
{% block body %}
<h1>My Page</h1>
<p>Welcome to my page!</p>
{% endblock %}
This example code will create a template that extends the base.html template, and adds a title and body block. The title block will contain the text "My Page", and the body block will contain an h1 tag with the text "My Page" and a paragraph tag with the text "Welcome to my page!".
{% extends "base.html" %}- This line tells Twig to extend the base.html template.{% block title %}My Page{% endblock %}- This line creates a title block with the text "My Page".{% block body %}- This line creates a body block.<h1>My Page</h1>- This line adds an h1 tag with the text "My Page" to the body block.<p>Welcome to my page!</p>- This line adds a paragraph tag with the text "Welcome to my page!" to the body block.{% endblock %}- This line ends the body block.
Helpful links
More of Twig
- How to use yield in Twig with PHP?
- How to parse XML in Twig with PHP?
- How to include a Twig file with PHP?
- How to use Slim/Twig-View in PHP?
- How to prevent Template Injection in PHP Twig?
- How to use a Twig variable in PHP?
- How to check if a string contains a substring in PHP Twig?
- How to use a switch case in PHP Twig?
- How to set a variable in PHP Twig?
- How to use the OR operator in Twig and PHP?
See more codes...