-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
How can I get all partials includes of a template? #682
Comments
TL;DR use method
As mentioned in the README.md:
For nodejs I use const { readFileSync, readdirSync } = require('fs')
const { resolve } = require('path')
const Mustache = require('mustache')
const getTemplate = path => {
try{
return readFileSync(path).toString()
} catch(e) {
console.warn(e.message)
// console.trace()
return ''
}
}
const getPartialTpl = partialName => getTemplate(resolve(partialsRoot, partialName + '.freemarker.html'))
const partialsFromTemplatesRecursiveReducer = (partials, template) => {
const templateToken = Mustache.parse(template)
const filterPartials = token => token[0] === '>'
const mapTokenName = token => token[1]
const filterUnique = (element, index, array) => array.indexOf(element) === index
const filterNew = partialName => !Object.keys(partials).includes(partialName)
const mapPartialTpl = partialName => [partialName, getPartialTpl(partialName)]
const newPartialNames = templateToken
.filter(filterPartials)
.map(mapTokenName)
.filter(filterUnique)
.filter(filterNew)
const newPartials = newPartialNames
.map(mapPartialTpl)
Object.assign(partials, Object.fromEntries(newPartials))
const newTemplates = newPartials.map(entry => entry[1]).filter(Boolean)
return Object.assign(partials, newTemplates.reduce(partialsFromTemplatesRecursiveReducer, partials))
}
const shouldAllPartialsBeLoaded = true
const partials = shouldAllPartialsBeLoaded
// simply load all partials (loadtime, Memory ?)
? Object.fromEntries(readdirSync(partialsRoot)
.map(file => file.replace(/.html$/,''))
.map(name => [name, getPartialTpl(name)]))
// or use Mustache.parse() to only load used partials
: [masterTemplate, pageTemplate].reduce(partialsFromTemplatesRecursiveReducer, {})
const pageContent = Mustache.render(pageTemplate, data, partials) Note: the expensive part in the reducer is the The effort to figure out the used partials in the (root-)template(s) competes with simply loading all partials in front. The question you have to answer is " In my case, I'm bulk-rendering a huge number of templates for various languages and varying layouts (same templates/partials, different data) with the same instance of Mustache, finally storing all rendered results as templates for another template engine (e.g. Apache freemarker or Apache velocity). Until now I had no real reason to not load all partials (in my case). |
Question:
How can I get all partials includes of a template? the includes not its contents. For example: for any partial include: {{>aTemplate}} i should get aTemplate
New Feature:
If it is not posible, one feature can be a function that return all partial includes as an array of string.
Why:
To fetch, by ajax requests, only the needed partials of a template.
The text was updated successfully, but these errors were encountered: