-
-
Save EvanK/124fd1066fae3e278350c70191eabd69 to your computer and use it in GitHub Desktop.
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
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) | |
}) |
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 | |
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; | |
} | |
) | |
); | |
} |
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
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