Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig using without Symfony [closed]

Tags:

php

twig

symfony

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?

like image 588
zhinyz Avatar asked Nov 09 '15 17:11

zhinyz


People also ask

Is Twig a Symfony?

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.

Can you use PHP in Twig?

Symfony defaults to Twig for its template engine, but you can still use plain PHP code if you want.

Is Twig a open source?

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.

What is raw in Twig?

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.


1 Answers

To give you a runnable sample:

  1. Install Twig in empty project:

     composer require "twig/twig:^3.0"
    
  2. 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']);
    
  3. Create the view:

     mkdir templates
     cd templates
     echo "Hello, {{ name }}!" > demo.twig
    
  4. Run the demo:

     cd ..
     php test.php
    
like image 129
Alain Tiemblo Avatar answered Sep 21 '22 20:09

Alain Tiemblo