Comparing version 0.0.6 to 0.0.7
{ | ||
"name": "raz", | ||
"description": "Razor-like syntax for templating views in Express framework by mixing HTML with JavaScript.", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"author": { | ||
@@ -14,4 +14,6 @@ "name": "Sergey", | ||
"engine", | ||
"template-engine", | ||
"mvc", | ||
"parser", | ||
"view", | ||
"template", | ||
@@ -18,0 +20,0 @@ "express" |
# Razor-Express View Template Engine | ||
**(it's still draft, don't use it)** | ||
**(draft)** | ||
@@ -91,5 +91,77 @@ | ||
``` | ||
And here is the [playground](https://runkit.com/develax/5bf574e98b71430012d4e641) of this example. | ||
Here is the [playground](https://runkit.com/develax/5bf574e98b71430012d4e641) of this example. | ||
Node.js + Express example | ||
--- | ||
All the code is in the **server.js** file. | ||
```js | ||
// Create Express web server app. | ||
const app = require('express')(); | ||
// 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"); | ||
// Create the route for the "Index.raz" view-template. | ||
// Note that we do not specify the file extension explicitly in this route because we already did it when registering the engine. | ||
app.get('/', (req, res) => { | ||
const model = { | ||
title: "Names of the Days of the Week", | ||
days: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] | ||
}; | ||
res.render("./index", model); | ||
}); | ||
// Express-app default port number. | ||
const port = process.env.PORT || 8080; | ||
// Starting Express-app. | ||
const server = app.listen(port, () => { | ||
console.log("Server is up on port " + port); | ||
}); | ||
// Catch Express-app errors. | ||
server.on('error', function (e) { | ||
if (e.code === 'EADDRINUSE') { | ||
console.error('Address is in use, stopped.'); | ||
} | ||
}); | ||
``` | ||
The **index.raz** view-template is pretty much the same as in the previous example except we have to add some basic HTML markup. Notice that the file has **'.raz'** extension which every Razor view template file must have. | ||
```HTML+Razor | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>@Model.title</title> | ||
</head> | ||
<body> | ||
<h2>@Model.title</h2> | ||
<ul> | ||
@for(var i = 0; i < Model.days.length; i++){ | ||
var day = Model.days[i]; | ||
<li>@day</li> | ||
} | ||
</ul> | ||
</body> | ||
</html> | ||
``` | ||
Now if you **run server.js** and open http://localhost:8080/ URL in the browser you will see the HTML page showing something like this: | ||
___ | ||
> ### Names of the Days of the Week | ||
> * Sunday | ||
> * Monday | ||
> * Tuesday | ||
> * Wednesday | ||
> * Thursday | ||
> * Friday | ||
> * Saturday | ||
____ | ||
:sparkles: *The Express server app with Razor template engine works!* :sparkles: | ||
The source code of this example is available in [RazorExpressExample](https://github.com/DevelAx/RazorExpressExample) repository. | ||
---------------------- | ||
@@ -96,0 +168,0 @@ DRAFT: |
145264
176