Created
April 7, 2012 14:24
-
-
Save machty/2329339 to your computer and use it in GitHub Desktop.
An attempt at Slim/Haml helpers for handlebars
This file contains 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
= handlebars_template_named "edit-field" do | |
= hb %{ if isEditing } do | |
= hb %{ view BD.TextField valueBinding="value" } | |
= hb_else | |
= hb %{ if value } do | |
= hbb %{ value } | |
= hb_else | |
span.no-name empty | |
/ Outputs | |
<script type="text/x-handlebars" data-template-name="edit-field> | |
{{#if isEditing}} | |
{{view BD.TextField valueBinding="value"}} | |
{{else}} | |
{{#if value}} | |
{{{value}}} | |
{{else}} | |
<span class="no-name">empty</span> | |
{{/if}} | |
{{/if}} | |
</script> |
This file contains 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
module SlimlHelper | |
# e.g. hb 'view bindAttr src="test.html" classBinding="isPriority"' | |
# produces: | |
# {{view bindAttr src="test.html" classBinding="isPriority"'}} | |
# | |
# hb 'view' do | |
# p Hello | |
# | |
# produces: | |
# {{#view}}<p>Hello</p>{{/view}} | |
def hb(hbstring, &block) | |
hb_priv(hbstring, true, &block) | |
end | |
# same as above, but with {{{ instead of {{ | |
def hbb(hbstring, &block) | |
hb_priv(hbstring, false, &block) | |
end | |
def hb_else | |
"{{else}}" | |
end | |
# usage: hb_link('Click Here', 'action "edit" on="click"') | |
def hb_link(content, hb) | |
result = '<a href="#" {{' + hb + '}}>' + content + '</a>' | |
result.html_safe | |
end | |
def handlebars_template(&block) | |
content_tag :script, type: "text/x-handlebars" do | |
capture(&block) | |
end | |
end | |
def handlebars_template_named(name, &block) | |
content_tag :script, type: "text/x-handlebars", "data-template-name" => name do | |
capture(&block) | |
end | |
end | |
private | |
def hb_priv(hbstring, escape, &block) | |
hbstring.strip! | |
first_tag = hbstring.split.first | |
hbstring.prepend('#') if block_given? # view vs. #view | |
open = escape ? "{{" : "{{{" | |
close = escape ? "}}" : "}}}" | |
output = "#{open}#{hbstring}#{close}" | |
if block_given? | |
output << capture(&block) | |
output << "#{open}/#{first_tag}#{close}" | |
end | |
output.html_safe | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment