gpt4 book ai didi

Symfony2 FOSUserBundle 重写表单

转载 作者:行者123 更新时间:2023-12-02 11:05:11 25 4
gpt4 key购买 nike

我正在尝试更改应用程序中注册表单的模板,以便我可以向其中添加一些其他 HTML。这是/My/UserBundle/Resources/views/Registration/register.html.twig 文件:

{% extends "MyUserBundle::layout.html.twig" %}

{% block fos_user_content %}
<section class="register site-content">
<header>
<h1>{{ 'layout.register'|trans({}, 'FOSUserBundle') }}</h1>
</header>
<div class="block">
{% include "FOSUserBundle:Registration:register_content.html.twig" %}
</div>
</section>
{% endblock fos_user_content %}

我已经成功覆盖了layout.html.twig:

{% extends 'MyMainBundle::layout.html.twig' %}

{% block title %}{{ site_name }}{% endblock %}

{% block content %}
{% for key, message in app.session.getFlashes() %}
<div class="{{ key }}">
{{ message|trans({}, 'FOSUserBundle') }}
</div>
{% endfor %}
{% block fos_user_content %}{% endblock %}
{% endblock %}

以及 form.html.twig:

{% extends 'FOSUserBundle::form.html.twig' %}

{% block field_row %}
<li class="form_row">
{{ form_label(form) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
</li>
{% endblock field_row %}

{% block form_widget %}
<ul {{ block('container_attributes') }}>
{{ block('field_rows') }}
{{ form_rest(form) }}
</ul>
{% endblock form_widget %}

配置部分:

# FOS User Configuration
fos_user:
db_driver: orm
firewall_name: main
user_class: My\UserBundle\Entity\User
from_email:
address: %admin_email%
sender_name: %site_name%
template:
engine: twig
theme: MyUserBundle::form.html.twig

我已清除缓存。

每当我去

http://localhost/register/

apache 会挂起直到超时。

我能弄清楚的最好消息是,PHP 最大执行消息显示它在第 16 行缓存中的 Twig 模板上崩溃。该行是 function doGetParent(...) 文件是:

<?php

/* FOSUserBundle::form.html.twig */
class __TwigTemplate_9cf68a2af1db50466c556a735bcdeba0 extends Twig_Template
{
public function __construct(Twig_Environment $env)
{
parent::__construct($env);

$this->blocks = array(
'field_row' => array($this, 'block_field_row'),
'form_widget' => array($this, 'block_form_widget'),
);
}

protected function doGetParent(array $context)
{
return "FOSUserBundle::form.html.twig";
}

protected function doDisplay(array $context, array $blocks = array())
{
$this->getParent($context)->display($context, array_merge($this->blocks, $blocks));
}

// line 3
public function block_field_row($context, array $blocks = array())
{
// line 4
echo " <li class=\"form_row\">
";
// line 5
echo $this->env->getExtension('form')->renderLabel($this->getContext($context, "form"));
echo "
";
// line 6
echo $this->env->getExtension('form')->renderErrors($this->getContext($context, "form"));
echo "
";
// line 7
echo $this->env->getExtension('form')->renderWidget($this->getContext($context, "form"));
echo "
</li>
";
}

// line 11
public function block_form_widget($context, array $blocks = array())
{
// line 12
echo " <ul ";
$this->displayBlock("container_attributes", $context, $blocks);
echo ">
";
// line 13
$this->displayBlock("field_rows", $context, $blocks);
echo "
";
// line 14
echo $this->env->getExtension('form')->renderRest($this->getContext($context, "form"));
echo "
</ul>
";
}

public function getTemplateName()
{
return "FOSUserBundle::form.html.twig";
}

public function isTraitable()
{
return false;
}
}

在第65行的\vendor\twig\lib\Twig\Template.php上超时,这是public function getParent(array $context )

很明显 getParent 存在一些问题,但我不知道这意味着什么或如何修复它。

最佳答案

根据 FOSUserBundle 文档:

The easiest way to override a bundle's template is to simply place a new one in your app/Resources folder. To override the layout template located at Resources/views/layout.html.twig in the FOSUserBundle directory, you would place you new layout template at app/Resources/FOSUserBundle/views/layout.html.twig.

As you can see the pattern for overriding templates in this way is to create a folder with the name of the bundle class in the app/Resources directory. Then add your new template to this folder, preserving the directory structure from the original bundle.

在我的项目中,正如他们所说,我覆盖了 FOSUserBundle 的布局,它的工作就像一个魅力。

因此,您需要以同样的方式创建app/Resources/FOSUserBundle/views/Registration/register.html.twig。 (或您想要覆盖的表单)

编辑

好的,我刚刚意识到您已选择扩展 FOSUserBundle。在这种情况下,您需要在包内执行此操作,而不是 app/Resources/ 。但你不需要放

{% extends 'FOSUserBundle::form.html.twig' %}

FOSUserBundle 将检测到您正在覆盖该 bundle 并将自动扩展。

您还需要告诉您的包 FOSUserBundle 是它的父包。

class YourBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}

关于Symfony2 FOSUserBundle 重写表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9221881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com