Last active
November 30, 2016 19:09
-
-
Save ethanclevenger91/16d34b9cc85b6e45b1c5 to your computer and use it in GitHub Desktop.
All in One Event Calendar - Adding filters + functions to Twig themes
For more reading on uses of functions vs. filters in Twig: http://twig.sensiolabs.org/doc/advanced.html
This file contains hidden or 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
function add_twig_functions_and_filters() { | |
global $ai1ec_front_controller; | |
$loader = $ai1ec_front_controller->return_registry(true)->get('theme.loader'); | |
$twig = $loader->get_twig_instance(false, true); | |
function foo($date) { | |
if(mktime() > strtotime($date->__toString)) { | |
return true; | |
} | |
else return false; | |
} | |
$expired = new Twig_SimpleFunction('expired', 'foo'); | |
$twig->addFunction($expired); | |
function bar($string) { | |
return $string.'ringding'; | |
} | |
$filter = new Twig_SimpleFilter('mFilter', 'bar'); | |
$twig->addFilter($filter); | |
} | |
add_action('init', 'add_twig_functions_and_filters'); |
This file contains hidden or 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
{% if expired(event.get('start')) %} | |
<span>{{'Expired'|mFilter}}</span> //Will output 'Expiredringding' | |
{% endif %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you very much, this is just what I was looking for! I was nearly giving up, after looking at the plugin code.