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

Dynamic output from one template #16

Open
paintedbicycle opened this issue Apr 13, 2014 · 20 comments
Open

Dynamic output from one template #16

paintedbicycle opened this issue Apr 13, 2014 · 20 comments

Comments

@paintedbicycle
Copy link

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
Copy link
Owner

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
Copy link
Author

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

@patrickkettner
Copy link
Owner

Thank you

On Thursday, May 8, 2014, Paul Wright [email protected] wrote:

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


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

patrick

@patrickkettner
Copy link
Owner

ping @paintedbicycle

@paintedbicycle
Copy link
Author

My bad. On it.

@patrickkettner
Copy link
Owner

ain't no thang :D

@paintedbicycle
Copy link
Author

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
Copy link
Contributor

Was there a solution for this?

@Sneagan
Copy link

Sneagan commented Sep 12, 2014

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

@patrickkettner
Copy link
Owner

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
Copy link

Sneagan commented Sep 24, 2014

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
Copy link
Owner

thanks @Sneagan!

@paintedbicycle
Copy link
Author

Happy to hear there is some interest in this!

The original idea was this:

  • want to be able to make a "page" called, say, "Blog Page"
  • want to be able to make several posts, say "Blog Post 1", "Blog Post 2" in full ("Single") format
  • want to be able to pull those blog posts onto the Blog page in a list

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

  • content for the blog page (page name, page content)
  • content for the blog posts (post name, post content)
  • ability to excerpt the posts (post name, post excerpt)

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
Copy link

Sneagan commented Sep 29, 2014

Thanks for the added clarity, Paul!

@patrickkettner
Copy link
Owner

@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
Copy link
Author

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
Copy link
Owner

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
Copy link
Owner

ping @paintedbicycle

@paintedbicycle
Copy link
Author

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
Copy link
Author

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.

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

4 participants