I don't find any information about using twig without Symfony. I want to use twig in my custom web page without symfony framework. Is it possible?
Twig is the template engine used in Symfony applications. There are tens of default filters and functions defined by Twig, but Symfony also defines some filters, functions and tags to integrate the various Symfony components with Twig templates.
Symfony defaults to Twig for its template engine, but you can still use plain PHP code if you want.
Twig is a template engine for the PHP programming language. Its syntax originates from Jinja and Django templates. It's an open source product licensed under a BSD License and maintained by Fabien Potencier. The initial version was created by Armin Ronacher.
3. raw. By default, everything in Twig gets escaped when automatic escaping is enabled. If you don't want to escape a variable you'll have to explicitly mark it as safe which you can do by using the raw filter.
To give you a runnable sample:
Install Twig in empty project:
composer require "twig/twig:^3.0"
Create the following "test.php" file:
<?php
require_once('vendor/autoload.php');
$loader = new \Twig\Loader\FilesystemLoader('./templates');
$twig = new \Twig\Environment($loader);
echo $twig->render('demo.twig', ['name' => 'Fabien']);
Create the view:
mkdir templates
cd templates
echo "Hello, {{ name }}!" > demo.twig
Run the demo:
cd ..
php test.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With