简体   繁体   English

如何在Symfony2中将值传递给树枝

[英]How to pass value to twig in Symfony2

I'm trying to have inputs with customized labels (based on other entity attribute). 我试图输入带有自定义标签的输入(基于其他实体属性)。

private $kilos_maxlim;
private $precio;

I set the values of $kilos_maxlim in the constructor of the class. 我在类的构造函数中设置$kilos_maxlim的值。 So, I have that field filled. 所以,我填补了那个领域。 I want to show an input of $precio but with the label based on the value of $kilos_maxlim . 我想显示$precio的输入,但带有基于$kilos_maxlim值的$kilos_maxlim

I have this input field in the classtype: 我在类类型中有此输入字段:

->add('precio', null, array(
    'attr' => array('autofocus' => true),
    'label' => 'label.precio',
))

How can I pass the value without being an input? 如何在没有输入的情况下传递值?

It should work as simple as this: 它应该像这样简单:

public function buildForm(FormBuilderInterface $builder, array $options) {
    // get the actual entity
    $entity = $builder->getData();

    // set the value as the label
    $builder->add('precio', null, array(
        'label' => 'label.precio ' . $entity->getKilosMaxlim(),
    ));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM