在局部文件中本地定义的变量对于调用的 erb 模板是否也可见?
如果我在局部变量中声明局部变量,然后从另一个 erb 模板渲染局部变量,后者是否也可以访问这些局部变量?
If I declare local variables in a partial and then render the partial from another erb template, will the latter also have accces to those local variables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我同意布莱恩的建议,尽管我也会推荐您参考 这个答案对于在哪里定义视图相关变量的同一主题更加全面。
I agree with Bryan's advice although I would also refer you to this answer which is a little bit more comprehensive on the same subject of where to define view-related variables.
简短回答:是的。 理论上您将能够访问定义的变量。
更长的答案:不要在视图中定义变量。 向视图添加逻辑(从而将其推送给客户端)确实是很糟糕的做法。 使用业务逻辑模型和操作处理控制器。
Short answer: Yes. You would theoretically be able to access the defined variable.
Longer answer: Don't define variables in views. Adding logic to the views (and therefore pushing it to the client) is really bad practice. Use models for your business logic and controllers for your action handling.