Comparing version 0.0.38 to 0.0.40
@@ -115,3 +115,3 @@ const RazorError = require('./RazorError'); | ||
sectionIsAlreadyDefined(sectionName, line, pos, viewFilePath) { | ||
var message = `The section '${sectionName}' at line ${line + 1} pos ${pos + 1} has been already defined in the file '${viewFilePath}'. You cannot assign the same name to different sections in the same file.`; | ||
var message = `Section '${sectionName}' at line ${line + 1} pos ${pos + 1} has been already defined in the file '${viewFilePath}'. You cannot assign the same name to different sections in the same file.`; | ||
return RazorError.new({ message, info: this.info, line, pos, len: sectionName.length, capture: this.sectionIsAlreadyDefined }); | ||
@@ -131,11 +131,16 @@ } | ||
sectionIsNotFound(sectionName, filePath) { | ||
var message = `The view '${filePath}' requires the section '${sectionName}' which cannot be found.`; | ||
var message = `View '${filePath}' requires the section '${sectionName}' which cannot be found.`; | ||
return RazorError.new({ message, info: this.info, capture: this.sectionIsNotFound }); | ||
} | ||
sectionBeenRendered(sectionName, renderedBy, attemptedBy) { | ||
var message = `The section '${sectionName}' has already been rendered by '${renderedBy}'. There is an atempt to rendered it again by '${attemptedBy}'.`; | ||
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}'.`; | ||
return RazorError.new({ message, info: this.info, capture: this.sectionBeenRendered }); | ||
} | ||
sectionNeverRendered(sectionName,) { | ||
var message = `Section '${sectionName}' has never been rendered. If a section exists it must be rendered.`; | ||
return RazorError.new({ message, info: this.info, capture: this.sectionBeenRendered }); | ||
} | ||
partialViewNotFound(partialView, searchedLocations) { | ||
@@ -142,0 +147,0 @@ let message = `The view "${this.info.filename}" cannot find the partial view "${partialView}".\nThe following locations were searched:\n${searchedLocations.join("\n")}`; |
@@ -72,4 +72,4 @@ 'use strict'; | ||
// Private | ||
let section = null; | ||
let sections = args.sections || {}; | ||
let sectionName = null; | ||
let sections = args.parsedSections; | ||
@@ -94,3 +94,2 @@ this.__val = function (i) { | ||
findPartialSync: args.findPartialSync, | ||
sections, | ||
parsedSections: args.parsedSections, | ||
@@ -104,7 +103,7 @@ partialsCache: args.partialsCache, | ||
this.__sec = function (name) { // section | ||
if (!section) | ||
section = name; | ||
else if (section === name) | ||
section = null; | ||
this.__sec = function (name) { // in section | ||
if (!sectionName) | ||
sectionName = name; | ||
else if (sectionName === name) | ||
sectionName = null; | ||
else | ||
@@ -118,7 +117,4 @@ throw new Error(`Unexpected section name = '${name}'.`); // Cannot be tested via user-inputs. | ||
if (section) { | ||
if (!sections[section]) | ||
sections[section] = { html: '' }; | ||
sections[section].html += val; | ||
if (sectionName) { | ||
sections[sectionName][args.filePath].html += val; | ||
} | ||
@@ -157,10 +153,19 @@ else { | ||
let sec = sections[name]; | ||
let secGroup = sections[name]; | ||
if (sec) { | ||
if (sec.renderedBy) | ||
throw args.er.sectionBeenRendered(name, sec.renderedBy, args.filePath); // TESTME: | ||
if (secGroup) { | ||
if (secGroup.renderedBy) | ||
throw args.er.sectionsAlreadyRendered(name, secGroup.renderedBy, args.filePath); // TESTME: | ||
sec.renderedBy = args.filePath; | ||
return new HtmlString(sec.html); | ||
let html = ''; | ||
for (var key in secGroup) { | ||
if (secGroup.hasOwnProperty(key)) { | ||
let sec = secGroup[key]; | ||
html += sec.html; | ||
} | ||
} | ||
secGroup.renderedBy = args.filePath; | ||
return new HtmlString(html); | ||
} | ||
@@ -314,4 +319,12 @@ else { | ||
if (err) | ||
return onError(err, this); | ||
return onError(err); | ||
try { | ||
if (this.args.root) | ||
this.checkSections(); | ||
} | ||
catch (exc) { | ||
return onError(exc); | ||
} | ||
return done(null, html); | ||
@@ -332,2 +345,5 @@ }); | ||
compilePageSync(html, this.args.model, this.args.viewData, isDebugMode(opts)); | ||
if (this.args.root) | ||
this.checkSections(); | ||
} | ||
@@ -379,2 +395,16 @@ catch (exc) { | ||
// Check if all sections have been rendered. | ||
checkSections() { | ||
let sections = this.args.parsedSections; | ||
for (var key in sections) { | ||
if (sections.hasOwnProperty(key)) { | ||
let sec = sections[key]; | ||
if (!sec.renderedBy) | ||
throw this.er.sectionNeverRendered(key); | ||
} | ||
} | ||
} | ||
parseHtml(blocks, outerWaitTag) { | ||
@@ -1137,9 +1167,14 @@ log.debug(); | ||
// check if the section name is unique .. | ||
let sectionId = sectionName + "#" + this.args.filePath; | ||
let section = this.args.parsedSections[sectionId]; | ||
let sections = this.args.parsedSections[sectionName]; | ||
if (section) | ||
throw this.er.sectionIsAlreadyDefined(sectionName, this.lineNum, sectionNamePos, this.args.filePath); // Tests: "Section 8". | ||
if (sections) { | ||
let section = sections[this.args.filePath] | ||
if (section) | ||
throw this.er.sectionIsAlreadyDefined(sectionName, this.lineNum, sectionNamePos, this.args.filePath); // Tests: "Section 8". | ||
} | ||
else { | ||
this.args.parsedSections[sectionName] = sections = {}; | ||
} | ||
this.args.parsedSections[sectionId] = { name: sectionName, filePath: this.args.filePath }; | ||
sections[this.args.filePath] = { name: sectionName, filePath: this.args.filePath, html: '' }; | ||
@@ -1359,4 +1394,8 @@ // skip all following whitespaces .. | ||
return { | ||
compile, | ||
compile: (args, done) => { | ||
args.root = true; | ||
return compile(args, done); | ||
}, | ||
compileSync: args => { | ||
args.root = true; | ||
return compileSync(args).html; | ||
@@ -1363,0 +1402,0 @@ } |
{ | ||
"name": "raz", | ||
"description": "Razor-like syntax for templating views in Express framework by mixing HTML with JavaScript.", | ||
"version": "0.0.38", | ||
"version": "0.0.40", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Sergey", |
@@ -12,2 +12,5 @@ # RAZ: Razor-Express view template engine | ||
- [What is Razor-Express](https://github.com/DevelAx/RazorExpress/blob/master/docs/overview.md#what-is-razor-express) | ||
- [**Razor-Express View API**](#razor-express-view-api) | ||
- [Analogues of ASP.NET MVC Razor HtmlHelper methods](#analogues-of-aspnet-mvc-razor-htmlhelper-methods) | ||
- [Examples of usage](#examples-of-usage) | ||
- [**Common pitfalls & remarks**](#warning-common-pitfalls) | ||
@@ -183,4 +186,62 @@ - [Missing semicolon](#missing-semicolon) | ||
----------------------- | ||
Razor-Express View API | ||
=== | ||
Analogues of ASP.NET MVC Razor HtmlHelper methods | ||
--- | ||
Razor-Express methods | ASP.NET MVC methods | ||
------------ | ------------- | ||
Html.layout | Layout | ||
Html.body | RenderBody | ||
Html.partial | Html.RenderPartial & Html.Partial | ||
Html.raw | Html.Raw | ||
Examples of usage | ||
=== | ||
Html.layout | ||
--- | ||
```HTML+RAZOR | ||
@{ | ||
Html.layout = "_layout"; | ||
} | ||
``` | ||
Html.body | ||
--- | ||
```HTML+RAZOR | ||
<!DOCTYPE html> | ||
<html> | ||
<head>...</head> | ||
<body> | ||
... | ||
@Html.body() | ||
... | ||
</body> | ||
</html> | ||
``` | ||
Html.partial | ||
--- | ||
```HTML+RAZOR | ||
<div> | ||
@Html.partial("_userForm") | ||
</div> | ||
``` | ||
or | ||
```HTML+RAZOR | ||
@if(Model.showUserForm){ | ||
Html.partial("_userForm"); | ||
} | ||
``` | ||
Html.raw | ||
--- | ||
```HTML+RAZOR | ||
@{ | ||
var boldText = "This is an example of <b>bold text</b>."; | ||
} | ||
<span>@Html.raw(boldText)</span> | ||
``` | ||
**While the documentation is under development, use [this repository](https://github.com/DevelAx/RazorExpressFullExample) for more comprehensive examples.** | ||
:warning: Common pitfalls | ||
@@ -187,0 +248,0 @@ === |
@@ -91,3 +91,3 @@ (function () { | ||
</div>`, | ||
error: `The section 'Scripts' at line 5 pos 14 has been already defined in the file 'Section 8'. You cannot assign the same name to different sections in the same file.` | ||
error: `Section 'Scripts' at line 5 pos 14 has been already defined in the file 'Section 8'. You cannot assign the same name to different sections in the same file.` | ||
}, | ||
@@ -94,0 +94,0 @@ { |
167534
3778
279