Php 使用Symfony表单、Twig和SymfonyTwigBridge时,如何覆盖默认模板?

Php 使用Symfony表单、Twig和SymfonyTwigBridge时,如何覆盖默认模板?,php,twig,symfony-forms,silex,Php,Twig,Symfony Forms,Silex,我在Silex应用程序中使用Symfony和Twig 我有一个注册页面,其中有一个表格: {% extends "base.twig" %} {% block title %}Welcome to My Example site{% endblock %} {% block head %} {{ parent() }} {% endblock %} {% block content %} <div class="row"> <div

我在Silex应用程序中使用Symfony和Twig

我有一个注册页面,其中有一个表格:

{% 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 %}
无法正确渲染

我在上面的模板的基础上制作了一个新模板,没有中的翻译功能

问题: 如何让twig使用新模板而不是默认模板?

您应该注册和


这将为您提供trans筛选器并解决您最初的问题。

如果您想使用自己的表单模板,您只需在注册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]),
 )) ;
请参阅文档:

但我认为最好使用原始模板,并像这样注册翻译提供商:

$app->register(new Silex\Provider\TranslationServiceProvider(), array(
      'locale' => '[Your_locale]',
      'translation.class_path' =>  __DIR__ . '/../vendor/symfony/src',
      'translator.messages' => array()
)) ;
如果您使用验证,请不要忘记阅读本食谱:


希望这能有所帮助。

两个答案都很好。谢谢。链接好像不见了?
“twig.form.templates”
对我来说不是正确的键。而使用
“twig.form.resources”
则有效。看见
$app->register(new Silex\Provider\TranslationServiceProvider(), array(
      'locale' => '[Your_locale]',
      'translation.class_path' =>  __DIR__ . '/../vendor/symfony/src',
      'translator.messages' => array()
)) ;