Skip to content

Commit

Permalink
Merge pull request #998 from dbauszus-glx/nonlanguage-templates
Browse files Browse the repository at this point in the history
Get nonlanguage [view] templates
  • Loading branch information
RobAndrewHurst authored Nov 13, 2023
2 parents db4b233 + a68c888 commit 346fedf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
42 changes: 27 additions & 15 deletions mod/utils/languageTemplates.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@
const getFrom = require('../provider/getFrom')

const getTemplate = require('../workspace/getTemplate')

const workspaceCache = require('../workspace/cache')

module.exports = async (params) => {

if (params.template === undefined) return;
if (params.template === undefined) return;

// Set english as default template language.
params.language ??= 'en'

const workspace = await workspaceCache()

if (!Object.hasOwn(workspace.templates, params.template)) {

// Set english as default template language.
params.language ??= 'en'
console.warn(`Template ${params.template} not found.`)
return params.template;
}

const workspace = await workspaceCache()
// NOT a language template
if (workspace.templates[params.template].src) {

if (!Object.hasOwn(workspace.templates, params.template)) {
const nonLanguage = await getTemplate(workspace.templates[params.template])

console.warn(`Template ${params.template} not found.`)
return params.template;
}
return nonLanguage.template
}

const allLanguages = workspace.templates[params.template]
const allLanguages = workspace.templates[params.template]

let template = Object.hasOwn(allLanguages, params.language)? allLanguages[params.language]: allLanguages.en;
let template = Object.hasOwn(allLanguages, params.language)
? allLanguages[params.language]
: allLanguages.en;

if (typeof template === 'string' && Object.hasOwn(getFrom, template.split(':')[0])) {
if (typeof template === 'string' && Object.hasOwn(getFrom, template.split(':')[0])) {

// Get template from method.
template = await getFrom[template.split(':')[0]](template)
}
// Get template from method.
template = await getFrom[template.split(':')[0]](template)
}

return template
return template
}
5 changes: 5 additions & 0 deletions mod/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ module.exports = async (req, res) => {

const template = await languageTemplates(params)

if (!template) {
res.status(400).send(`Template undefined`)
return;
}

const view = template.replace(/{{2}([A-Za-z][A-Za-z0-9]*)}{2}/g, matched => {

// regex matches {{ or }}
Expand Down

0 comments on commit 346fedf

Please sign in to comment.