-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feature/#130 twig tpl engine
- Loading branch information
Showing
39 changed files
with
1,156 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ Thumbs.db | |
_.* | ||
.project | ||
.idea | ||
/*.iml | ||
.vscode | ||
.cache | ||
.settings | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* This is a wrapper for the Twig.js engine. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const Twig = require('twig'); | ||
const config = require('config'); | ||
|
||
const files = {}; | ||
const coreHelpersDir = `${config.get('nitro.basePath')}app/templating/twig/helpers/`; | ||
const projectHelpersDir = `${config.get('nitro.basePath')}project/helpers/`; | ||
const coreFiles = fs.readdirSync(coreHelpersDir); | ||
const projectFiles = fs.readdirSync(projectHelpersDir); | ||
|
||
coreFiles.map((file) => { | ||
if (path.extname(file) === '.js') { | ||
files[path.basename(file, '.js')] = coreHelpersDir + file; | ||
} | ||
}); | ||
|
||
projectFiles.map((file) => { | ||
if (path.extname(file) === '.js') { | ||
files[path.basename(file, '.js')] = projectHelpersDir + file; | ||
} | ||
}); | ||
|
||
Object.keys(files).forEach((key) => { | ||
const helperTagFactory = require(files[key]); | ||
|
||
// expose helper as custom tag | ||
Twig.extend(function(Twig) { | ||
Twig.exports.extendTag(helperTagFactory(Twig)); | ||
}); | ||
}); | ||
|
||
Twig.renderWithLayout = function(path, options, fn) { | ||
const layoutPath = options.settings.views + '/' + options.layout + '.' + options.settings['view engine']; | ||
|
||
function layoutRendered(error, layout) { | ||
function bodyRendered(error, body) { | ||
layout = layout.replace('<!-- Replace With Body -->', body); | ||
return fn(null, layout); | ||
} | ||
return Twig.__express(path, options, bodyRendered); | ||
} | ||
return Twig.__express(layoutPath, options, layoutRendered); | ||
}; | ||
|
||
module.exports = Twig; |
78 changes: 78 additions & 0 deletions
78
generators/app/templates/app/templating/twig/helpers/partial.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
'use strict'; | ||
|
||
/** | ||
* twig helper: {% partial Partial Name %} | ||
* | ||
* Usage | ||
* {% partial 'head' %} | ||
* | ||
*/ | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const config = require('config'); | ||
const twigUtils = require('../utils'); | ||
|
||
module.exports = function (Twig) { | ||
return { | ||
type: 'partial', | ||
regex: /^partial\s+('\S*')$/, | ||
next: [], | ||
open: true, | ||
compile: function(token) { | ||
|
||
token.name = Twig.expression.compile.apply(this, [{ | ||
type: Twig.expression.type.expression, | ||
value: token.match[1].trim() | ||
}]).stack; | ||
|
||
delete token.match; | ||
return token; | ||
}, | ||
parse: function(token, context, chain) { | ||
try { | ||
const partial = Twig.expression.parse.apply(this, [token.name, context]); | ||
let innerContext = Twig.ChildContext(context); | ||
let template; | ||
let templateFile = `${partial}.${config.get('nitro.viewFileExtension')}`; | ||
|
||
const templateFilePath = path.join( | ||
config.get('nitro.basePath'), | ||
config.get('nitro.viewPartialsDirectory'), | ||
templateFile | ||
); | ||
|
||
// TODO CHECK WHAT THIS IF SHOULD DO | ||
if (partial instanceof Twig.Template) { | ||
template = name; | ||
} else if (fs.existsSync(templateFilePath)) { | ||
// Import file | ||
template = Twig.Templates.loadRemote(templateFilePath, { | ||
method: 'fs', | ||
base: '', | ||
async: false, | ||
options: this.options, | ||
id: templateFilePath | ||
}); | ||
} else { | ||
return { | ||
chain: chain, | ||
output: twigUtils.logAndRenderError( | ||
new Error(`Partial ${templateFilePath} not found.`) | ||
) | ||
}; | ||
} | ||
|
||
return { | ||
chain: chain, | ||
output: template.render(innerContext) | ||
}; | ||
} catch (e) { | ||
return { | ||
chain: chain, | ||
output: twigUtils.logAndRenderError(e) | ||
}; | ||
} | ||
} | ||
}; | ||
}; |
Oops, something went wrong.