开发者

Backbone.js with Eco Templates: How to include template within a template?

Is it possible to include a template within a template? Maybe something similar to the way ERB handles partials?

Rather than attempting to render nested models in a fashion like ERB, it's better to let Backbone.js take care of this.

Note, I am using coffeescript syntax:

Projects.IndexView

template: JST["backbone/templates/projects/index"]

addAll: () ->
    @options.projects.each(@addOne)

addOne: (project) ->
    view = new Worktimer.Views.Projects.ProjectView({model : project})
    @$("#projects-table").append(view.render().el)

render: ->
    $(@el).html(@template(projects: @options.projects.toJSON() ))
    @addAll()

the model Project has a nested collection called sessions:

Projects.ProjectView

template: JST["backbone/templates/projects/project"]

$(@el).html(@template(@model.toJSON() ))     
for s in @model.sessions.models
    v = new Worktimer.Views.ProjectSessions.ShowView(model: s)
    $(@el).find('.sessions').append(v.render().el)

ProjectSessions.ShowView

template: JST["backbone/templates/project_sessions/show"]

render: ->
    $(this.el).html(@template(@model.toJSON() ))

so, in the end we have nested templates like this:

  • 开发者_运维百科Projects Index
    • Project
      • Session
      • Session
      • Session
      • Session
    • Project
      • Session
    • Project
      • Session
      • Session


here a little helper I use for spine:

# 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>"


I don't think Eco supports this. It's intended more as a simple templating system like Mustache than as a full-fledged ERB replacement. On Rails, you'd probably render an Eco template and inject the output into an ERB or Haml template.

For Node.js development, you might want to take a look at CoffeeKup, which lets you do your templating in CoffeeScript and supports partials.


If you prefix the template with .erb, you can use the ERB processor.

Change yourfile.js.coffee to yourfile.js.coffee.erb, then you'll be able to add <%= %> tags to your CoffeeScript template.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜