Fork me on GitHub

show

An article by Gaspard Bucher

Display an attribute

Display an attribute

  • actions Show popup/publish actions to edit, propose, publish the node. The parameter is a list of actions separated by commas ("edit,publish").
  • attr find value as attribute
  • eval evaluate a RubyLess expression
  • label (true, false, t, dyn str) prefix with a label field.
  • publish Set whether to auto-publish node after save.
  • text, t RubyLess evaluated string.
  • tlabel translated label text
  • format sprintf format / strftime format
  • zero (hide) hide zero values
  • tz time zone to display date in (list possible)
  • tformat translate a format like ‘short_date’
  • lang lang to translate sprintf format
see actions, attr, eval, label, publish, set, text, t, tlabel

Since ‘show’ is a very common method it can also be accessed with the following shortcut:

<b do='created_at' format='%d'/>

Instead of the longer:

<b do='show' date='created_at' format='%d'/>

This works as long as the method [:created_at, {:format => String}] is not RubyLess safe because Zafu normally first tries the call created_at(:format => "%d") and falls back to “show” on resolution failure.

You can also use the shorter s>

<b do='s' attr='first_name'/>

show an attribute

If you have an attribute that has the same name as a view method, RubyLess will not guess correctly. In these cases, you can use the explicit “show” method with “attr”. For example if you have a property called “form” (which would resolve to the zafu method r_form):

<r:show attr='form'/>

You might also be interested in trans method to dynamically translate attributes and such.

show a date

You can also use “show” to display and format dates:

<r:show date='updated_at' format='%H:%M'/>

An alternative to the method above is:

<span do='updated_at.strftime("%H:%M")'/>

You can also translate date formats (see load dictionary) :

<r:show date='updated_at' tformat='info_date'/>

time zone

You may want to use an explicit time zone when displaying dates instead of the visitor’s time zone:

<r:show date='event_at' tformat='full_date' tz='#{tz}'/>

relative time (age)

Finally, you may want to display time in a relative format (which nicely avoids all the time zone headaches):

<r:show date='event_at' format='age/%Y-%m-%d'/>

The “age” format gives messages such as “1 hour ago”, “in 2 days”. If the date is far in the past or future (more then a week), the date after the ”/” is used.

show a number

You can format the output of a number with “format” and hide null values with the “zero” param:

<r:show attr='value' format='%.2f' zero='hide'/>

label (shortcuts)

If you need to add a label and/or only show the value if it is not blank, you could write:

<li do='if' test='first_name'><label do='t'>first_name</label> <span do='first_name'/></li>

but this is tedious so there is a shortcut:

<li do='s' label='t' blank='hide' attr='first_name'/>

Have a look at the label param for details.