Created
January 24, 2013 22:03
-
-
Save jtrim/4628410 to your computer and use it in GitHub Desktop.
Possible way to express conditionals in dual purpose (rails + mustache) templates.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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