Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handlebars partial vs. render vs. template

The Fire Up Ember.js screencast uses partial, template, render to render templates within templates, but I'm still not sure I know which one to use when.

In the same screencast the partial is explained as using the context and all data relative to the current controller, while render uses the the specified controller's matching template, context etc:

Fire Up Ember.js: partial versus Fire Up Ember.js

Can someone please clarify the differences between partial, template, render and when (examples) to use which?

like image 224
Marius Butuc Avatar asked Feb 04 '13 22:02

Marius Butuc


People also ask

What is a partial In handlebars?

Handlebars allows for template reuse through partials. Partials are normal Handlebars templates that may be called directly by other templates.

What is handlebars template?

What is Handlebars? Handlebars is a simple templating language. It uses a template and an input object to generate HTML or other text formats. Handlebars templates look like regular text with embedded Handlebars expressions.


2 Answers

The way I understand it, the way they break down is like this:

"render" gives you an entire view/controller/template context of its own to work with.

An example will be a top navigation that includes dynamic pieces. The content will be maintained within a TopNavController and inserted into the application template using "render"

"partial" will insert a template, but that template will be using the current context instead of its own. Partial is also a newer part of the framework, meant to replace using template to some extent.

An example would be showing a list of users and having each user be a relatively complicated piece in the list (avatar, name, email, etc) you can just loop through the list and insert the partial based in the context of each user.

"template" just inserts the template using the current context. I believe it's not good style though to use template to render pieces inside of a template, you should rather use "partial" although template will work the same way for most cases.

like image 160
Andre Malan Avatar answered Sep 21 '22 16:09

Andre Malan


This chart given in ember's website gives a good comparison between render, partial and view.

Here is a snippet image of the comparison given in the website:

enter image description here

like image 39
user2431285 Avatar answered Sep 21 '22 16:09

user2431285