在symfony2中创建事件以呈现twig文件

I'm new to symfony2 and I'm looking for solution to create an event that render a twig file.

Assume we have a base template

<!DOCTYPE html>
<html>
<head>
    {% block meta %}{% endblock %}
    <title>{% block title %}Welcome!{% endblock %}</title>
    {% block stylesheets %}{% endblock %}
</head>
<body data-cachetime="{{ cacheTimeStamp() }}">
    {% block header %}{% endblock %}
    <div id="content">
        <div class="container">
            {% block content %}{% endblock %}
        </div>
    </div>
    {% block footer %}{% endblock %}
    {% block version %}{% endblock %}
    {% block javascripts %}{% endblock %}
    {% block trackers %}{% endblock %}
</body>
</html>

As you can see, I've a "version" block. I've created an VersionBundle which read the version from a file. The version twig template

{% block version %}
    <div id="version"><p>Version: {{ versionString() }}</p></div>
{% endblock %}

calls an "ViewHelper" (i came from Zend ;-)) which calls the function from VersionBundle. But now the tricky part: The VerionBundle is only registered for 'dev' and 'test' Environment in the AppKernel. Thats why i create the 'version' block instead of calling the ViewHelper directly in the base twig file.

But i don't know how to create an event to render the version twig template first so the data will passed to the base twig.

I would maybe change the way you try to do it, using more Symfony features and avoiding defining Zend like "helpers". It's a good rule of thumb to keep Twig as minimal as it can be, just rendering view from data the templates receive.

First, define the version variable in parameters_dev.yml and parameters_test.yml files (in app/config). Example:

parameters:
    version: 32

Define it as version: ~ in parameters.yml.

Make this variable available in all Twig templates in the app/config/config.yml file:

twig:
    globals:
        version: %version%

Then, in your version block definition, do the following:

{% block version %}
    {% if version is not null %}
        <div id="version"><p>Version: {{ version }}</p></div>
    {% endif %}
{% endblock %}

Hope it helps!