Home Twig Templating Engine: Five Minute Guide

Twig Templating Engine: Five Minute Guide

Twig is a must-have tool for any PHP developer. It’s a powerful templating engine that allows you to leave the HTML out of your back-end code, and put it where it belongs – in the template files. It’s particularly handy if you’re working alongside a front-end developer, as you can work in parallel. Caching helps to keep everything running quickly.

This guide is a quick introduction to using Twig in your projects. If you’ve never used a templating engine before or you’re a seasoned pro, this guide will get you up and running with Twig. Once you’ve started using it, you can’t go back.

Get Twig

It’s a good idea to start with the introduction first. This gets you the Twig download and some setup instructions.

A Quick Example

Here’s a quick way to build a simple navigation bar with Twig. First, the template file:

This is going to loop through a list of links and display them, or show an apologetic message if there are no links.

Now for the PHP:

After setting up Twig, this simply builds an array of links and sends the data to the parser.

Changes Not Showing Up?

Twig includes built-in caching, which is worth turning on for the performance boost. You’ll need to remember to delete the files from the cache folder when putting changes live though, or you’ll see the cached templates.

If you don’t want to keep clearing the cache folder while building a new page, just disable caching on your development site. You can disable the cache by setting the “cache” value in the array to false. The PHP example included above includes caching, albeit with a path that you will need to fill in.

What Else?

There is a lot you can do with Twig. We may come back to this topic in the future.

Photo by Nrbelex

About ReadWrite’s Editorial Process

The ReadWrite Editorial policy involves closely monitoring the tech industry for major developments, new product launches, AI breakthroughs, video game releases and other newsworthy events. Editors assign relevant stories to staff writers or freelance contributors with expertise in each particular topic area. Before publication, articles go through a rigorous round of editing for accuracy, clarity, and to ensure adherence to ReadWrite's style guidelines.

Get the biggest tech headlines of the day delivered to your inbox

    By signing up, you agree to our Terms and Privacy Policy. Unsubscribe anytime.

    Tech News

    Explore the latest in tech with our Tech News. We cut through the noise for concise, relevant updates, keeping you informed about the rapidly evolving tech landscape with curated content that separates signal from noise.

    In-Depth Tech Stories

    Explore tech impact in In-Depth Stories. Narrative data journalism offers comprehensive analyses, revealing stories behind data. Understand industry trends for a deeper perspective on tech's intricate relationships with society.

    Expert Reviews

    Empower decisions with Expert Reviews, merging industry expertise and insightful analysis. Delve into tech intricacies, get the best deals, and stay ahead with our trustworthy guide to navigating the ever-changing tech market.