Comparing version 0.5.2 to 0.5.3
{ | ||
"name": "appex", | ||
"version": "0.5.2", | ||
"version": "0.5.3", | ||
"description": "nodejs web api with typescript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
148
readme.md
@@ -72,6 +72,7 @@  | ||
* [templating](#templating) | ||
* [rendering templates](#rendering_templates) | ||
* [template context](#template_context) | ||
* [layouts](#template_layouts) | ||
* [partials](#template_partials) | ||
* [overview](#template_overview) | ||
* [context](#template_context) | ||
* [syntax](#template_syntax) | ||
* [layouts and sections](#template_layouts_and_sections) | ||
* [render](#template_render) | ||
* [caching and devmode](#caching_and_devmode) | ||
@@ -691,8 +692,9 @@ * [json schema](#json_schema) | ||
<a name="rendering_templates" /> | ||
### rendering templates | ||
<a name="template_overview" /> | ||
### overview | ||
the template engine is passed on the appex app context. The following demonstrates | ||
passing data to, and rendering a template with the engine. | ||
```javascript | ||
The appex template engine is available to all handlers by default. it is accessible | ||
on the context.template property. the following is an example of its use. | ||
``` | ||
//---------------------------------------------- | ||
@@ -702,2 +704,3 @@ // view.txt | ||
<ul> | ||
@for(var n in context.users) { | ||
@@ -707,5 +710,6 @@ | ||
@(context.users[n].name) | ||
<li>@(context.users[n].name)</li> | ||
} | ||
} | ||
</ul> | ||
@@ -724,2 +728,4 @@ //---------------------------------------------- | ||
var text = context.template.render('./view.txt', { users: users }); | ||
context.response.headers['Content-Type'] = 'text/html'; | ||
@@ -730,22 +736,100 @@ context.response.send(text); | ||
``` | ||
note: appex templates supports two control statements, @if for conditions and @for for iteration. | ||
note: rendering variables are achieved with the @(expression) syntax. i.e. @("hello world") or | ||
@(my_var_here). | ||
<a name="template_context" /> | ||
### context | ||
each template is passed a data context. this context allows the caller to | ||
send data to the template for rendering. the context parameter is optional. | ||
the example below is sending the users array to the template context for | ||
rendering. | ||
<a name="template_context" /> | ||
### template context | ||
``` | ||
export function index(context) { | ||
var users = [{name:'dave' , online : true}, | ||
{name:'smith', online : true}, | ||
{name:'jones', online : false}, | ||
{name:'alice', online : true}]; | ||
All user data passed to a template for rendering is passed on the templates 'context'. | ||
context.response.send(context.template.render('./view.txt', { users: users })); | ||
} | ||
``` | ||
<a name="layouts" /> | ||
### layouts | ||
<a name="template_syntax" /> | ||
### syntax | ||
appex templates support layouts by way of the @layout and @section statements. | ||
appex templates support the following statements and syntax | ||
#### if statement | ||
if statments are supported. | ||
``` | ||
@if(expression) { | ||
some content | ||
} | ||
@if(a > 10) { | ||
some content | ||
} | ||
@(user.loggedin) { | ||
<span>welcome</span> | ||
} | ||
``` | ||
#### for statement | ||
the following for loops are supported. | ||
``` | ||
@for(var i = i; i < 100; i++) { | ||
@(i) | ||
} | ||
@for(var n in list) { | ||
@(list[n]) | ||
} | ||
``` | ||
#### expressions | ||
will emit the value contained. | ||
``` | ||
@('hello world') | ||
@(123) | ||
@(some_variable) | ||
``` | ||
#### code blocks | ||
code blocks can be useful for adding template side rendering logic. | ||
``` | ||
@{ | ||
var message = 'hello' | ||
} | ||
@(message) | ||
``` | ||
#### comments | ||
``` | ||
@* | ||
this comment will not be rendered! | ||
*@ | ||
``` | ||
<a name="template_layouts_and_sections" /> | ||
### layouts and sections | ||
appex templates support template inheritance. | ||
consider the following where layout.txt defines the sections 'header' and 'content' and the view.txt overrides | ||
these sections with its own content. | ||
```javascript | ||
``` | ||
//---------------------------------------------- | ||
@@ -818,13 +902,12 @@ // layout.txt | ||
note : it is optional to override content in the view.txt. | ||
note : when specifying a layout, the view will only render content within | ||
the layouts section placeholders. | ||
note : @sections without a body (like the header above) are treated as placeholders. | ||
<a name="render" /> | ||
### render | ||
<a name="partials" /> | ||
### partials | ||
appex templates also allow for partial views with the @render statment. consider the following | ||
which renders the nav.txt file into the layout.txt file. | ||
```javascript | ||
``` | ||
//---------------------------------------------- | ||
@@ -918,2 +1001,15 @@ // nav.txt | ||
in addition to this, a implementation where the devmode is false can override the caching | ||
behaviour with the following. | ||
```javascript | ||
export function index(context) { | ||
// manually override the template devmode option. | ||
context.template.option.devmode = true; | ||
context.response.send(context.template.render('./view.txt')) | ||
} | ||
``` | ||
<a name="json_schema" /> | ||
@@ -920,0 +1016,0 @@ ## json schema |
Sorry, the diff of this file is too big to display
409525
7993
1409