Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
6056 efrain 1
## JsRender: best-of-breed templating
2
 
3
*Simple and intuitive, powerful and extensible, lightning fast*
4
 
5
*For templated content in the browser or on Node.js (with Express 4, Hapi and Browserify integration)*
6
 
7
**JsRender** is a light-weight but powerful templating engine, highly extensible, and optimized for high-performance rendering, without DOM dependency. It is designed for use in the browser or on Node.js, with or without jQuery.
8
 
9
**[JsRender](https://github.com/BorisMoore/jsrender)** and **[JsViews](https://github.com/BorisMoore/jsviews)** together provide the next-generation implementation of the official jQuery plugins *[JQuery Templates](https://github.com/BorisMoore/jquery-tmpl)*, and *[JQuery Data Link](https://github.com/BorisMoore/jquery-datalink)* -- and supersede those libraries.
10
 
11
### Documentation and downloads
12
 
13
**[Documentation](http://www.jsviews.com)**, **[downloads](http://www.jsviews.com/#download)**, **[samples](http://www.jsviews.com/#samples)** and **[API docs and tutorials](http://www.jsviews.com/#jsrapi)** are available on the **[www.jsviews.com website](http://www.jsviews.com/#jsrender)**.
14
 
15
The content of this ***ReadMe*** is available also as a *[JsRender Quickstart](http://www.jsviews.com/#jsr-quickstart)*.
16
 
17
### JsRender and JsViews
18
 
19
JsRender is used for data-driven rendering of templates to strings, ready for insertion in the DOM.
20
 
21
It is also used by the *[JsViews](http://www.jsviews.com/#jsviews)* platform, which adds data binding to JsRender templates, and provides a fully-fledged MVVM platform for easily creating interactive data-driven single page apps and websites.
22
 
23
## JsRender installation
24
 
25
*jsrender.js* is available from [downloads](http://www.jsviews.com/#download) on the jsviews.com site.
26
 
27
*CDN delivery* is available from the ***[cdnjs](https://cdnjs.com)*** CDN at [cdnjs.com/libraries/jsrender](https://cdnjs.com/libraries/jsrender).
28
 
29
Alternatively:
30
- It can be installed with **[Bower](http://bower.io/search/?q=jsrender)**, using `$ bower install jsrender`
31
- It can be loaded using an *AMD script loader*, such as RequireJS
32
- For installation using *Node.js* (*npm*) see *[JsRender Node.js Quickstart](http://www.jsviews.com/#jsr-node-quickstart)*
33
- (For browser loading using *Browserify* or *webpack* - see *[JsRender Node.js Quickstart](http://www.jsviews.com/#jsr-node-quickstart)*, *[JsRender as a Browserify module](http://www.jsviews.com/#node/browserify@jsrender)* and *[JsRender as a webpack module](http://www.jsviews.com/#node/webpack@jsrender)*)
34
 
35
#### Using JsRender with jQuery
36
 
37
When jQuery is present, JsRender loads as a jQuery plugin and adds `$.views`, `$.templates` and `$.render` to the jQuery namespace object, `$` (or `window.jQuery`).
38
 
39
*Example HTML page:* [JsRender with jQuery](http://www.jsviews.com/#download/pages-jsr-jq)
40
 
41
#### Using JsRender without jQuery
42
 
43
When jQuery is not present, JsRender provides its own `jsrender` namespace object, exposed as `window.jsrender`
44
 
45
The `jsrender` namespace provides the same methods/APIs as with jQuery, so if jQuery is not present you can still use all the API examples, by simply writing:
46
 
47
```js
48
var $ = window.jsrender;
49
 
50
// Now use code as in samples/examples, with $.views... $.templates... $.render...
51
```
52
 
53
*Example HTML page:* [JsRender without jQuery](http://www.jsviews.com/#download/pages-jsr)
54
 
55
#### JsRender on Node.js
56
 
57
JsRender can be used to render templates on the server (using Node.js) as well as in the browser. JsRender on Node.js has all the features and APIs of JsRender in the browser, plus some additional ones specific to Node.js.
58
 
59
It also provides built-in *Express*, *Hapi* and *Browserify* integration -- which makes it easy to register templates as simple `.html` files on the file system, and then load and render them either server-side, client-side or both.
60
 
61
**Learn more:** *[JsRender Node.js Quickstart](http://www.jsviews.com/#jsr-node-quickstart)* and *[JsRender APIs for Node.js](http://www.jsviews.com/#jsrnode)*.
62
 
63
**Code samples:** See *[JsRender Node Starter](https://github.com/BorisMoore/jsrender-node-starter)* for running code examples of Node.js scenarios, including with *Express*, *Hapi* and *Browserify*.
64
 
65
## JsRender usage
66
 
67
### _Define a template_
68
 
69
From a string:
70
 
71
```js
72
var tmpl = $.templates("Name: {{:name}}");
73
```
74
 
75
From a template declared as markup in a script block:
76
 
77
```html
78
<script id="myTemplate" type="text/x-jsrender">
79
Name: {{:name}}
80
</script>
81
```
82
 
83
then, somewhere in your script:
84
 
85
```js
86
var tmpl = $.templates("#myTemplate");
87
```
88
 
89
On Node.js, from an .html file containing the template markup:
90
 
91
```js
92
var $ = require('jsrender'); // returns the jsrender namespace object
93
var tmpl = $.templates("./templates/myTemplate.html");
94
```
95
 
96
[Learn more...](http://www.jsviews.com/#d.templates)
97
 
98
### _Render a template_
99
 
100
`tmpl.render(object)` (or shortcut form: `tmpl(object)`) renders the template with the object as data context.
101
 
102
```js
103
var tmpl = $.templates(" Name: {{:name}}<br/> ");
104
 
105
var person = {name: "Jim"};
106
 
107
// Render template for person object
108
var html = tmpl.render(person); // ready for insertion, e.g $("#result").html(html);
109
 
110
// result: "Name: Jim<br/> "
111
```
112
 
113
`tmpl.render(array)` (or `tmpl(array)`) renders the template once for each item in the array.
114
 
115
```js
116
var people = [{name: "Jim"}, {name: "Pedro"}];
117
 
118
// Render template for people array
119
var html = tmpl.render(people); // ready for insertion...
120
 
121
// result: "Name: Jim<br/> Name: Pedro<br/> "
122
```
123
 
124
[Learn more...](http://www.jsviews.com/#rendertmpl)
125
 
126
### _Register a named template - and render it_
127
 
128
```js
129
// Register named template - "myTmpl1
130
$.templates("myTmpl1", "Name: {{:name}}<br/> ");
131
 
132
var person = {name: "Jim"};
133
 
134
// Render named template
135
var html = $.templates.myTmpl1(person);
136
 
137
// Alternative syntax: var html = $.render.myTmpl1(person);
138
 
139
// result: "Name: Jim<br/> "
140
```
141
 
142
[Learn more...](http://www.jsviews.com/#rendertmpl)
143
 
144
### _Template tags_
145
 
146
#### Template tag syntax
147
 
148
- All tags other than `{{: ...}}` `{{> ...}}` `{{* ...}}` `{{!-- --}}` behave as *block tags*
149
 
150
- Block tags can have content, unless they use the self-closing syntax:
151
    - *Block tag - with content:* `{{someTag ...}} content {{/someTag}}`
152
    - *Self-closing tag - no content (empty):* `{{someTag .../}}`
153
 
154
- A particular case of self-closing syntax is when any block tag uses the named parameter `tmpl=...` to reference an external template, which then replaces what would have been the block content:
155
 
156
    - *Self-closing block tag referencing an external template:* `{{someTag ... tmpl=.../}}`
157
 (This lets you do [template composition](http://www.jsviews.com/#tagsyntax@composition). See [example](http://www.jsviews.com/#samples/jsr/composition/tmpl).)
158
 
159
- Tags can take both unnamed arguments and named parameters:
160
    - `{{someTag argument1 param1=...}} content {{/someTag}}`
161
    - an example of a named parameter is the `tmpl=...` parameter mentioned above
162
    - arguments and named parameters can be assigned values from simple data-paths such as `address.street` or from richer expressions such as `product.quantity * 3.1 / 4.5`, or `name.toUpperCase()`
163
 
164
[Learn more...](http://www.jsviews.com/#tagsyntax)
165
 
166
#### Built-in tags
167
 
168
#### _{{: ...}}_ (Evaluate)
169
 
170
`{{: pathOrExpr}}` inserts the value of the path or expression.
171
 
172
```js
173
var data = {address: {street: "Main Street"} };
174
var tmpl = $.templates("<b>Street:</b> {{:address.street}}");
175
var html = tmpl.render(data);
176
 
177
// result: "<b>Street:</b> Main Street"
178
```
179
 
180
[Learn more...](http://www.jsviews.com/#assigntag)
181
 
182
#### _{{> ...}}_ (HTML-encode)
183
 
184
`{{> pathOrExpr}}` inserts the *HTML-encoded* value of the path or expression.
185
 
186
```js
187
var data = {condition: "a < b"};
188
var tmpl = $.templates("<b>Formula:</b> {{>condition}}");
189
var html = tmpl.render(data);
190
 
191
// result: "<b>Formula:</b> a &lt; b"
192
```
193
 
194
[Learn more...](http://www.jsviews.com/#htmltag)
195
 
196
#### _{{include ...}}_ (Template composition - partials)
197
 
198
`{{include pathOrExpr}}...{{/include}}`evaluates the block content against a specified/modified data context.
199
 
200
`{{include ... tmpl=.../}}` evaluates the specified template against an (optionally modified) context, and inserts the result. (Template composition).
201
 
202
```js
203
var data = {name: "Jim", address: {street: "Main Street"} };
204
 
205
// Register two named templates
206
$.templates({
207
  streetTmpl: "<i>{{:street}}</i>",
208
  addressTmpl: "{{:name}}'s address is {{include address tmpl='streetTmpl'/}}."
209
});
210
 
211
// Render outer template
212
var html = $.templates.addressTmpl(data);
213
 
214
// result: "Jim's address is <i>Main Street</i>"
215
```
216
 
217
[Learn more...](http://www.jsviews.com/#includetag)
218
 
219
#### _{{for ...}}_ (Template composition, with iteration over arrays)
220
 
221
`{{for pathOrExpr}}...{{/for}}`evaluates the block content against a specified data context. If the new data context is an array, it iterates over the array, renders the block content with each data item as context, and concatenates the result.
222
 
223
`{{for pathOrExpr tmpl=.../}}` evaluates the specified template against a data context. If the new data context is an array, it iterates over the array, renders the template with each data item as context, and concatenates the result.
224
 
225
```html
226
<script id="peopleTmpl" type="text/x-jsrender">
227
  <ul>{{for people}}
228
    <li>Name: {{:name}}</li>
229
  {{/for}}</ul>
230
</script>
231
```
232
 
233
```js
234
var data = {people: [{name: "Jim"}, {name: "Pedro"}] };
235
var tmpl = $.templates("#peopleTmpl");
236
var html = tmpl.render(data);
237
 
238
// result: "<ul> <li>Name: Jim</li> <li>Name: Pedro</li> </ul>"
239
```
240
 
241
[Learn more...](http://www.jsviews.com/#fortag)
242
 
243
#### _{{props ...}}_ (Iteration over properties of an object)
244
 
245
`{{props pathOrExpr}}...{{/prop}}` or `{{props pathOrExpr tmpl=.../}}` iterates over the properties of the object returned by the path or expression, and renders the content/template once for each property - using as data context: `{key: propertyName, prop: propertyValue}`.
246
 
247
```html
248
<script id="personTmpl" type="text/x-jsrender">
249
  <ul>{{props person}}
250
    <li>{{:key}}: {{:prop}}</li>
251
  {{/props}}</ul>
252
</script>
253
```
254
 
255
```js
256
var data = {person: {first: "Jim", last: "Varsov"} };
257
var tmpl = $.templates("#personTmpl");
258
var html = tmpl.render(data);
259
 
260
// result: "<ul> <li>first: Jim</li> <li>last: Varsov</li> </ul>"
261
```
262
 
263
[Learn more...](http://www.jsviews.com/#propstag)
264
 
265
#### _{{if ...}}_ (Conditional inclusion)
266
 
267
`{{if pathOrExpr}}...{{/if}}` or `{{if pathOrExpr tmpl=.../}}` renders the content/template only if the evaluated path or expression is 'truthy'.
268
 
269
`{{if pathOrExpr}}...{{else pathOrExpr2}}...{{else}}...{{/if}}` behaves as '*if' - 'else if' - 'else'* and renders each block based on the conditions.
270
 
271
```html
272
<script id="personTmpl" type="text/x-jsrender">
273
  {{if nickname}}
274
    Nickname: {{:nickname}}
275
  {{else name}}
276
    Name: {{:name}}
277
  {{else}}
278
    No name provided
279
  {{/if}}
280
</script>
281
```
282
 
283
```js
284
var data = {nickname: "Jim", name: "James"};
285
var tmpl = $.templates("#personTmpl");
286
var html = tmpl.render(data);
287
 
288
// result: "Nickname: Jim"
289
```
290
 
291
[Learn more...](http://www.jsviews.com/#iftag)
292
 
293
#### Other built-in tags
294
 
295
For details on all the above built-in tags, as well as *[comment tags](http://www.jsviews.com/#commenttag)* _{{!-- ... --}}_ and *[allow code tags](http://www.jsviews.com/#allowcodetag)* _{{\*&nbsp;...&nbsp;}} and {{\*:&nbsp;...}}_, see the [tags documentation](http://www.jsviews.com/#jsrtags) on jsviews.com.
296
 
297
#### Custom tags
298
 
299
Creating your own custom tags is easy. You can provide an object, with render method, template, event handlers, etc. See samples [here](http://www.jsviews.com/#samples/jsr/tags) and [here](http://www.jsviews.com/#samples/tag-controls) on jsviews.com. But for simple tags, you may only need a simple render function, or a template string.
300
 
301
For example the two following definitions for a `{{fullName/}}` tag provide equivalent behavior:
302
 
303
As a render function:
304
 
305
```js
306
$.views.tags("fullName", function(val) {
307
  return val.first + " " + val.last;
308
});
309
```
310
 
311
Or as a template string:
312
 
313
```js
314
$.views.tags("fullName", "{{:first}} {{:last}}");
315
```
316
 
317
Either way, the result will be as follows:
318
 
319
```js
320
var tmpl = $.templates("{{fullName person/}}");
321
var data = {person: {first: "Jim", last: "Varsov"}};
322
var html = tmpl.render(data);
323
 
324
// result: "Jim Varsov"
325
```
326
 
327
### _Helpers_
328
 
329
For details on helpers, see the [Helpers](http://www.jsviews.com/#helpers) documentation topic on jsviews.com.
330
 
331
Here is a simple example. Two helpers - a function, and a string:
332
 
333
```js
334
var myHelpers = {
335
  upper: function(val) { return val.toUpperCase(); },
336
  title: "Sir"
337
};
338
```
339
 
340
Access the helpers using the `~myhelper` syntax:
341
 
342
```js
343
var tmpl = $.templates("{{:~title}} {{:first}} {{:~upper(last)}}");
344
```
345
 
346
We can pass the helpers in with the `render()` method
347
 
348
```js
349
var data = {first: "Jim", last: "Varsov"};
350
 
351
var html = tmpl.render(data, myHelpers);
352
 
353
// result: "Sir Jim VARSOV"
354
```
355
 
356
Or we can register helpers globally:
357
 
358
```js
359
$.views.helpers(myHelpers);
360
 
361
var data = {first: "Jim", last: "Varsov"};
362
var html = tmpl.render(data);
363
 
364
// result: "Sir Jim VARSOV"
365
```
366
 
367
[Learn more...](http://www.jsviews.com/#helpers)
368
 
369
### _Converters_
370
 
371
Converters are used with the `{{:...}}` tag, using the syntax `{{mycvtr: ...}}}`.
372
 
373
Example - an *upper* converter, to convert to upper case:
374
 
375
```js
376
$.views.converters("upper", function(val) { return val.toUpperCase(); });
377
 
378
var tmpl = $.templates("{{:first}} {{upper:last}}");
379
var data = {first: "Jim", last: "Varsov"};
380
var html = tmpl.render(data);
381
 
382
// result: "Jim VARSOV"
383
```
384
 
385
[Learn more...](http://www.jsviews.com/#converters)
386
 
387
### _Logic and expressions_
388
 
389
JsRender supports rich expressions and logic, but at the same time encapsulates templates to prevent random access to globals. If you want to provide access to global variables within a template, you have to pass them in as data or as helpers.
390
 
391
You can assign rich expressions to any template arguments or parameters, as in:
392
 
393
`{{:person.nickname ? "Nickname: " + person.nickname : "(has no nickname)"}}`
394
 
395
or
396
 
397
```html
398
{{if ~limits.maxVal > (product.price*100 - discount)/rate}}
399
  ...
400
{{else ~limits.minVal < product.price}}
401
  ...
402
{{else}}
403
  ...
404
{{/if}}
405
```
406
 
407
### _Documentation and APIs_
408
 
409
See the [www.jsviews.com](http://www.jsviews.com) site, including the *[JsRender Quickstart](http://www.jsviews.com/#jsr-quickstart)* and [JsRender APIs](http://www.jsviews.com/#jsrapi) topics.
410
 
411
### _Demos_
412
 
413
Demos and samples can be found at [www.jsviews.com/#samples](http://www.jsviews.com/#samples/jsr), and throughout the [API documentation](http://www.jsviews.com/#jsrapi).
414
 
415
(See also the [demos](https://github.com/BorisMoore/jsrender/tree/master/demos) folder of the GitHub repository - available [here](http://borismoore.github.io/jsrender/demos/index.html) as live samples).