【发布时间】:2018-06-22 18:55:02
【问题描述】:
我正在尝试在 Symfony 3.3.2 (https://symfony.com/doc/2.8/form/form_customization.html) 中自定义表单渲染。
app/Resources/views/form/fields.html.twig
{% block test %}
<p>TEST</p>
{% endblock %}
app/Resources/views/default/new.html.twig
{% form_theme form 'form/fields.html.twig' %}
{{ form_widget(form.age) }}
{{ block('test') }}
src/AppBundle/Controller/DefaultController.php
class DefaultController extends Controller
{
public function indexAction(Request $request)
{
$test = new Test();
$test->setAge(8);
$form = $this->createForm(TestType::class, $test);
return $this->render('default/new.html.twig', array(
'form' => $form->createView(),
));
}
}
src/AppBundle/Entity/Test.php
namespace AppBundle\Entity;
class Test
{
protected $age;
public function getAge()
{
return $this->age;
}
public function setAge($age)
{
$this->age = $age;
}
}
src/AppBundle/Form/TestType.php
class TestType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('age')
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Test::class,
));
}
}
app/config/config.yml
twig:
debug: '%kernel.debug%'
strict_variables: '%kernel.debug%'
form_themes:
- 'form/fields.html.twig'
我想我误解了一些东西,因为我希望看到一个带有 8 数字的输入,并且在字符串“TEXT”下方,但实际上,我只有输入。那么如何在 new.html.twig 中插入自定义块呢?
【问题讨论】: