How to create a Symfony2 templating helper
As you get started with Symfony2 you will probably find yourself getting stuck on some tasks that are second-nature to you when developing in symfony 1. One of these will probably be adding a custom helper to your view layer. Hopefully this quick article will clarify that particular process.
Create a helper class
In order to work with the Symfony2 view layer your helper class must implement the HelperInterface interface. This interface is quite simple:
namespace Symfony\Components\Templating\Helper;
interface HelperInterface
{
function getName();
function setCharset($charset);
function getCharset();
}
Most of the time you’ll be extending the abstract base helper class provided
in the core, which provides concrete implementations of the latter two
methods. This remaining method, getName(), should return the name your
helper can be called by from the view layer.
For example, a helper that outputs the Google Analytics tracking code could look something like this:
namespace Application\HelloBundle\Helper;
use Symfony\Components\Templating\Helper\Helper;
class TrackerHelper extends Helper
{
public function getName()
{
return 'tracker';
}
}
Register your helper
Once you have a concrete helper class you could add it directly to the
templating engine using the $engine->set($helper) method. However, Symfony2
provides another, more scalable way to register helpers using the dependency
injection container’s tagging mechanism.
To register a service as a templating helper, add a templating.helper tag
that includes an alias attribute of the name you would like to use to
reference this helper. For example, in hello/config/config.xml:
<services>
<service id="tracker_helper" class="Application\HelloBundle\Helper\TrackerHelper">
<tag name="templating.helper" alias="tracker" />
</service>
</services>
Make it do something!
Now that your helper is registered you can access it in your template files
via the templating engine (i.e. $view->tracker). Let’s make it do something
now so this isn’t just academic.
In order to output the correct tracking code, our helper will need to know what site id to use. We can pass this value to our helper’s constructor:
class TrackerHelper extends Helper
{
protected $profileId;
public function __construct($profileId)
{
$this->profileId = $profileId;
}
public function __toString()
{
// return tracking code that includes $this->profileId
}
// ...
}
Last we just need to configure the constructor parameter in the service container configuration:
<parameters>
<parameter key="tracker.profile_id">UA-XXXXX-XX</parameter>
</parameters>
<services>
<service id="tracker_helper" class="Application\HelloBundle\Helper\TrackerHelper">
<tag name="templating.helper" alias="tracker" />
<argument>%tracker.profile_id%</argument>
</service>
</services>
With that, you can now output the tracking code in your template:
<?php echo $view->tracker ?>
VoilĂ ! You can now get started developing your own Symfony2 templating helpers.