Create custom Twig node and parser #DSL

{
"require": {
"twig/twig": "^2.4"
},
"autoload": {
"psr-4": {
"SWP\\": "src/SWP/"
}
}
}
<?php
// load composer autoloader
require_once
__DIR__.'/vendor/autoload.php';
// define Twig loader with our simple template
$loader = new Twig_Loader_Array([
'index' => '{% gimme article with {\'foo\': \'bar\'} %} {{ dump(article) }} {% endgimme %}',
]);
// create Twig instance and pass there our loader (and enable debug extension).
$twig = new Twig_Environment($loader, ['debug' => true]);
$twig->addExtension(new \Twig_Extension_Debug());
// register our custom extension
$twig->addExtension(new \SWP\Extension\GimmeExtension());

echo $twig->render('index');
src/SWP/Extension/GimmeExtension.php
{% gimme article with {'foo': 'bar'} %} {# loaded data will be visible under `article` variable. #} {{ dump(user) }} {% endgimme %}
src/SWP/Parser/GimmeTokenParser.php
src/SWP/Node/GimmeNode.php

Paweł Mikołajczuk

Written by

Open Source developer (#php, #javascript). Focused on software for newsrooms. Github: https://github.com/ahilles107

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade