1 | # ![Δt](https://s3.amazonaws.com/cloud.ohloh.net/attachments/49947/%CE%94t_med.png)[dynamictemplate](http://dodo.github.com/node-dynamictemplate/)
|
2 |
|
3 |
|
4 |
|
5 | [dynamictemplate](http://dodo.github.com/node-dynamictemplate/) is yet
|
6 | another template engine, but this time loaded with full async support
|
7 | and capable of being changed even after the template was rendered.
|
8 |
|
9 | It works in browsers too.
|
10 |
|
11 | → [Check out the demo!](http://dodo.github.com/node-dynamictemplate/example/circles.html)
|
12 |
|
13 | ## Installation
|
14 |
|
15 | ```bash
|
16 | $ npm install dynamictemplate
|
17 | ```
|
18 |
|
19 | ## Documentation
|
20 |
|
21 |
|
22 |
|
23 | what are the specialities/features of dt?
|
24 |
|
25 | - async & dynamic → changeable even after tpl was rendered
|
26 | - pure javascipt with a hugh eventbased api → modular & extendable
|
27 | - runs on server and browser ()
|
28 | - different approach than dom: dont get your elements out of the black box. keep only those which you need.
|
29 | - minimalistic (all the other stuff is hidden in modules :P)
|
30 | - jquery or dom api
|
31 | - designer integration (compiles html to a mask which can be applied on a simplified version of the tpl)
|
32 |
|
33 | - flow control is important again
|
34 | - because of its async nature it doesnt struggle with creating tags even after rendering was finished. (which is why i called it 'dynamic')
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | this file
|
43 |
|
44 | is only a convenient helper for asyncxml to create tags easily
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | ### Writing templates
|
63 |
|
64 | ```coffeescript
|
65 | tpl = new Template schema:'xml', doctype:off, pretty:off, encoding:'utf-8', end:on, -> # default settings
|
66 | @$tag 'xml', ->
|
67 | @$tag 'child', "content"
|
68 | ```
|
69 |
|
70 | This actually allows you to write real templates with [asyncxml](https://github.com/dodo/node-asyncxml).
|
71 | Normally, asyncxml just gives you the ability to write asynchronous XML-generating code.
|
72 |
|
73 | ### How to write tags
|
74 |
|
75 | Maybe the main difference to some other template engines is that all the tags are asynchronous.
|
76 | This has the side effect that every tag has to be closed manually. As this can get a little bit anoying when you write very large templates, I added a little shortcut which invokes the end for you at the and of the child scope:
|
77 |
|
78 | ```coffeescript
|
79 | @html ->
|
80 | @body("content").end()
|
81 | @end()
|
82 |
|
83 | # is the exactly same as
|
84 |
|
85 | @$html ->
|
86 | @$body "content"
|
87 | ```
|
88 |
|
89 | Just add a dollar sign (`$`) in front of the tag method and it acts a little bit more synchronous again.
|
90 |
|
91 | ### Plugins
|
92 |
|
93 | * [Δt compiler](https://github.com/dodo/node-dt-compiler) - this compiles static HTML to template masks.
|
94 | * [Δt jquery adapter](https://github.com/dodo/node-dt-jquery) - this lets you insert the template into dom with the help of [jQuery](http://jquery.com/).
|
95 |
|
96 | ### The dynamic part
|
97 |
|
98 | TODO i couldnt find the time to build an example
|
99 | that's works best with dynamictemplate, so please stand by.
|
100 |
|
101 |
|
102 | #### Just FYI
|
103 |
|
104 | This is not finished yet.
|
105 | But please, make yourself comfortable, take a cookie and **start contributing**!
|
106 |
|
107 |
|
108 | ## Example
|
109 |
|
110 | If you are familiar with [coffeekup](http://coffeekup.org), you should recognize this:
|
111 |
|
112 | ```coffeescript
|
113 | stringify = (func) -> func.toString()
|
114 | shoutify = (s) -> s.toUpperCase() + "!"
|
115 | template = ({title, desc, path, user, max}) ->
|
116 | new Template schema:5, doctype:on, ->
|
117 | @$html ->
|
118 | @$head ->
|
119 | @$meta charset:'utf-8'
|
120 | @$title "#{title or 'Untitled'} | My awesome website"
|
121 | if desc?
|
122 | @$meta name:'description', content:desc
|
123 | @$link rel:'stylesheet', href:'/stylesheets/app.css'
|
124 | @$script src:'/javascripts/jquery.js'
|
125 | @$script stringify -> # browser code
|
126 | $ ->
|
127 | alert "Alerts are so annoying..."
|
128 | @$body ->
|
129 | @$header ->
|
130 | @$h1 title or 'Untitled'
|
131 | @$nav ->
|
132 | @$ul ->
|
133 | unless path is '/'
|
134 | @$li -> @$a href:'/', "Home"
|
135 | @$li -> @$a href:'/chunky', "Bacon!"
|
136 | switch user.role
|
137 | when 'owner', 'admin'
|
138 | @$li -> @$a href:'/admin', "Secret Stuff"
|
139 | when 'vip'
|
140 | @$li -> @$a href:'/vip', "Exclusive Stuff"
|
141 | else
|
142 | @$li -> @$a href:'/commoners', "Just Stuff"
|
143 | @$section ->
|
144 | @$h2 "Let's count to #{max}:"
|
145 | @$p "#{i}" for i in [1..max]
|
146 | @$footer ->
|
147 | @$p shoutify "bye"
|
148 | ```
|
149 |
|
150 | [![Build Status](https://secure.travis-ci.org/dodo/node-asyncxml.png)](http://travis-ci.org/dodo/node-dynamictemplate)
|