DEV Community

Cover image for How to Install Twig in Php in 2025?
Anna Golubkova
Anna Golubkova

Posted on

1

How to Install Twig in Php in 2025?

Twig is a popular templating engine for PHP that is fast, secure, and flexible. As of 2025, Twig continues to be a preferred choice for developers looking to manage HTML code separately from PHP logic. This guide will walk you through the process of installing Twig in a PHP environment. Following these steps will ensure that your setup is streamlined and optimized for performance.

Prerequisites

Before getting started, ensure you have the following:

  • PHP 8.1 or later installed on your system
  • Composer, a dependency manager for PHP, installed and working
  • Basic understanding of PHP and command-line tools

Step 1: Setting Up Your PHP Project

Create a new directory for your project, or navigate to your existing project directory. This will be the environment where you will install Twig.

mkdir my-php-project
cd my-php-project
Enter fullscreen mode Exit fullscreen mode

Step 2: Using Composer to Install Twig

Composer is the most efficient way to manage your PHP dependencies. Use Composer to install Twig by running the following command:

composer require twig/twig
Enter fullscreen mode Exit fullscreen mode

This command will add Twig to your project's dependencies.

Step 3: Configuring Twig

Once Twig is installed, you need to configure it to work with your PHP files. Create a new PHP file, index.php, and initialize Twig in it:

<?php

require_once 'vendor/autoload.php';

// Define directories
$loader = new \Twig\Loader\FilesystemLoader('templates');
$twig = new \Twig\Environment($loader, [
    'cache' => 'compilation_cache',
]);

// Sample data
$template = $twig->load('sample.html');
echo $template->render(['name' => 'World']);
Enter fullscreen mode Exit fullscreen mode

In this configuration:

  • FilesystemLoader('templates'): This specifies the directory for your Twig templates.
  • Environment: Sets up the Twig environment with an optional cache directory named compilation_cache.

Step 4: Creating a Twig Template

Inside your project, create a directory called templates if it doesn’t already exist. Inside this directory, create a new template file sample.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello Twig</title>
</head>
<body>
    <h1>Hello, {{ name }}!</h1>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

This simple template uses Twig syntax to insert data dynamically.

Helpful Links

As you work with Twig, particularly within frameworks like Symfony or CodeIgniter, you may encounter scenarios that require additional resources. The following links provide extra guidance:

Conclusion

Installing Twig in PHP environments in 2025 is a straightforward process thanks to Composer's ease of use and Twig's well-documented API. By following this guide, you should have a working Twig setup that is ready to generate dynamic and clean HTML output. For advanced features and troubleshooting, the provided resources will be invaluable in your development journey.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • --last-failed: Zero in on just the tests that failed in your previous run
  • --only-changed: Test only the spec files you've modified in git
  • --repeat-each: Run tests multiple times to catch flaky behavior before it reaches production
  • --forbid-only: Prevent accidental test.only commits from breaking your CI pipeline
  • --ui --headed --workers 1: Debug visually with browser windows and sequential test execution

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Practical examples included!

Watch Video πŸ“ΉοΈ

πŸ‘‹ Kindness is contagious

DEV is better (more customized, reading settings like dark mode etc) when you're signed in!

Okay