Skip to content

timworxDE/leaf-twig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Leaf Twig

Latest Stable Version Twig Version

The standalone version of Symfony's Twig templating engine for use in the Leaf framework.

This package is oriented towards the Leaf/Blade package.

Installation

Install using composer:

composer require timworx/leaf-twig

Usage

Create a Twig instance by passing it the folder(s) where your template files are located, and the Twig options like a cache folder. Render a template by calling the render method. More information about the Twig templating engine can be found on https://twig.symfony.com/.

use Leaf\Twig;

$twig = new Twig(['templates'], ['cache' => 'var/cache']);

You can also initialise it globally and configure the instace later.

$twig = new Twig;

// somewhere, maybe in a different file
$twig->configure(['templates'], ['cache' => 'var/cache']);

// alternative
$twig->config(['templates'], ['cache' => 'var/cache']);
echo $twig->render('index.html.twig', ['name' => 'John Doe']);
exit;

We can have this as our template index.html.twig

<!Doctype html>
<html>
    <head>
        <title>{{ name }}</title>
    </head>
    <body>
        <div class="container">{{ name }}</div>
    </body>
</html>

You can extend Twig using TwigFilters, TwigFunctions and Extensions:

// Function
$twig->addFunction('md5', function ($string) {
    return md5($string);
});

// Filter
$twig->addFilter('md5', function ($string) {
    return md5($string);
});

// Extension
$twig->addExtension(new \App\Extension\MyExtension()); // Your own created extension

Which allows you to use the following in your blade template:

MD5 hashed string with function: {{ md5(name) }}
MD5 hashed string with filter: {{ name|md5 }}

You can also set global variables:

$twig->addGlobal('myGlobal', 'The Value');

If you want to use additional Twig functions, you can access the Twig instance.

$twig->twig(); // The Twig environment instance

For more Twig directives check out the original documentation.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages