Twig
Twig template architecture
Base.twig
There are a few boilerplate sections that each page needs, such as:- meta tags, title and important scripts and styles which live in
<head></head> - the main div which generally sets the basic layout
- Secondary scripts and styles which are loaded below the main div
<footer></footer>which contains footer links and other global information
base.twig which all pages extend:
Adding new base files
There may be situations where you need a base file which is very different to the standardbase.twig . Instead of hacking it to pieces by extending all the blocks, rather just create a new file, such as base-login.twig or base-ecommerce.twig and extend that in the page template instead
Macros
Documentation link Macros are essentially functions which spit out template partials. You can pass arguments to the macro which will then be available in the template. They help to reduce code re-use and improve readability.Suggested use
Create a macros folder and files to organize related macros:Partials
We need some way to break page templates up into smaller pieces to keep them from becoming unwieldy as the site grows. These smaller templates could be thought of as ‘sections’, or ‘components’, but I think the most accurate name is ‘partials’ as they are really just isolated groups of HTML that get pulled into templates. Sections fit the same definition, but asection is the name of a semantic HTML element so that might get a bit confusing.
Creating and including partials
A partial should be linked to a specific page. If its global, its probably better placed as a macro. Therefore to help with search, each partial should start with the name of the page its linked to, followed by an underscore and then the name of the partial, e.gcontact_form.twig. The partial should also be stored in a partials folder contained within the page folder.
A partial file just contains HTML, it doesn’t need any special Twig syntax. To include the partial, you add the following line wherever you want it rendered:
Embeds
If your partial has slots that can be overwritten, then you can create your partial withblocks:
include as in the above example if it wants to just output the defaults, or it can use the embed keyword to change it:
