4
votes

Im using Symfony and Twig in a Silex application.

I have a registration page with a form in:

{% extends "base.twig" %}

{% block title %}Welcome to My Example site{% endblock %}
{% block head %}
{{ parent() }}
{% endblock %}
{% block content %}

<div class="row">
<div class="span12">
    <h2>Register</h2>
    <p>
        Register for this site and we'll give you free access to cool stuff
        in addition you can subscribe to our premium content.
    </p>
   
    <form  action="{{app.config.site.secureUrl}}/register-handler" method="post">
        <fieldset >
            {{ form_widget(form) }}
            <button type="submit" class="btn btn-info">Send</button>
        </fieldset>
    </form>
</div>
</div>

</div>

{% endblock %}

I get the following error when trying to render the page:

Twig_Error_Syntax: The filter "trans" does not exist in "form_div_layout.html.twig" at line 35

I've narrowed this down to the Symfony translation extension not being installed and as such the default template located in:

vendor\symfony\twigbridge\Symfony\Bridge\Twig\Resources\views\Form\form_div_layout.html.twig

does not render correctly.

I've made a new template based on the one above without the translation functions in.

Question

how do I get twig to use the new template instead of the default one?

2

2 Answers

6
votes

If you want to use your own template for form, you just have to specify it in the options when you register Twig :

$app->register(new Silex\Provider\TwigServiceProvider(), array(
      'twig.path' => __DIR__ . '/[Path_to_views_directory]',
      'twig.class_path' => __DIR__ . '/vendor/twig/lib',
      'twig.form.templates'   => array([path_to_your_overriden_template]),
 )) ;

see the documentation : TwigProvider documentation

But I think it is better to use the original template and register the translation provider like this :

$app->register(new Silex\Provider\TranslationServiceProvider(), array(
      'locale' => '[Your_locale]',
      'translation.class_path' =>  __DIR__ . '/../vendor/symfony/src',
      'translator.messages' => array()
)) ;

And if you use validation don't forget to read this cookbook :

Translation cookbook

Hope this will help.

1
votes

You should register the SymfonyBridgesServiceProvider and the TranslationServiceProvider.

That should give you the trans filter and solve your initial problem.