-
-
Save andymantell/1bd1e0fb5f8d659bf11f to your computer and use it in GitHub Desktop.
| var assemble = require('assemble'); | |
| var ext = require('gulp-extname'); | |
| module.exports = function(grunt) { | |
| grunt.registerTask('assemble', 'Assemble', function() { | |
| var done = this.async(); | |
| var site = assemble.init(); | |
| site.partials('src/core/templates/partials/**/*.hbs'); | |
| site.layouts('src/core/templates/layouts/*.hbs'); | |
| site.option('layout', 'default'); | |
| // Dummy helpers to stop errors being thrown for the moment | |
| site.helper('author', function() {return 'here'}); | |
| site.helper('button', function() {return 'here'}); | |
| site.helper('byline', function() {return 'here'}); | |
| site.helper('compile', function() {return 'here'}); | |
| site.helper('debug', function() {return 'here'}); | |
| site.helper('icon', function() {return 'here'}); | |
| site.helper('ifCond', function() {return true}); | |
| site.helper('image', function() {return 'here'}); | |
| site.helper('loadArticle', function() {return []}); | |
| site.helper('loadAuthorArticles', function() {return []}); | |
| site.helper('navigation', function() {return 'here'}); | |
| site.helper('panel', function() {return 'here'}); | |
| site.helper('parseJSON', function() {return []}); | |
| site.helper('render', function() {return 'here'}); | |
| site.helper('renderJSON', function() {return 'here'}); | |
| site.helper('teamFiller', function() {return ''}); | |
| // Build the pattern library | |
| site.task('demo', function() { | |
| site | |
| .src('src/sites/demo/pages/**/*.hbs') | |
| .pipe(ext()) | |
| .pipe(site.dest('converted-html/demo')); | |
| }); | |
| // Build the main site | |
| site.task('main', function() { | |
| site | |
| .src('src/sites/main/pages/**/*.hbs') | |
| .pipe(ext()) | |
| .pipe(site.dest('converted-html/main')); | |
| }); | |
| site.run(['demo', 'main'], done); | |
| }); | |
| }; |
Lol... I had started to write up a response on using assemble directly, then noticed that you are, but just inside a grunt task.
When editing the code, I noticed that you're doing assemble.dest() instead of site.dest.
Also, in the latest code, we removed the automatic renaming of destination extensions, so use gulp-extname between src and dest:
var ext = require('gulp-extname');
site.src('src/sites/main/pages/**/*.hbs')
.pipe(ext())
.pipe(site.dest('converted-html/demo'));You should also set the default layout: site.option('layout', 'default'); or whatever yours is.
Hope this helps.
Also, I wouldn't rely on doneCallback always working, even though that's a nice hack 😄
It's better to do assemble.run(['demo', 'main'], this.async());
@doowb Thanks! I've updated it a little bit. It's renaming to .html now, but no actual rendering of handlebars is occuring. I feel like I'm missing something fundamental. Is this even the right way of integrating it with Grunt?
Also, using site.dest() instead of assemble.dest() gives me a Fatal error: Object #<Object> has no method 'call' error...
What I mean to say is, that when I view the output files, there's raw naked handlebars frolicking inside. Not html...
The reason it's not rendering is because you're using a different instance of assemble in dest so it can't find the template files and just pushes the raw vinyl file through.
I'm not sure exactly why you're getting that error without more context around it. What do your templates look like? Do you have any helpers or data?
BTW... I don't get emails from gists 😦 That's something I think github needs to fix.
I've got several custom helpers, and custom data being piped in, the assemble 0.4 version of my site has been growing in complexity quite quickly.
I've not actually registered the helpers yet though! That could be the problem!? When I change it to site.dest(), it tries to render and fails because I haven't registered them! I'll poke around a bit further and see if I can make some headway...
Helpers will probably be the biggest migration piece. You can register a helper just by doing... site.helper('my-helper', function () { ... });
Or you can register a folder with glob patterns site.helpers('src/helpers/*.js') and it'll load them by their filename. We don't use the .register signature any more.
Another change inside helpers is that this is specially bound so you can get access to better information than before:
function () {
this.app; // the current instance of assemble
this.context; // the current context built up for that template based on `mergeContext` rules.
this.options; // the current options from `assemble.options`
}Check out template for more information on these. We're still working on the documentation for a lot of this.
Ok so there's a bit of migrating to do here... Are they still handlebars helpers though? So they're the same as before, I just need to refactor them to follow the new patterns?
Thanks for your help anyway, appreciate that this is all still slightly bleeding edge. My hand is forced slightly by the need to be using Handlebars 2.0 due to a bug in 1.3 that assemble 0.4 uses.
Yes, if you're using Handlebars (default) then write the helpers for Handlebars. If you take a look at template and how template types are created, some of your helpers might be able to be replaced. If you want to post some of your helpers, I could probably point out if they could be replaced or need to be migrated.
Ok, I've temporarily boshed in a bunch of dummy helpers just to suppress the errors like this:
site.helper('author', function() {return 'here'});
So now, handlebars does indeed seem to be kicking in and doing it's thing, but the outputted html is not being wrapped in the default layout. I updated the main insertion point to {% body %} and I've got these two lines in there:
site.layouts('src/core/templates/layouts/*.hbs');
site.option('layout', 'default');
Am I missing something else? I've posted my default.hbs here: https://gist.github.com/andymantell/1bd1e0fb5f8d659bf11f#file-default-hbs
Are they still handlebars helpers though?
like @doowb mentioned, yes. but... we used to make you jump through hoops to register them, now you can just register a regular javascript function as a helper. e.g:
site.helper('lower', function(str) {
return str.toLowerCase();
});but the outputted html is not being wrapped in the default layout.
I think this is the issue I discussed with @doowb a day or two ago. It's not you! I think it's a bug
Thanks Jon, that's good to know. What's the prognosis? It's obviously a fairly critical one that would block me from upgrading to 0.6.
Should I bail out, and return to 0.4? I'm totally expecting there to be various issues with 0.6 which I'm happy to ride out and help you diagnose etc but at the same time I have to deliver to my client so if 0.6 is not quite ready yet it would be great to get an idea of roughly how it's currently looking. Thanks!
@anymantell will you put console.log('layouts', site.views.layouts) on line 13?
It's really hard to debug this when all we have are small pieces. I haven't been having problems with layouts so I need to find out if somehow I have a different version of something that's working only for me.
Yeah, sorry, I realise it's really hard to debug this kind of stuff remotely. Here's the output of site.views.layouts from line 13 anyway:
One thing I noticed is that in all of your sub-layouts, you're using layout: default.hbs... it should just be layout: default. If you're doing this inside your pages, then it won't be pointing to the correct layout since page frontmatter overrides the global options e.g. site.option('layout', 'default');
Another thing that I had to do was return the streams inside the assemble tasks so the run waited for them to finish before telling grunt that it was done.
I created this repo with some samples based on what you described and everything seems to be working for me with those changes.
Hooray! That's pretty much sort of working now :-) It's all kinds of broken still, but that's to be expected - I still need to migrate all my helpers and I daresay fix a few other things but this is a darn good start - it gets about halfway through rendering now and the output all looks correctish and well formed. So, now I just need to finish the migration...
Thankyou so much for your help, is massively appreciated. I'll keep you posted on my progress... :-)
I've made a start on a custom grunt task as shown above, but all it does is copy all of the .hbs files to the output directory, seemingly making no attempt to render them to html. I've obviously missed something or misunderstood somewhere, but not sure where just yet. Any thoughts?