Created
August 26, 2014 16:36
-
-
Save chrispappas/da15783c1c932f0e07bd to your computer and use it in GitHub Desktop.
Boolean-safe default filter
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 | |
class DefaultBooleanFilterExtension extends \Twig_Extension { | |
public function getFilters() { | |
return array( | |
new \Twig_SimpleFilter('defaultBoolean', array($this, 'default_boolean')), | |
); | |
} | |
public function default_boolean($value, $default) { | |
if ($value !== false && empty($value)) { | |
return $default; | |
} else { | |
return $value; | |
} | |
} | |
/** | |
* Returns the name of the extension. | |
* | |
* @return string The extension name | |
*/ | |
public function getName() { | |
return 'default_boolean_filter_extension'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment