Skip to content

Instantly share code, notes, and snippets.

@the-teacher
Created July 13, 2012 11:46
Show Gist options
  • Save the-teacher/3104483 to your computer and use it in GitHub Desktop.
Save the-teacher/3104483 to your computer and use it in GitHub Desktop.
Coffee Script TagHelper Template
$ ->
class @TagHelper
constructor: (test_mode = false) ->
if test_mode
console.log "h = new document.TagHelper"
console.log @cdata_section('<hello world>')
console.log @tag('br')
console.log @tag('br', null, true)
console.log @tag('br', null, false)
console.log @tag('br', {'id': 'test_br', 'data-test': 'some-data'}, false)
console.log @tag('br', {'id': 'test_br', 'data-test': 'some-data', 'title': 'me & you'}, false, true)
console.log @tag('br', {'id': 'test_br', 'data-test': 'some-data', 'title': 'me &amp; you'}, false, false)
console.log @content_tag('p', {'id': 'test_p', 'data-test': 'some-data', 'title': 'me &amp; you'}, 'hello world!', false)
console.log @link_to('github.com/the-teacher', 'https://github.com/the-teacher')
console.log @link_to('github.com/the-teacher', 'https://github.com/the-teacher', {class: 'ajaxian_link', id: 'uniq_link'})
console.log @link_to('github.com/the-teacher', 'https://github.com/the-teacher', {'id': 'test_a', 'data-test': 'some-data', 'title': 'me &amp; you'}, false)
console.log @hidden_field('some_name', null)
console.log @hidden_field('some_name', '12')
console.log @hidden_field('some_name', '12', {'id': 'test_input', 'data-test': 'some-data', 'title': 'me &amp; you'}, false)
console.log @text_field('some_name', null)
console.log @text_field('some_name', '12')
console.log @text_field('some_name', '12', {'id': 'test_input', 'data-test': 'some-data', 'title': 'me &amp; you', palceholder: 'hello world'}, false)
console.log @content_tag 'p', {id: 123}, =>
"""
Hello World!
#{@link_to('github.com/the-teacher', 'https://github.com/the-teacher')}
#{@link_to('google', 'https://google.com')}
It's a template of Paragraph
"""
, false
tag_helper = @
console.log @content_tag 'p', {}, (tag = tag_helper) ->
"""
Hello World!
#{tag.link_to('github.com/the-teacher', 'https://github.com/the-teacher')}
#{tag.link_to('google', 'https://google.com')}
It's a template of Paragraph
"""
, {id: 123}
, false
escapeHTML: (content) -> content.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;')
build_attributes: (options = null, escape = true) ->
attributes = ''
if options
for name, value of options
value = "#{value}"
value = @escapeHTML(value) if escape
attributes += """ #{name}='#{value}'"""
attributes
cdata_section: (content) -> """<![CDATA[#{content}]]>"""
tag: (name, options = null, open = false, escape = true) ->
closed = if open then '' else ' /'
attributes = @build_attributes(options, escape)
"""<#{name}#{attributes}#{closed}>"""
content_tag: (name, options = null, content, escape = true) ->
content = content() if typeof(content) is 'function'
attributes = @build_attributes(options, escape)
"""<#{name}#{attributes}>#{content}</#{name}>"""
# alias of *content_tag*
block: (name, options = null, content, escape = true) ->
@content_tag(name, options, content, escape)
link_to: (content, url = null, options = null, escape = true) ->
href = ''
href = " href='#{url}'" if url
attributes = @build_attributes(options, escape)
"""<a#{href}#{attributes}>#{content}</a>"""
input: (type = 'text', name, value = null, options = null, escape = true) ->
_value = ''
_value = " value='#{value}'" if value
attributes = @build_attributes(options, escape)
"""<input type=#{type}' name='#{name}'#{_value}#{attributes} />"""
hidden_field: (name, value = null, options = null, escape = true) ->
@input('hidden', name, value, options, escape)
text_field: (name, value = null, options = null, escape = true) ->
@input('text', name, value, options, escape)
@tag_helper = new @TagHelper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment