Skip to content

Instantly share code, notes, and snippets.

@chrispappas
Created August 26, 2014 16:36
Show Gist options
  • Save chrispappas/da15783c1c932f0e07bd to your computer and use it in GitHub Desktop.
Save chrispappas/da15783c1c932f0e07bd to your computer and use it in GitHub Desktop.
Boolean-safe default filter
<?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