Skip to content

Instantly share code, notes, and snippets.

@apfelbox
Created November 21, 2013 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apfelbox/7579366 to your computer and use it in GitHub Desktop.
Save apfelbox/7579366 to your computer and use it in GitHub Desktop.
varargs in Twig custom functions
<?php
class Extension extends Twig_Extension
{
public function myFunc ()
{
var_dump(func_get_args());
}
public function getFunctions ()
{
return [
new Twig_SimpleFunction("myFunc", [$this, "myFunc"])
];
}
public function getName ()
{
"test";
}
}
<?php
require_once 'vendor/autoload.php';
require_once 'Extension.php';
$loader = new Twig_Loader_String();
$twig = new Twig_Environment($loader, [
]);
$twig->addExtension(new Extension());
echo $twig->render("{{ myFunc(1, 2, 3, 4, 5) }}");
@apfelbox
Copy link
Author

Output:

array (size=5)
  0 => int 1
  1 => int 2
  2 => int 3
  3 => int 4
  4 => int 5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment