Skip to content

Commit 7c2e376

Browse files
committed
Adding Alter Twig example
1 parent fb5307e commit 7c2e376

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/**
4+
* @param Twig_Environment $env - The Twig Environment - https://twig.symfony.com/api/1.x/Twig_Environment.html
5+
* @param $config - Config of `@basalt/twig-renderer`
6+
*/
7+
function addCustomExtension(\Twig_Environment &$env, $config) {
8+
9+
/**
10+
* @example `<h1>Hello {{ customTwigFunctionThatSaysWorld() }}!</h1>` => `<h1>Hello Custom World</h1>`
11+
*/
12+
// $env->addFunction(new \Twig_SimpleFunction('customTwigFunctionThatSaysWorld', function () {
13+
// return 'Custom World';
14+
// }));
15+
16+
/*
17+
* Reverse a string
18+
* @param string $theString
19+
* @example `<p>{{ reverse('abc') }}</p>` => `<p>cba</p>`
20+
*/
21+
// $env->addFunction(new \Twig_SimpleFunction('reverse', function ($theString) {
22+
// return strrev($theString);
23+
// }));
24+
25+
26+
// $env->addExtension(new \My\CustomExtension());
27+
28+
// `{{ foo }}` => `bar`
29+
// $env->addGlobal('foo', 'bar');
30+
}

packages/edition-twig/patternlab-config.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@
4444
"source/_macros"
4545
]
4646
}
47-
]
47+
],
48+
"alterTwigEnv": [{
49+
"file": "alter-twig.php",
50+
"functions": ["addCustomExtension"]
51+
}]
4852
}
4953
},
5054
"cacheBust": true,

0 commit comments

Comments
 (0)