1 | ## A Async Template for Node.js, Use ejs template language [![Build Status](https://travis-ci.org/DoubleSpout/ariestp.svg?branch=master)](https://travis-ci.org/DoubleSpout/ariestp)
|
2 |
|
3 | ##Install
|
4 |
|
5 | npm install ariestp
|
6 |
|
7 | ###SimpleExample
|
8 |
|
9 | "use strict";
|
10 | const aries = require("ariestp");
|
11 | const fs = require("fs");
|
12 | const http = require("http");
|
13 |
|
14 | const asyncTpl = `<%?
|
15 | ctx.thinking;
|
16 | setTimeout(function () {
|
17 | ctx.thinking = "1 seconde later"
|
18 | aries();
|
19 | }, 1000);
|
20 |
|
21 | %>
|
22 | <%- ctx.thinking %>
|
23 |
|
24 | <%?
|
25 | ctx.http.get('http://www.baidu.com/', (res) => {
|
26 | ctx.status = res.statusCode;
|
27 | res.resume();
|
28 | aries();
|
29 | }).on('error', (e) => {
|
30 | ctx.status = 500;
|
31 | aries();
|
32 | });
|
33 | %>
|
34 | <%- ctx.status %>
|
35 | `;
|
36 |
|
37 | aries.compile(asyncTpl, {http:http}, (err, renderStr, isUseCache) => {
|
38 | if(err) return console.log(err);
|
39 | console.log(renderStr);
|
40 | });
|
41 |
|
42 |
|
43 | ##API
|
44 |
|
45 | ### aries.set(opt)
|
46 |
|
47 | path: __dirname, // template find root path
|
48 | includeId: function, // ex: (templateId, cb) => return fs.readFile(path.join(__dirname,templateId+'.html'), cb);
|
49 | cacheTime:10000, // 编译缓存时间,单位:毫秒
|
50 |
|
51 | ### aries.compileFile(filepath, ctx, cb)
|
52 |
|
53 | filepath:"async_example.html" // template relate to root path
|
54 | ctx: {} // template ctx object
|
55 | cb: function // ex: (err, renderStr, isUseCache) => {}
|
56 |
|
57 |
|
58 | ### aries.compile(templateStr, ctx, cb)
|
59 |
|
60 | templateStr:"<%= ctx.xxxx %>" // template relate to root path
|
61 | ctx: {} // template ctx object
|
62 | cb: function // ex: (err, renderStr, isUseCache) => {}
|
63 |
|
64 |
|
65 |
|
66 | ## Template Language
|
67 |
|
68 | ### async tag
|
69 |
|
70 | <%?
|
71 | setTimeout(function () {
|
72 | ctx.thinking = "1 seconde later"
|
73 | aries(); //make sure to call aries(); function to end the async function.
|
74 | }, 1000);
|
75 | %>
|
76 |
|
77 |
|
78 | ### example
|
79 |
|
80 | see more example in `example` folder
|
81 |
|
82 | <!DOCTYPE html>
|
83 | <html lang="zh_CN" class="html-">
|
84 | <body>
|
85 | <!-- test let fature -->
|
86 | <% let testLet = "let" %>
|
87 | <h4>aries is support that: <%= testLet %></h4>
|
88 |
|
89 | <!-- test const fature -->
|
90 | <% const testConst = "const"; %>
|
91 | <h4>aries is support that: <%= testConst %></h4>
|
92 |
|
93 | <!-- test template string fature -->
|
94 | <% let tempString = "string"; %>
|
95 | <% let templateString = `template ${tempString} `;%>
|
96 | <h4>aries is support that: <%= templateString %></h4>
|
97 |
|
98 | <!-- test string.raw string fature -->
|
99 | <% var stringRaw = String.raw({ raw: 'srn.a ' }, 't', 'i', 'g', 'r', 'w'); %>
|
100 | <h4>aries is support that: <%= stringRaw %></h4>
|
101 |
|
102 | <!-- test Symbol fature -->
|
103 | <% if( Symbol.for("bar") === Symbol.for("bar") ){ %>
|
104 | <h4>aries is support that: <%= "Symbol" %></h4>
|
105 | <% } %>
|
106 |
|
107 | <!-- test map fature -->
|
108 | <% var m = new Map(); %>
|
109 | <% var o = {p: "Hello World"};%>
|
110 | <% m.set(o, "map"); %>
|
111 | <% let testMap = m.get(o); %>
|
112 | <h4>aries is support that: <%= testMap %></h4>
|
113 |
|
114 | <!-- <%= ctx.fsdsdfsdfsd %> -->
|
115 | <% function* helloWorldGenerator() { %>
|
116 | <% yield 'Generator'; %>
|
117 | <% return 'ending'; %>
|
118 | <% } %>
|
119 | <% let hw = helloWorldGenerator(); %>
|
120 | <% let testGenerator = hw.next().value; %>
|
121 | <h4>aries is support that: <%= testGenerator %></h4>
|
122 |
|
123 | <!-- test arrow functions fature -->
|
124 | <% let testArrowFunction = ""; %>
|
125 | <% ["arrow","fuction"].forEach((x) => { %>
|
126 | <% testArrowFunction += ' ' +x; %>
|
127 | <% })%>
|
128 | <h4>aries is support that: <%= testArrowFunction %></h4>
|
129 |
|
130 | <!-- test for .. of fature -->
|
131 | <% var str = ''; %>
|
132 | <% for (let item of 'for...of') { %>
|
133 | <% str += item; %>
|
134 | <% } %>
|
135 | <h4>aries is support that: <%= str %></h4>
|
136 |
|
137 | <!-- test for .. of fature -->
|
138 | <% include "foot.html" %>
|
139 |
|
140 |
|
141 | <!-- test for .. of fature ,includeId and transfer param, in foot tpl, use ctx.param1 and ctx.param2 to get it-->
|
142 | <% includeId "foot?param1=1¶m2=2" %>
|
143 |
|
144 | </body>
|
145 | </html>
|
146 |
|
147 |
|
148 | ## MIT
|
149 |
|
150 | ```
|
151 | Copyright (c) 2013 wu zhonghua <snoopyxdy@gmail.com>
|
152 |
|
153 | The MIT License
|
154 |
|
155 | Permission is hereby granted, free of charge, to any person obtaining
|
156 | a copy of this software and associated documentation files (the
|
157 | "Software"), to deal in the Software without restriction, including
|
158 | without limitation the rights to use, copy, modify, merge, publish,
|
159 | distribute, sublicense, and/or sell copies of the Software, and to
|
160 | permit persons to whom the Software is furnished to do so, subject to
|
161 | the following conditions:
|
162 |
|
163 | The above copyright notice and this permission notice shall be
|
164 | included in all copies or substantial portions of the Software.
|
165 |
|
166 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
167 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
168 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
169 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
170 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
171 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
172 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
173 | ``` |
\ | No newline at end of file |