如何从模板中的 Twig 的 FormView 访问底层对象? [英] How to access an underlying object from a Twig's FormView in a template?

查看:29
本文介绍了如何从模板中的 Twig 的 FormView 访问底层对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ParamterValue 类,它通过属性 ParamterValue->paramDef 引用 ParamterDefinition 类.我创建了 ParamterValueType 来构建表单.

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

如何访问模板中的 ParamterValue->paramDef 对象?我只需要它用于渲染表单的一些逻辑,实际上我不需要渲染 ParamterDefinition,这就是为什么我不在 ParamterValueType 中包含 paramDef 表单字段的原因.即使我愿意,我如何从表单视图字段访问下层对象?

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?

所以这里的一般情况是这样的:我有一个我想从表单创建的对象,该对象具有对另一个对象的引用,该对象具有呈现表单所需的数据(但我不需要一个小部件,只是一些数据来执行逻辑).我是否需要在 ParamterValueType 中包含该引用对象才能访问它?

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?

推荐答案

你通常可以做到

{{ form.vars.data.paramDef }}

实际上,这只适用于 Symfony 2.1.

Actually, this only works as of Symfony 2.1.

这篇关于如何从模板中的 Twig 的 FormView 访问底层对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆