Last active
August 29, 2015 13:57
-
-
Save joelpittet/9772785 to your computer and use it in GitHub Desktop.
Example Twig Macro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"twig/twig": "1.*" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'vendor/autoload.php'; | |
$loader = new Twig_Loader_Filesystem('.'); | |
$twig_options = [ | |
'cache' => FALSE, | |
'autoescape' => TRUE, | |
'strict_variables' => FALSE, | |
// 'debug' => TRUE, | |
'auto_reload' => TRUE, | |
]; | |
$twig = new Twig_Environment($loader, $twig_options); | |
// $twig->addExtension(new Twig_Extension_Debug()); | |
// The links. | |
$links = [ | |
[ | |
'name' => 'link 1', | |
'href' => '#', | |
], | |
[ | |
'name' => 'link 2', | |
'href' => '#', | |
'links' => [ | |
[ | |
'name' => '<b>sub link 2.1</b>', | |
'href' => '#', | |
'links' => [], | |
], | |
[ | |
'name' => 'sub link 2.2', | |
'href' => '#', | |
'links' => [], | |
], | |
], | |
], | |
]; | |
echo $twig->render('page.twig', ['links' => $links]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% import "menu.twig" as menu %} | |
<!DOCTYPE html> | |
<html> | |
<body> | |
{{ menu.menu_links(links) }} | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should be able to just download these and run
composer update
(if you have composer).