Comparing version 0.0.11 to 0.0.12
@@ -136,3 +136,3 @@ const htmlEncode = require('js-htmlencode'); | ||
<h1> | ||
An error occurred during the compilation of a 'razor-syntax' page required to process this request. | ||
A template compilation error occured | ||
</h1> | ||
@@ -139,0 +139,0 @@ <div class="stack">${stack}</div> |
27
index.js
@@ -12,6 +12,8 @@ 'use strict'; | ||
compileSync: getParser().compileSync, | ||
register: registerRazorEngine | ||
register: registerRazorEngine, | ||
handleErrors: handleErrors | ||
}; | ||
const Razor = require('./core/Razor'); | ||
const RazorError = require('./core/errors/RazorError'); | ||
var razor, parser; | ||
@@ -38,2 +40,23 @@ | ||
app.set('view engine', ext); | ||
} | ||
} | ||
function handleErrors(app, errorCode){ | ||
app.use(appErrorHandler); | ||
function appErrorHandler(err, req, res, next) { | ||
if (res.headersSent) | ||
return next(err); // must | ||
var env = app.get('env'); | ||
if (env === "dev" && err instanceof RazorError) { | ||
var errorHtml = err.html(); | ||
res.status(errorCode || 500); | ||
res.send(errorHtml); | ||
return; | ||
} | ||
return next(err); | ||
} | ||
} | ||
{ | ||
"name": "raz", | ||
"description": "Razor-like syntax for templating views in Express framework by mixing HTML with JavaScript.", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Sergey", |
@@ -1,5 +0,21 @@ | ||
# Razor-Express View Template Engine | ||
# Razor-Express View Template Engine (raz) | ||
**(draft)** | ||
- [**Intro**](#intro) | ||
- [**Quick Start**](#quick-start) | ||
- [Node.js example](#nodejs-example) | ||
- [Express web-server example](#express-web-server-example) | ||
- [**Pitfalls**](#warning-common-pitfalls) | ||
- [Missing semicolon](#missing-semicolon) | ||
- [**Overview**](https://github.com/DevelAx/RazorExpress/blob/master/docs/overview.md) | ||
- [What is View Template](https://github.com/DevelAx/RazorExpress/blob/master/docs/overview.md#what-is-view-template) | ||
- [What is View Template Engine](https://github.com/DevelAx/RazorExpress/blob/master/docs/overview.md#what-is-view-template-engine) | ||
- [What is Razor-Express](https://github.com/DevelAx/RazorExpress/blob/master/docs/overview.md#what-is-razor-express) | ||
----------------------- | ||
Intro | ||
=== | ||
When I just started to dive into the world of *Node.js* after years of working with [ASP.NET MVC](https://docs.microsoft.com/en-us/aspnet/core/mvc/overview) I couldn't find any *view template engine* that was as convenient, elegant, concise, and syntactically close to native HTML markup language as [Razor](https://docs.microsoft.com/en-us/aspnet/core/mvc/views/layout) was. To be more precise, **ASP.NET MVC Razor markup is a hybrid of HTML markup and C# programming language**. And when it comes to code it's also syntactically close to the original C# language. So, this is exactly what I expected to see in the NodeJS world (except that it should be JavaScript instead of C#). | ||
@@ -11,2 +27,4 @@ | ||
----------------------- | ||
Quick Start | ||
@@ -86,3 +104,3 @@ === | ||
If you'd like to see all these parts working together here is the [playground](https://runkit.com/develax/5bf574e98b71430012d4e641) of it. | ||
If you'd like to see all these parts working together here is the [playground](https://runkit.com/develax/razor-quick-example) of it. | ||
@@ -158,7 +176,46 @@ Express web-server example | ||
:sparkles: *The Express server app with Razor template engine works!* :sparkles: | ||
*The Express server app with Razor template engine works!* :thumbsup: | ||
The source code of this example is available in [RazorExpressExample](https://github.com/DevelAx/RazorExpressExample) repository. | ||
----------------------- | ||
:warning: Common pitfalls | ||
=== | ||
Missing semicolon | ||
--- | ||
Some developers have a habit of not putting a semicolon at the end of JavaScript code lines. This is a personal matter of course, although not considered good form. Be that as it may, when writing view-templates for Razor-Express, a **semicolon at the end of JavaScript expressions is strictly required!** If you do not follow this requirement, there may be cases when Razor isn't able to understand your instructions and throws a pretty vague error. Let's take a look at this example. | ||
```JS | ||
//////////////////////////////////////////////// | ||
// Example of missing semicolon error in Razor-Express. | ||
// https://www.npmjs.com/package/raz | ||
//////////////////////////////////////////////// | ||
// Semicolon is missed at the end of the line of code "var text = i * i". | ||
const template = ` | ||
<ul> | ||
@for(var i = 0; i < 10; i++){ | ||
var text = i * i | ||
<li>@text</li> | ||
} | ||
</ul>`; | ||
const razor = require("raz") | ||
try{ | ||
var html = razor.compileSync({ template }); | ||
} | ||
catch(err){ | ||
console.log(err); | ||
} | ||
``` | ||
If you run this code you will get the error: | ||
> RazorError: **The code or section block is missing a closing "}" character.** Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup. The block starts at line 3 with text: "@for(var i = 0; i < 10; i++){" | ||
[Test this example with RunKit.](https://runkit.com/develax/razor-pitfalls-semicolon) | ||
-------------------- | ||
## To be continued soon.. | ||
> `layouts`, `partial views`, `sections` and `viewStarts` are implemented but haven't been documented yet. |
@@ -5,8 +5,8 @@ //'use strict'; | ||
args = args || {}; | ||
const RazorError = require('../core/errors/RazorError'); | ||
const path = require('path'); | ||
const app = require('express')(); | ||
const razor = require("../index"); | ||
razor.register(app); | ||
@@ -19,17 +19,2 @@ var viewsPath = path.join(__dirname, args.views || '/views'); | ||
// Parser call.. | ||
var html = razor.compileSync({ | ||
template: '<span>@(Model)</span>', | ||
model: "Hello World!" | ||
}); | ||
console.log(html); | ||
// var cwd = process.cwd(); | ||
// var tmp = require('os').tmpdir(); | ||
// console.log(cwd); | ||
// console.log(tmp); | ||
// app.use(function (req, res, next) { | ||
@@ -57,3 +42,2 @@ // res.locals.req = req; | ||
// app.get('/', (rq, rs) => { | ||
@@ -79,24 +63,3 @@ // rs.render("./sections/index", { header: "This is a HEADER", content: 'This is CONTENT.', footer: "This is FOOTER" }); | ||
app.use(appErrorHandler); | ||
function appErrorHandler(err, req, res, next) { | ||
if (res.headersSent) | ||
return next(err); // must | ||
var env = app.get('env'); | ||
if (env !== "production" && err instanceof RazorError) { | ||
var errorHtml = err.html(); | ||
res.status(500); | ||
res.send(errorHtml); | ||
} | ||
else { | ||
return next(err); | ||
} | ||
//res.status(500); | ||
//res.render('error', { error: err }); | ||
} | ||
razor.handleErrors(app); | ||
const port = process.env.PORT || 1337; | ||
@@ -103,0 +66,0 @@ |
148515
219
3388