How to access an underlying object from a Twig'

2019-02-22 04:05发布

问题:

I have a ParamterValue class which references ParamterDefinition class, by property ParamterValue->paramDef. I created ParamterValueType to build a form.

How can I access ParamterValue->paramDef object in a template? I just need it for some logic in rendering a form, I actually don't need to render ParamterDefinition, that's why I don't include paramDef form field in ParamterValueType. Even if I would, how could I access underling object from the form view field?

So the general situation here looks like this: I have an object which I want to create from a form, that object has a reference to another object which has data that are needed to render the form (but I don't need a widget for it, just some data to perform logic). Do I need to include that referenced object in ParamterValueType to get access to it or not?

回答1:

You can usually just do

{{ form.vars.data.paramDef }}

Actually, this only works as of Symfony 2.1.



回答2:

I didn't quite understand what you're trying to do, but I think that creating a form extension or using global vars will get you there.

Take a look at

http://toni.uebernickel.info/2011/11/25/how-to-extend-form-fields-in-symfony2.html

to learn about form extensions and

http://blogsh.de/2012/01/04/how-to-write-host-aware-twig-templates-in-symfony/

to learn about global vars.



标签: symfony twig