Comparing version 1.0.3 to 1.1.3
@@ -141,3 +141,3 @@ const RazorError = require('./RazorError'); | ||
sectionsAlreadyRendered(sectionName, renderedBy, attemptedBy) { | ||
var message = `Sections named '${sectionName}' have already been rendered by '${renderedBy}'. There is an atempt to rendered it again by '${attemptedBy}'.`; | ||
var message = `Sections named '${sectionName}' has already been rendered by '${renderedBy}'. There is an atempt to rendered it again by '${attemptedBy}'.`; | ||
return RazorError.new({ message, info: this.info, capture: this.sectionsAlreadyRendered }); | ||
@@ -144,0 +144,0 @@ } |
'use strict'; | ||
require('./utils'); | ||
function compilePageSync(html, model, viewData, isDebugMode) { | ||
function compilePageSync(html, model, viewData, context, isDebugMode) { | ||
if (isDebugMode) { | ||
@@ -11,2 +11,3 @@ let sandbox = html._sandbox; | ||
defineConstant(sandbox, "ViewData", viewData); | ||
defineConstant(sandbox, "Context", context); | ||
defineConstant(sandbox, "debug", isDebugMode); | ||
@@ -19,4 +20,6 @@ vm.runInNewContext(html._js, sandbox); | ||
const ViewData = viewData; | ||
const Context = context; | ||
const debug = isDebugMode; | ||
eval(html._js); | ||
html = model = viewData = context = isDebugMode = undefined; | ||
eval(Html._js); | ||
} | ||
@@ -32,5 +35,5 @@ | ||
function compilePage(html, model, viewData, isDebugMode, done) { | ||
function compilePage(html, model, viewData, context, isDebugMode, done) { | ||
try { | ||
compilePageSync(html, model, viewData, isDebugMode); | ||
compilePageSync(html, model, viewData, context, isDebugMode); | ||
return html.__renderLayout(done); | ||
@@ -99,2 +102,3 @@ } | ||
let compileOpt = { | ||
context: args.context, | ||
template: result.data, | ||
@@ -196,2 +200,3 @@ filePath: result.filePath, | ||
let compileOpt = { | ||
context: args.context, | ||
model: viewModel || args.model, // if is not set explicitly, set default (parent) model | ||
@@ -340,3 +345,3 @@ findPartial: args.findPartial, | ||
compilePage(htmlObj, this.args.model, this.args.viewData, debugMode, (err, html) => { | ||
compilePage(htmlObj, this.args.model, this.args.viewData, this.args.context, debugMode, (err, html) => { | ||
if (err) | ||
@@ -367,3 +372,3 @@ return error(err, htmlObj.__dbg); | ||
var html = this.getHtml(htmlArgs); | ||
compilePageSync(html, this.args.model, this.args.viewData, debugMode); | ||
compilePageSync(html, this.args.model, this.args.viewData, this.args.context, debugMode); | ||
this.checkSections(); | ||
@@ -370,0 +375,0 @@ } |
@@ -29,5 +29,6 @@ require('./utils'); | ||
module.exports = class Razor { | ||
constructor(options) { | ||
constructor(options, razorOpts) { | ||
this.options = options; | ||
this.ext = options.settings['view engine'] || options.ext; | ||
this.ext = options.settings['view engine'] || razorOpts.ext; | ||
this.context = razorOpts.context; | ||
this.env = options.settings.env; | ||
@@ -64,2 +65,3 @@ const debug = dbg.isDebugMode(this.env); | ||
let parserArgs = { | ||
context: this.context, | ||
filePath: filepath, | ||
@@ -66,0 +68,0 @@ template: viewStartsJsHtml + jsHtml, |
61
index.js
'use strict'; | ||
const dbg = require('./core/dbg/debugger'); | ||
const dbg = require('./core/dbg/debugger'); | ||
const Razor = require('./core/Razor'); | ||
var razor, parser, _ext = "raz"; | ||
const DefaultContext = require('./core/RazorContext'); | ||
var razor, parser, _requestContext; | ||
var isSetUp = false; | ||
var _settings = { ext: 'raz', context: DefaultContext }; | ||
module.exports = { | ||
// https://expressjs.com/en/guide/using-template-engines.html | ||
// https://www.npmjs.com/package/hbs | ||
__express: renderFile, | ||
renderFile: renderFile, | ||
setup, | ||
initContext, | ||
renderFile, | ||
render: getParser().compileSync, | ||
register: registerRazorEngine, | ||
handleErrors: handleErrors, | ||
handleErrors, | ||
debug: isDebugMode() | ||
}; | ||
} | ||
function setup(app, settings) { | ||
if (isSetUp) | ||
throw new Error('The RAZ app has been setup already.'); | ||
_settings = Object.assign(_settings, settings); | ||
if (_settings.context) | ||
initContext(app, _settings.context); | ||
if (_settings.register || _settings.ext !== "raz") | ||
register(app, _settings.ext) | ||
} | ||
function register(app, ext) { | ||
app.engine(ext, renderFile); | ||
app.set('view engine', ext); | ||
} | ||
function renderFile(filepath, options, done) { | ||
options.ext = _ext; | ||
const razorOpts = { ext: _settings.ext, context: _requestContext } | ||
if (!razor) | ||
razor = new Razor(options); | ||
razor = new Razor(options, razorOpts); | ||
@@ -27,2 +47,3 @@ razor.renderFile(filepath, done); | ||
function getParser() { | ||
@@ -36,10 +57,3 @@ if (!parser) { | ||
function registerRazorEngine(app, ext = _ext) { | ||
_ext = ext; | ||
app.engine(_ext, renderFile); | ||
app.set('view engine', _ext); | ||
} | ||
function handleErrors(app, errorCode) { | ||
app.use(appErrorHandler); | ||
@@ -62,8 +76,17 @@ | ||
function isDebugMode(){ | ||
function isDebugMode() { | ||
return dbg.isDebugMode(getEnv()); | ||
} | ||
function getEnv(){ | ||
function getEnv() { | ||
return process && process.env.NODE_ENV; | ||
} | ||
function initContext(app, context) { | ||
const Context = context; | ||
app.use((req, res, next) => { | ||
_requestContext = new Context(req); | ||
next(); | ||
}); | ||
} |
{ | ||
"name": "raz", | ||
"description": "Razor-like Template Engine for NodeJS Express library based on ASP.NET MVC Razor syntax. Template your views by mixing HTML markup with JavaScript server-side code!", | ||
"version": "1.0.3", | ||
"version": "1.1.3", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Sergey F.", |
@@ -187,3 +187,3 @@ # RAZ: Razor-Express view template engine for NodeJS | ||
// This code is meant for Node.js | ||
const razor = require("raz"); | ||
const razor = require("raz")(); | ||
var html = razor.render({ model, template }); | ||
@@ -226,7 +226,9 @@ ``` | ||
const app = require('express')(); | ||
// Register 'Razor-Express' template engine. | ||
// 'Express' will automatically find the Razor-Express module (within the `node-modules` folder) using this extension. | ||
// Register 'Razor' template engine and the default extesnion for the view-template files. | ||
// 'Express' will automatically find the Razor module (within the `node-modules` folder) using this extension. | ||
// If you decide to skip registering the engine then you will have to explicitly specify the file extension in the route handler. | ||
app.set('view engine', "raz"); | ||
// There is an alternative way to register Razor-Express engine (see more in Razor-Express API docs): | ||
//const raz = require('raz'); | ||
//razor.setup(app, { register: true }); | ||
@@ -233,0 +235,0 @@ // Create the route for the "Index.raz" view-template. |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
411
5
223572
18
4538