Sha256: d3d317ad08763e8155c8e66c1c06ec8d7ff97d8d18efec0edef9f80c2bd3bce6

Contents?: true

Size: 1.51 KB

Versions: 4

Compression:

Stored size: 1.51 KB

Contents

Luca.templates ||= {}

if window.JST?
  for templateId, templateFn of JST when templateId.match(/^luca\/templates\//)
    Luca.templates[ templateId.replace('luca/templates/','') ] = templateFn
    _( JST ).delete( templateId )

# This is a convenience method for accessing the templates
# available to the client side app, either the ones which ship with Luca
# available in Luca.templates ( these take precedence ) or
# the app's own templates which are usually available in JST

# optionally, passing in variables will compile the template for you, instead
# of returning a reference to the function which you would then call yourself
Luca.template = (template_name, variables, partials)->
  window.JST ||= {}

  if _.isFunction(template_name)
    return template_name(variables)

  luca = Luca.templates?[ template_name ]
  jst = JST?[ template_name ]

  unless luca? or jst?
    needle = new RegExp("#{ template_name }$")

    luca = _( Luca.templates ).detect (fn,template_id)->
      needle.exec( template_id )

    jst = _( JST ).detect (fn,template_id)->
      needle.exec( template_id )

  throw "Could not find template named #{ template_name }" unless luca || jst

  template = luca || jst

  if template instanceof Hogan.template
    if variables?
      return template.render(variables, partials)

  return template(variables) if variables?

  template

Luca.available_templates = (filter="")->
  available = _( Luca.templates ).keys()

  if filter.length > 0
    _( available ).select (tmpl)-> tmpl.match(filter)
  else
    available

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
luca-0.9.91 app/assets/javascripts/luca/core/templates.coffee
luca-0.9.9 app/assets/javascripts/luca/core/templates.coffee
luca-0.9.899 app/assets/javascripts/luca/core/templates.coffee
luca-0.9.89 app/assets/javascripts/luca/core/templates.coffee