patrickkettner / grunt-compile-handlebars

grunt plugin to compile static html from a handlebars plugin
MIT License
115 stars 29 forks source link

Dynamic output from one template #16

Open paintedbicycle opened 10 years ago

paintedbicycle commented 10 years ago

I'm looking for a way to have one of each type of content in my /src/ folder, but perhaps multiple versions in the /dist/ folder. Types could be 'events', 'blog posts', 'people', etc. I only need one version of the template for each of these and the .json file(s) would have the content.

Something like:

events: {
  preHTML: 'src/partials/header.handlebars',
  postHTML: 'src/partials/footer.handlebars',
  template: 'src/events/event.handlebars',
  templateData: 'src/events/*.json',
  output: 'dist/events/event-[data.EventName].html'
}

Where above, there is one event template and perhaps multiple .json files (one per event). But I would need a way to change the output title based on something in the .json file.

Ultimately, I'd prefer to only have one global .json file and then do something like event.eventName but there doesn't seem like a way to output multiple different files based on one template and json file. I thought about declaring each block separately in the gruntfile, but with one template and one json file, there is no way to tell the processor which block to read from the .json file.

Are either of these two approaches possible?

Paul

patrickkettner commented 10 years ago

Hey Paul, I am super sorry for not replying sooner, somehow I missed the notification :[[

Could you create a PR with a failing test that shows what you are expecting, and I will get it working for you :]

paintedbicycle commented 10 years ago

Sure! I'll look at this this weekend. Thanks

patrickkettner commented 10 years ago

Thank you

On Thursday, May 8, 2014, Paul Wright notifications@github.com wrote:

Sure! I'll look at this this weekend. Thanks

— Reply to this email directly or view it on GitHubhttps://github.com/patrickkettner/grunt-compile-handlebars/issues/16#issuecomment-42603932 .

patrick

patrickkettner commented 10 years ago

ping @paintedbicycle

paintedbicycle commented 10 years ago

My bad. On it.

patrickkettner commented 10 years ago

ain't no thang :D

paintedbicycle commented 10 years ago

Ok I have added a failing test. Take a look and see if you think the idea matches your script. No worries if not. Happy to talk through it more as well, especially when it comes to setup. Might be a better way to dynamically pass all children, etc.

ryanburgess commented 10 years ago

Was there a solution for this?

Sneagan commented 10 years ago

I'm trying to get this working as well. Did you guys ever make any headway?

patrickkettner commented 10 years ago

Sorry guys, I have been swamped at work. I would like to get this in asap, however.

Thought on this a bit, however, and I have a proposal I would love to run by the three of you

Basically, I think it makes the most sense to use handlebars templates on the filenames themselves, and then just process the file as we normally do.

events: {
  template: 'src/events/event.handlebars',
  templateData: 'src/events/*.json',
  output: 'dist/events/event-{{EventName}}.html'
}

The only issue I am hitting is that the task looks over templates, and since there is only one in this setup, I am not sure when to properly loop over the templateData to create the output files.

Unless anyone has a problem with this, or a better idea, this is what I will be moving forward with

Sneagan commented 10 years ago

Swamped here too! :) I wound up hacking up a solution. I'll see if I can get a PR together that works without some of the more questionable tactics I used.

patrickkettner commented 10 years ago

thanks @Sneagan!

paintedbicycle commented 10 years ago

Happy to hear there is some interest in this!

The original idea was this:

So, in the data file, you'd have:

Pretty standard format, but most grunt templating scripts I've worked with don't quite go that distance.

Anyway, that's where my original thoughts were.

Paul

Sneagan commented 10 years ago

Thanks for the added clarity, Paul!

patrickkettner commented 10 years ago

@paintedbicycle - correct me if I am wrong, but the only real thing stopping you from doing that is the output naming, right? Like, if you manually added every post in your config, it would work, just be horrible, ugly, slow and awful - right?

paintedbicycle commented 10 years ago

Yes, making each file separately would be possible, but a pain. Would be great to be able to loop through any number of "posts" in the json file.

Also, I'm not sure currently how I'd create a page from one part of the data file and then pull excerpts from a number of other pages onto that page as in the blog list option.

patrickkettner commented 10 years ago

Sorry, I don't think my question was clear.

The only thing that is preventing it is the file naming, correct? If that is the case, did you see my proposal on how to do that?

patrickkettner commented 9 years ago

ping @paintedbicycle

paintedbicycle commented 9 years ago

I think your proposal covers it - just having a hard time visualizing it. I see that the output file is dynamic, so it seems that you could have multiple "events" in the data.json file and it would loop through them. As long as you could do other things like including a header and footer and access other parts of the data file (for global things like site title) then I think we'd be set

paintedbicycle commented 9 years ago

I guess the closest example is your globbed examples with the dynamic filename, however I don't want to have separate JSON and separate input files. I'd like to loop through the JSON, using one input template and output multiple output templates with a name that comes from the JSON. So a foreach loop, going through the JSON and creating dynamic outputs with names I can control from the data.

Additionally, I'd like to be able to specify a "section" in the JSON during the build call (gruntfile.js) that makes the data start from that section. This would be helpful for the above - I could loop though the "posts" array, or the "events" array in the JSON file.

And it would be nice if the PRE and POST could use JSON data too, but that doesn't seem to be the case. I'd throw a header in there with page title, that I could get from the JSON.