HAML:仅当条件为真时才创建容器/包装元素
远景,但我想知道是否有任何方法可以做这样的事情:
%p # ONLY SHOW THIS IF LOCAL VARIABLE show_paras IS TRUE
= name
换句话说,它总是显示里面的内容,但如果(某些条件)为真,它只在其周围包裹一个容器。
Longshot, but I'm wondering if there's any way to do something like this:
%p # ONLY SHOW THIS IF LOCAL VARIABLE show_paras IS TRUE
= name
In other words, it always shows the content inside, but it only wraps a container around it if (some-condition) is true.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用原始 html,但是您必须在开头和结尾都有 if 语句:
假设您所做的不仅仅是
= name
,您可以使用部分:您可以还使用 HAML 的
surround
(虽然这有点乱):最后,我可能会做的就是根本不尝试省略
p
标签,而只使用 CSS 类设置两个不同的p
样式看起来像我想要的方式:You could use raw html, but then you'd have to have the if statement both at the beginning and end:
Assuming you're doing more than just
= name
, you could use a partial:You could also use HAML's
surround
(though this is a little messy):Finally, what I would probably do is not try to omit the
p
tag at all, and just use CSS classes to set up two differentp
styles to look the way I want:另一种选择是,如果不满足条件,则使用
haml_tag
将其包装在替代标记中:Another option is to wrap it in an alternative tag if the condition isn't met, using
haml_tag
:我能想到的最干净的方法是这样的:
但这并不完全是haml。
一般来说,标记是针对内容的,所以如果 show_paras 是一个更具表现力的调整,您可能应该使用 css 来更改 %p 的行为
The cleanest way I can think of doing is like this:
But it's not exactly haml.
Generally markup is the for the content, so if show_paras is a more presentational tweak you should probably be using css to change the behaviour of the %p instead
https://github.com/haml/haml/commit/66a8ee080a9fb82907618227e88ce5c2c969e9d1
https://github.com/haml/haml/commit/66a8ee080a9fb82907618227e88ce5c2c969e9d1