Skip to content

Instantly share code, notes, and snippets.

@jtrim
Created January 24, 2013 22:03
Show Gist options
  • Select an option

  • Save jtrim/4628410 to your computer and use it in GitHub Desktop.

Select an option

Save jtrim/4628410 to your computer and use it in GitHub Desktop.
Possible way to express conditionals in dual purpose (rails + mustache) templates.
# The following is a possible way we can express conditionals in templates that
# are written to be rendered *both* by Rails and for the front-end in Mustache.
#
# ```haml
# - task.if 'assignee' do
# %label.subtle 'assigned to'
# = task.assignee_name
#
# - task.unless 'assignee' do
# %label.subtle 'not assigned'
# ```
#
def if(property, &block)
internal_markup = capture block
if js?
"{{##{property}}}" + \
internal_markup
"{{/#{property}}}"
else
if model.send(property).present?
internal_markup
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment