如何在Haml中编写if条件? [英] How to write if-condition in Haml?

查看:65
本文介绍了如何在Haml中编写if条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Ruby on Rails应用程序的Haml中编写ifif-else语句?

How to write if and if-else statements in Haml for a Ruby on Rails application?

推荐答案

HAML是基于缩进的,并且解析器可能很棘手.您无需在Haml中使用-end".请改用缩进.在Haml中,只要在Ruby评估命令后增加缩进量,块就会开始.缩进量减少时结束.如果没有,则按以下示例进行采样.

HAML is indentation based , and the parser can be tricky.You don't need to use "- end" in Haml. Use indentation instead.In Haml,a block begins whenever the indentation is increased after a Ruby evaluation command. It ends when the indentation decreases.Sample if else block as follows.

- if condition
  = something
- else
  = something_else

一个实际例子

- if current_user
  = link_to 'Logout', logout_path
- else
  = link_to 'Login', login_path

如果您只想使用if条件,那么

Edit : If you just want to use if condition then

 - if current_user
  = link_to 'Logout', logout_path

这篇关于如何在Haml中编写if条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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