if

An article by Gaspard Bucher

conditional execution

  1. action
  2. ajax
  3. API
  4. classes
  5. common attributes
  6. conditions
    1. blank?
    2. case
    3. ClassCondition
    4. else
    5. if
    6. is_ancestor?
    7. selenium
    8. when
  7. context
  8. dates
  9. display
  10. forms
  11. i18n
  12. meta
  13. SQLiss
  14. urls

conditional execution

  • test test condition (RubyLess)
  • attr test condition (node attribute)
  • in ancestor tag name
see ..._if, case, ClassCondition, else, when, zena 1.0

Conditional execution can be used to display content depending on user rights or other conditions:

<r:if test='can_edit?'>
  <r:link mode='edit'>clic to edit</r:link>
</r:if>

When the test cannot be confused with a regular method, the code above can be written more shortly without the “if” or with a “do” method:

<r:can_edit?>
  <r:link mode='edit'>clic to edit</r:link>
</r:can_edit?>

<div do='can_edit?'>
  <r:link mode='edit'>clic to edit</r:link>
</div>

If you want to use > or < in tests, you need to escape them with &gt; and &lt; respectively:

<r:if test='created_at &gt; date'>

workflow conditions

Available conditions related to user rights:

can_edit? can_write? can_drive?
public? empty?

conditional contexts / class scopes

Any context and ClassCondition behave like an “if” conditional execution:

<div do='pages' limit='1'>
  ... has pages ...
  <r:else>
    does not have pages
  </r:else>
</div>

Note that you should limit the query to a single object if you just want to test for page existence.

Or

<div do='Image'>
  ... I am an image ...
  <r:else>
    I am not an image
  </r:else>
</div>

ancestor tag

You can use in to test wether the current element is inside a given ancestor method:

<r:if in='form'>we are in a form</r:if>