1 | # Express + Marko
|
2 |
|
3 | See the [marko-express](https://github.com/marko-js-samples/marko-express) sample
|
4 | project for a working example.
|
5 |
|
6 | ## Installation
|
7 |
|
8 | ```
|
9 | npm install express --save
|
10 | npm install marko --save
|
11 | ```
|
12 |
|
13 | ## Skip the view engine
|
14 |
|
15 | The built in view engine for express may be asynchronous, but it doesn't support streaming (check out [Rediscovering Progressive HTML Rendering](http://www.ebaytechblog.com/2014/12/08/async-fragments-rediscovering-progressive-html-rendering-with-marko/) to see why this is so important). So instead we'll [bypass the view engine](https://strongloop.com/strongblog/bypassing-express-view-rendering-for-speed-and-modularity/).
|
16 |
|
17 | ## Usage
|
18 |
|
19 | Marko provides a submodule (`marko/express`) to add a `res.marko` method to the express response object. This function works much like `res.render`, but doesn't impose the restrictions of the express view engine and allows you to take full advantage of Marko's streaming and modular approach to templates.
|
20 |
|
21 | By using `res.marko` you'll automatically have access to `req`, `res`, `app`, `app.locals`, and `res.locals` from within your Marko template and custom tags. These values are added to `out.global`.
|
22 |
|
23 | ```javascript
|
24 | require("marko/node-require"); // Allow Node.js to require and load `.marko` files
|
25 |
|
26 | var express = require("express");
|
27 | var markoExpress = require("marko/express");
|
28 | var template = require("./template");
|
29 |
|
30 | var app = express();
|
31 |
|
32 | app.use(markoExpress()); //enable res.marko(template, data)
|
33 |
|
34 | app.get("/", function(req, res) {
|
35 | res.marko(template, {
|
36 | name: "Frank",
|
37 | count: 30,
|
38 | colors: ["red", "green", "blue"]
|
39 | });
|
40 | });
|
41 |
|
42 | app.listen(8080);
|
43 | ```
|