Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for partials #13

Open
nurpax opened this issue May 22, 2014 · 10 comments
Open

Add support for partials #13

nurpax opened this issue May 22, 2014 · 10 comments

Comments

@nurpax
Copy link

nurpax commented May 22, 2014

It'd be great to have support for partial templates.

@nurpax
Copy link
Author

nurpax commented May 22, 2014

I saw there was #7 which asked for partials, but that was closed without actually adding partial support (right?). Issue #3 mentions someone had a work in progress patch that'd add partials (or dependencies to templates). I think this'd be something that should be supported out of the box without extra dependencies.

@kusmierz
Copy link
Collaborator

@nurpax
Copy link
Author

nurpax commented May 22, 2014

This works with only handlebars.runtime when minified with r.js? Using swag means yet another library dependency and +20 KB minimized download size.

@kusmierz
Copy link
Collaborator

Yes, it works, I'm using it with r.js. Not 20, but 13kB :)

Anyway. You can also use only partial feature, but I think swag has few another useful (everyday) helpers... So it worth to spend 13kB more, I think.

@nurpax
Copy link
Author

nurpax commented Aug 3, 2014

I actually found a really easy way to do partials without Swag. I'll try to put together a minimal example and send a PR against your README.

@kusmierz
Copy link
Collaborator

kusmierz commented Aug 3, 2014

would be cool, thanks in advance!

@chrisslater
Copy link

Was this issue ever resolved? I am using your implementation and am looking to add partial support myself.

@kusmierz
Copy link
Collaborator

kusmierz commented Dec 2, 2014

unfortunately only using https://github.com/elving/swag...

@nurpax
Copy link
Author

nurpax commented Dec 2, 2014

Yeah, like I said above, there's an easy way to do this without Swag. I just never sent my PR for the simple example as I was unable to get the hbs example project working and was unable to send a minimal PR against the example project. See #17.

You can pretty much just directly use Handlebars.registerPartial. Here's a use in a bigger project: https://github.com/nurpax/hswtrack/blob/3a336b95705d678ecc4209664085cff8e3d42177/static/app/stats.js. This project is nowadays rewritten in JSX but this old commit works fine.

define(['jquery', 'handlebars', 'underscore', 'app/view', 'app/model', 'hbs!templates/stats-main', 'hbs!templates/stats-history', 'hbs!templates/exercise-no-edit'], function($, Handlebars, _, view, model, templateStatsMain, templateStatsHistory, templateExercise) {
   ...

   Handlebars.registerPartial('exerciseNoEdit', templateExercise);

HTH.

@chrisslater
Copy link

Ok cool I see what you have done here. I was struggling to find how to precompile partials, but I see hbs can do it for me. So taking what you have, I have edited hbs-builder.js to allow me to define a partial in the require string.

'hbs!partial:path/to/templates'

and then modify the write output for precompiling requirejs.

# hbs-builder.js
var isPartial = (name.substr(0, 8) === 'partial:');
...
if (isPartial) {
  write(
    "define('hbs!" + name + "', ['handlebars'], function(Handlebars){ \n" +
      "Handlebars = Handlebars || this.Handlebars;\n" +
      "return Handlebars.registerPartial('" + name.substr(8) + "', Handlebars.template(" + compiled.toString() + ")); \n" +
    "});\n"
  );
} else {
  write(
    "define('hbs!" + name + "', ['handlebars'], function(Handlebars){ \n" +
      "Handlebars = Handlebars || this.Handlebars;\n" +
      "return Handlebars.template(" + compiled.toString() + ");\n" +
    "});\n"
  );
}
# hbs.js
...
if (isPartial) {
  onload(Handlebars.registerPartial(name, raw));
} else {
  // Just return the compiled template
  onload(Handlebars.compile(raw));
}

What are your initial thoughts?

There is definitely improvement to be made (for one the reputation of code for one) and registerPartial doesn't return anything so I may just return the template instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants