Advanced Custom Fields helper for WordPress
The templating components a class and a interface: - Functions, which hooks into the Twig environment and adds the functions you add to it. - FunctionInterface, an interface that you must adheer to., (*1)
Lets create the pimple config for the functions and a Hello World function class that we will create later:, (*2)
$pimple['helloWorldFunction'] = function() { return new HelloWorld(); }; $pimple['templatingFunctions'] = function ($c) { return new TemplatingFunctions($c['objectStorage'], array( $c['helloWorldFunction'] )); };
Note the objectStorage is the SPLObjectStorage class., (*3)
You need to implement the FunctionsInterface and create a class that can be added to the Functions service, so lets create a simple hello world one now:, (*4)
class HelloWorld implements FunctionInterface { public function getFunctionName() { return "helloWorld"; } public function getFunctionBody() { echo "Hello world"; } }
As you can see the getFunctionName() gets the name of the function and the getFunctionBody() is what is outputted when you call the function., (*5)
Before we use this in the a twig view we need to get the templatingFunctions service to register the functions, simply create an instance of it and it will setup the hooks, so just call the pimple config:, (*6)
$pimple['templatingFunctions'];
Finally you would use this in a view like this, (*7)
{{ helloWorld() }}
As well as creating your own functions there are a few built in functions:, (*8)