Skip to content

Instantly share code, notes, and snippets.

@caefer
Created March 23, 2011 13:49
Show Gist options
  • Select an option

  • Save caefer/883108 to your computer and use it in GitHub Desktop.

Select an option

Save caefer/883108 to your computer and use it in GitHub Desktop.
<?php
namespace MyProject\MyBundle\Twig\Extension;
class MyExtension extends \Twig_Extension
{
public function getFilters()
{
return array(
'toggle' => new \Twig_Filter_Method($this, 'doToggleFilter'),
);
}
public function doToggleFilter($parameters = array(), $key, $value)
{
if (isset($parameters[$key])) {
$currentValues = explode(',', $parameters[$key]);
if (($set = array_search($value, $currentValues))) {
unset($currentValues[$set]);
} else {
$currentValues[] = $value;
}
$parameters[$key] = implode(',', $currentValues);
} else {
$parameters[$key] = $value;
}
return $parameters;
}
public function getName()
{
return 'my';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment