Skip to content

Instantly share code, notes, and snippets.

@EvanK

EvanK/example.js Secret

Last active March 20, 2017 18:26
Show Gist options
  • Save EvanK/124fd1066fae3e278350c70191eabd69 to your computer and use it in GitHub Desktop.
Save EvanK/124fd1066fae3e278350c70191eabd69 to your computer and use it in GitHub Desktop.
var path = require('path')
var twig = require('node-twig')
var options = {
extensions: [
{
file: path.join(__dirname, 'map.php'),
func: 'mapForTwigExtension'
}
],
context: {
state: 'TX'
}
}
twig.renderFile(path.join(__dirname, 'test.twig'), options, (err, output) => {
if (err) console.error(err)
else console.log(output)
})
<?php
function mapForTwigExtension(\Twig_Environment &$twig) {
$twig->addFilter(
new \Twig_Filter('map',
function ($text, $map, $default = null) {
// If map has a key matching $text, return its mapped value
if (isset($map[$text])) {
return $map[$text];
}
// Otherwise, if a default was specified, return it
if (null !== $default) {
return $default;
}
// Otherwise, return the original value
return $text;
}
)
);
}
So, you're from {{ state | map({ "TX":"Texas", "LA":"Louisiana" }, "WHERE exactly?") }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment