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

References between content files #46

Open
dotloadmovie opened this issue Apr 22, 2017 · 1 comment
Open

References between content files #46

dotloadmovie opened this issue Apr 22, 2017 · 1 comment

Comments

@dotloadmovie
Copy link

Hi @Gottwik - really loving this project, great work. I'm using it to build a simple static blog and it's working great, however I was looking for some guidance on a question that's arisen

For each post I add I'd like to have a sections of "related content" at the bottom of the page. To avoid duplication of content ideally I'd be able to reference other content files inside the same generator - something like

{
title: 'article 1',
content: 'lorem ipsum dolor sit amet',
related: [
    {
      item: '@@article2'
    },
    {
      item: '@@article3'
    }
  ]
}

Currently I can access global items but not siblings. Is there any way you recommend of doing this? (I see potential issues with circular references of course...)

@Gottwik
Copy link
Owner

Gottwik commented Apr 23, 2017

Thank you for a very good question, @dotloadmovie . I am glad you like enduro.js, I hope we can improve it together.

Yea, this can't be currently done easily, but I wish it was because it's a good idea ;-)

I created a sample for you: https://github.com/Gottwik/enduro_samples/tree/master/related_articles

The idea is to load the related articles using a custom handlebars helper. Veeery simple. Basically you just use enduro.api.flat.load('<path-to-your-file>') to fetch the context.

The template looks like this:

<h1>{{name}}</h1>
<p>{{text}}</p>
<h3>related articles:</h3>
<ul>
	{{#related_articles}}
		<li>{{name}}</li>
	{{/related_articles}}
</ul>

and the helper looks like this:

var Promise = require('bluebird')

enduro.templating_engine.registerHelper('related_articles', function (options) {

	// store the actual rendered html
	// alternativelly a each helper could be used
	var rendered_html = ''

	// store the promises to read related articles
	var related_article_promises = []

	// go throgh all relate articles and load them
	for (index in this.related_articles) {

		// store the related article
		var related_article = this.related_articles[index]

		// load the actual content of the article
		var related_article_promise = enduro.api.flat.load('/generators/article/' + related_article.related_article_id)
			.then((related_article_context) => {

				// generate the actual html code
				return options.fn(related_article_context)
			})
			.then((rendered_html_block) => {

				// add the html code together with the previous ones
				rendered_html += rendered_html_block
			})

		// add whole thing to a array of promises
		related_article_promises.push(related_article_promise)
	}

	// return whole html block upon reading all related articles
	return Promise.all(related_article_promises)
		.then(() => {
			return rendered_html
		})
})

Eeeh, it looks complicated, but it isn't, I promise!

Btw, the circular thing could be solved by a shallow globalizer: http://www.endurojs.com/docs/globalizer#shallow-globalizer

Let me know if this helps you, or if you had any other ideas on how to solve this.

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

2 participants