Skip to content

Instantly share code, notes, and snippets.

@twetzel
Created June 21, 2012 04:02
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save twetzel/2963752 to your computer and use it in GitHub Desktop.
Save twetzel/2963752 to your computer and use it in GitHub Desktop.
.eco - Partials .. Render Partials in .jst.eco templates (for Rails + Spine / maybe Backbone)
# Render Partials in ECO-Templates like in Rails-ERB
#
# usefull to clean up structure in spine.js and other js-mvc´s like backbone
#
# usage:
# <%- render_partial 'path/to/partial' %> .. will render ../spine-app/views/path/to/_partial.jst.eco
# <%- render_partial 'path/to/partial', foo: 'bar' %> .. will render ../spine-app/views/path/to/_partial.jst.eco .. locals = @foo
#
window.render_partial = ( path, options = {} ) ->
# add the leading underscore (like rails-partials)
path = path.split('/')
path[ path.length - 1 ] = '_' + path[ path.length - 1 ]
path = path.join('/')
# render and return the partial if existing
try
JST["app/views/#{ path }"]( options )
catch error
# if App.Environment != 'production' then "<p class='error'>Sorry, there is no partial named '#{ path }'.</p>" else ''
"<p class='error'>Sorry, there is no partial named '#{ path }'.</p>"
@kpvarma27
Copy link

+1 . This really works.
In case if you are using backbone-rails you might want to change the line 16 to
JST"#{ path }"

Copy link

ghost commented Oct 22, 2013

+1 Great job, ty!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment