@@ -13,32 +13,29 @@ It works with all javascript frameworks that support
13
13
[ hyperscript] ( https://github.com/hyperhype/hyperscript ) .
14
14
15
15
## Usage as a template engine
16
- ### render(templateId, scope, target)
17
- Renders the template using the scope. If a target node is passed, the result
18
- will be inserted into the node, otherwise it will return the DOM element.
16
+ ### compile(element) -> render(scope)
17
+ Compiles the element as a template.
18
+ Returns a function that interpolate the scope inside the element.
19
+ You can render as many times you like.
19
20
20
- - templateId: the id of the view template tag in the current page .
21
+ - element: Any DOM element .
21
22
- scope: template scope to be used.
22
- - target: optional DOM element where the result will be displayed. If none
23
- target is passed it will return a DOM element with the result.
24
23
25
24
``` html
26
25
<html >
27
26
<head >
28
27
<script type =" module" >
29
- import render from " https://cdn.jsdelivr.net/gh/marcodpt/tint /template.js"
30
-
31
- render (' my-view ' , {
28
+ import compile from " . /template.js"
29
+ const render = compile ( document . getElementById ( " app " ))
30
+ render ({
32
31
message: " Hello World!"
33
- }, document . getElementById ( " app " ) )
32
+ })
34
33
</script >
35
34
</head >
36
35
<body >
37
- <div id =" app" ></div >
38
-
39
- <template id =" my-view" >
40
- <h1 :text =" message" ></h1 >
41
- </template >
36
+ <div id =" app" >
37
+ <h1 :text =" message" >Loading...</h1 >
38
+ </div >
42
39
</body >
43
40
</html >
44
41
```
@@ -54,63 +51,55 @@ You can check the result:
54
51
- [ source] ( https://raw.githubusercontent.com/marcodpt/tint/main/hello.html )
55
52
56
53
## Building a todo app
57
- Now we gonna show examples wrappers you can use with some hyperscript
58
- frameworks that you can use to construct real world applications.
54
+ Now we will show examples with hyperscript frameworks to build real world
55
+ applications.
59
56
60
57
In all examples we use the same ` body ` . This is a full demonstration of the
61
58
power of layout separation.
62
59
63
60
``` html
64
61
<body >
65
- <main id =" app" ></main >
66
- <template id =" todo" >
62
+ <main id =" app" >
67
63
<h1 >To do list</h1 >
68
64
<input type =" text" :value =" value" :oninput =" NewValue" >
69
65
<ul >
70
66
<li :each =" todos" :text ></li >
71
67
</ul >
72
68
<button :onclick =" AddTodo" >New!</button >
73
- </template >
69
+ </main >
74
70
</body >
75
71
```
76
72
77
73
### Todo without any framework or virtual DOM
78
- This is not the recommended way to do it.
79
- You will update the DOM more than necessary, you will lose focus and maybe
80
- other things.
74
+ This is not the recommended way to do this.
75
+ You'll update the DOM more than necessary, lose focus, and maybe other things.
81
76
But here's a demo anyway.
82
77
83
78
``` js
84
- import render from " https://cdn.jsdelivr.net/gh/marcodpt/tint/template.js"
79
+ import compile from " https://cdn.jsdelivr.net/gh/marcodpt/tint/template.js"
80
+
81
+ const render = compile (document .getElementById (" app" ))
85
82
86
83
const state = {
87
84
todos: [],
88
85
value: " " ,
89
86
AddTodo : () => {
90
87
state .todos .push (state .value )
91
88
state .value = " "
92
- rerender ( )
89
+ render (state )
93
90
},
94
91
NewValue : ev => {
95
92
state .value = ev .target .value
96
93
}
97
94
}
98
95
99
- const node = document .getElementById (" app" )
100
- const rerender = () => render (' todo' , state, node)
101
-
102
- rerender ()
96
+ render (state)
103
97
```
104
98
105
99
- [ live] ( https://marcodpt.github.io/tint/template.html )
106
100
- [ source] ( https://raw.githubusercontent.com/marcodpt/tint/main/template.html )
107
101
108
102
### Todo with [ Superfine] ( https://github.com/jorgebucaran/superfine )
109
- Here we changed a little bit the usage of superfine, you must pass an object as
110
- state, and you need to add inside the object:
111
- - nodeId: The id of the DOM element where you will mount the app.
112
- - templateId: The id of the template tag with your view.
113
-
114
103
``` js
115
104
import superfine from " https://cdn.jsdelivr.net/gh/marcodpt/tint/superfine.js"
116
105
@@ -136,22 +125,20 @@ const setState = superfine(state)
136
125
- [ source] ( https://raw.githubusercontent.com/marcodpt/tint/main/superfine.html )
137
126
138
127
### Todo with [ Hyperapp] ( https://github.com/jorgebucaran/hyperapp )
139
- We eliminated ` view ` property because ` tint ` will generate it automatically
140
- based on ` templateId ` that you pass to ` app ` .
128
+ We delete the ` view ` property because ` tint ` will automatically generate it
129
+ based on the ` node ` that is passed to the ` app ` .
141
130
142
- We've also introduced an ` actions ` object for your static methods, but you can
143
- also enter any variable that is a constant here.
131
+ We've also introduced an ` actions ` object for your static methods.
144
132
145
133
Everything else is exactly equals on hyperapp.
146
134
147
- [ Here] ( https://github.com/jorgebucaran/hyperapp/issues/1098 ) is a nice
148
- discussion where this wrapper came to existence .
135
+ [ Here] ( https://github.com/jorgebucaran/hyperapp/issues/1098 ) is the thread
136
+ that gave rise to this wrapper .
149
137
150
138
``` js
151
139
import app from " https://cdn.jsdelivr.net/gh/marcodpt/tint/hyperapp.js"
152
140
153
141
app ({
154
- templateId: ' todo' ,
155
142
actions: {
156
143
AddTodo : state => ({
157
144
... state,
@@ -175,17 +162,17 @@ app({
175
162
- [ source] ( https://raw.githubusercontent.com/marcodpt/tint/main/hyperapp.html )
176
163
177
164
### Todo with [ Mithril.js] ( https://github.com/MithrilJS/mithril.js )
178
- In this example you also must import mithril in the page by yourself before the
165
+ In this example, you must also import mithril into the page yourself before the
179
166
wrapper.
180
167
181
- You can pass any of the components methods like: ` oninit ` , ` oncreate ` , etc .
182
- And it will work fine .
168
+ The ` view ` method is generated by the ` state ` property .
169
+ All other components methods are available. Ex: ` oninit ` , ` oncreate ` , etc .
183
170
184
171
``` js
185
172
import component from ' https://cdn.jsdelivr.net/gh/marcodpt/tint/mithril.js'
186
173
174
+ const app = document .getElementById (" app" )
187
175
const state = {
188
- templateId: ' todo' ,
189
176
todos: [],
190
177
value: " " ,
191
178
AddTodo : () => {
@@ -196,17 +183,23 @@ const state = {
196
183
state .value = ev .target .value
197
184
}
198
185
}
186
+ const todo = component (app, {
187
+ oninit : () => {
188
+ console .log (' component oninit' )
189
+ },
190
+ state: state
191
+ })
199
192
200
- m .mount (document . getElementById ( " app" ), component (state) )
193
+ m .mount (app, todo )
201
194
```
202
195
203
196
- [ live] ( https://marcodpt.github.io/tint/mithril.html )
204
197
- [ source] ( https://raw.githubusercontent.com/marcodpt/tint/main/mithril.html )
205
198
206
199
### Todo with [ preact] ( https://github.com/preactjs/preact )
207
- Our preact wrapper is not the best. But it serves to show an example
200
+ Our ` preact ` wrapper is not the best. But it serves to show an example
208
201
with a global state and no components. If you think you can improve this
209
- wrapper please send a pull request.
202
+ wrapper, please submit a pull request.
210
203
211
204
``` js
212
205
import preact from " https://cdn.jsdelivr.net/gh/marcodpt/tint/preact.js"
@@ -231,8 +224,8 @@ const render = preact(state, document.getElementById("app"))
231
224
- [ live] ( https://marcodpt.github.io/tint/preact.html )
232
225
- [ source] ( https://raw.githubusercontent.com/marcodpt/tint/main/preact.html )
233
226
234
- ## Usage as a low level library
235
- To build all the wrappers for the TODO application, we use this package as a
227
+ ## Usage as a low- level library
228
+ To build all the wrappers for the ` TODO ` application, we use this package as a
236
229
low-level library.
237
230
238
231
``` js
@@ -255,7 +248,7 @@ If you created something interesting, or a wrapper for another framework or
255
248
improved any example. Submit a pull request or open an issue with your
256
249
code.
257
250
258
- ### tint(h, text) -> render
251
+ ### tint(h, text) -> compile
259
252
260
253
Transforms all template tags (with id attribute) within the current page into a
261
254
[ hyperscript] ( https://github.com/hyperhype/hyperscript ) function.
@@ -268,20 +261,22 @@ DOM or virtual DOM element, if no function is passed, it will use
268
261
[ hyperscript] ( https://github.com/hyperhype/hyperscript ) function that create
269
262
DOM or vDOM text nodes.
270
263
271
- ### render(templateId, scope, rootTag, rootAttributes ) -> element
264
+ ### compile(element, templateId) -> render(scope ) -> DOM/vDOM element
272
265
273
266
Return the DOM or vDOM element (based on the ` h ` function) that is generated
274
- by renderering the ` scope ` with the template.
275
-
276
- - templateId: The name of the template. Observe that you can only use as
277
- custom tags templates that id contains ` - ` . As described
278
- [ here] ( https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements ) .
279
- - scope: The data passed to interpolate the template, any JSON object even
280
- with javascript functions is valid!
281
- - rootTag: The optional root where the resulted should be mounted. If no root
282
- is passed it will be mounted inside a ` div ` tag.
283
- - rootAttributes: The optional attributes object (with the exactly hypescript
284
- syntax of the ` h ` function). No attributes is the default.
267
+ by renderering the ` scope ` within the ` element ` .
268
+
269
+ Optionally, you can use a ` templateId ` to replace the ` element ` 's inner HTML
270
+ with your own contents.
271
+
272
+ - element: The ` element ` that will be the root of the result. If you don't
273
+ pass a ` templateId ` , it will be treated as a complete template. in case you
274
+ provide a ` templateId ` it will only be used as the root of the result,
275
+ ignoring its content.
276
+ - templateId: The optional name of the template you want to render.
277
+ - scope: The data passed to interpolate the ` element `
278
+ (or ` templateId ` if passed),
279
+ any JSON object even with javascript functions is valid!
285
280
286
281
## Docs for the template engine
287
282
### : attribute
@@ -374,12 +369,12 @@ el.innerHTML
374
369
#### Append text to node
375
370
``` html
376
371
<template id =" text-1" >
377
- <h1 :text =" name " >Hello </h1 >
372
+ <h1 :text =" content " >Hello World! </h1 >
378
373
</template >
379
374
```
380
375
``` js
381
376
render (' text-1' , {
382
- name : " John"
377
+ content : " Hello John! "
383
378
}).innerHTML
384
379
```
385
380
``` html
@@ -654,13 +649,20 @@ render('with-2', {
654
649
<template id =" with-3" >
655
650
<div :with =" 0" >
656
651
<p :text =" 0" ></p >
657
- <p :text =" 1" ></p >
652
+ <template :with =" 1" >
653
+ <p :text =" " ></p >
654
+ </template >
658
655
</div >
659
656
<div :with =" 1" >
660
657
<p :text =" " ></p >
661
658
</div >
662
659
<div :with =" 2" >
663
- <p :text =" " ></p >
660
+ <template :with =" " >
661
+ <p :text =" " ></p >
662
+ </template >
663
+ </div >
664
+ <div :with =" 3" >
665
+ <p >This will not render</p >
664
666
</div >
665
667
</template >
666
668
```
@@ -801,7 +803,7 @@ With the following tag in your `HTML` `body`
801
803
``` html
802
804
<template id =" custom-1" >
803
805
<div >
804
- <my-button :btn =" button" :text =" title" >Go </my-button >
806
+ <my-button :btn =" button" :text =" title" >Action </my-button >
805
807
</div >
806
808
</template >
807
809
```
@@ -813,15 +815,15 @@ render('custom-1', {
813
815
```
814
816
``` html
815
817
<div >
816
- <button class =" btn btn-primary" >Go Submit</button >
818
+ <button class =" btn btn-primary" >Submit</button >
817
819
</div >
818
820
```
819
821
820
822
#### Iterate with custom tags.
821
823
``` html
822
824
<template id =" custom-2" >
823
825
<div >
824
- <my-button :each =" " :btn =" button" :text =" title" >Go </my-button >
826
+ <my-button :each =" " :btn =" button" :text =" title" ></my-button >
825
827
</div >
826
828
</template >
827
829
```
@@ -833,8 +835,8 @@ render('custom2', [
833
835
```
834
836
``` html
835
837
<div >
836
- <button class =" btn btn-secondary" >Go Cancel</button >
837
- <button class =" btn btn-primary" >Go Submit</button >
838
+ <button class =" btn btn-secondary" >Cancel</button >
839
+ <button class =" btn btn-primary" >Submit</button >
838
840
</div >
839
841
```
840
842
@@ -936,19 +938,19 @@ render('my-list', [
936
938
```
937
939
938
940
## Philosophy
939
- - Separation: Functions and data transformations belong to javascript, design
940
- and visual presentation to html and css, and the data belongs to database.
941
+ - Separation: Functions and data transformations belong in javascript, design
942
+ and visual presentation to the html and css, and the data belongs to the database.
941
943
- Designers and people with no javascript knowledge should understand it
942
944
and quickly become productive.
943
945
- Templates must be valid ` XML ` /` HTML ` that can be inserted into a
944
946
` template ` tag.
945
- - Should not conflict with other templates engines and frameworks.
946
- - Every layout should be written only once, without repetition.
947
+ - It must not conflict with other templates engines and frameworks.
948
+ - Each layout should be written only once, without repetition.
947
949
- Simplicity matters.
948
- - Beautiful syntax matters .
950
+ - Elegant syntax is important .
949
951
950
952
## Influences and thanks
951
- This work is hugely influenced by this amazing template engines and frameworks:
953
+ This work is hugely influenced by these amazing template engines and frameworks:
952
954
- [ mustache] ( https://mustache.github.io/mustache.5.html )
953
955
- [ transparency] ( https://github.com/leonidas/transparency )
954
956
- [ thymeleaf] ( https://www.thymeleaf.org/ )
@@ -968,12 +970,12 @@ First, thanks for reading this far.
968
970
969
971
Everything within this documentation is tested
970
972
[ here] ( https://marcodpt.github.io/tint/ ) .
971
- And it will always be so . Any changes to the documentation, any contributions
972
- MUST be present in the tests.
973
+ And it will always like this . Any changes to the documentation,
974
+ any contributions MUST be present in the tests.
973
975
974
976
If the tests do not pass in your browser, if you find any bugs, please raise
975
977
an issue.
976
978
977
979
Any syntax changes must be within the philosophy of this project.
978
980
979
- It's a very simple project. Any contribution is highly appreciated.
981
+ It's a very simple project. Any contribution is greatly appreciated.
0 commit comments