Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 419 lines (307 sloc) 8.953 kb
e5668b7 readme
Artem Tyurin authored
1 # Fest
2
3 ## Данные и вывод
4
5 ### fest:set
6
7 Объявить внутреннюю переменную
8
9 ```xml
10 <fest:set name="name">John</fest:set>
11 ```
12
13 ```xml
14 <fest:set name="full_name">
15 <fest:get name="name"/><fest:space/>F. Kennedy
16 </fest:set>
17 ```
18
c386d00 (README) attribute 'test' for fest:set
Artem Tyurin authored
19 Для ```fest:set``` можно использовать атрибут ```test```. Операция выполнится, если его значение (js-выражение) истинно.
20
0f9573a test for get-set-params, some readme additions
Artem Tyurin authored
21 ```xml
a0ef53a readme
Artem Tyurin authored
22 <fest:set name="name" test="false">should not be set</fest:set>
0f9573a test for get-set-params, some readme additions
Artem Tyurin authored
23 ```
24
a0ef53a readme
Artem Tyurin authored
25 Внутри ```fest:set``` доступен контекст ```params```, передаваемый через ```fest:get```.
0f9573a test for get-set-params, some readme additions
Artem Tyurin authored
26
27 ```xml
28 <fest:set name="line">
29 Hello,<fest:space/><fest:value>params.username</fest:value>
30 </fest:set>
31 <fest:get name="line">{username: "John"}</fest:get>
32 ```
33
e5668b7 readme
Artem Tyurin authored
34 ### fest:get
35
36 Получить переменную, объявленную через ```fest:set```
37
38 ```xml
39 <fest:get name="name"/>
40 ```
41
0f9573a test for get-set-params, some readme additions
Artem Tyurin authored
42 ```xml
43 <fest:get name="name">{'some': 'data'}</fest:get>
44 ```
45
b9b6d78 @AndrewSumin add doc about get select
AndrewSumin authored
46 Если указать тег select, то выражение внутри выполнится и результирующая строка будет именем блока set.
47 ```xml
48 <fest:script>
49 var name = 'foo'
50 </fest:srcipt>
51 <fest:get select="name"/>
52 <fest:set name="foo">foo</fest:set>
53 <fest:set name="bar">bar</fest:set>
54 ```
0f9573a test for get-set-params, some readme additions
Artem Tyurin authored
55
a52a9e5 @madimp fest:element
madimp authored
56 ### fest:element
57
58 Вывод ноды с переменным именем
59
60 ```xml
61 <fest:script>
62 var variable = 'table';
63 </fest:script>
f8b7143 @AndrewSumin select in fest:get
AndrewSumin authored
64 <fest:element select="variable">
a52a9e5 @madimp fest:element
madimp authored
65 fest code
66 </fest:element>
f8b7143 @AndrewSumin select in fest:get
AndrewSumin authored
67 <fest:element select="variable2">
a52a9e5 @madimp fest:element
madimp authored
68 fest code
69 </fest:element>
70 ```
71
72 Выведет
73
74 ```xml
75 <table>fest code</table><div>fest code</div>
76 ```
77
e5668b7 readme
Artem Tyurin authored
78 ### fest:attributes, fest:attribute
79
80 Добавить атрибуты к родительскому тегу. Все ```fest:attribute``` должны быть внутри блока ```fest:attributes```, который должен быть первым внутри тега.
81
82 ```xml
83 <a>
84 <fest:attributes>
0c5f959 @AndrewSumin add {} to documentation
AndrewSumin authored
85 <fest:attribute name="href"><fest:value>json.href</fest:value></fest:attribute>
e5668b7 readme
Artem Tyurin authored
86 </fest:attributes>
87 Some link
88 </a>
89 ```
90
9a0437e @AndrewSumin error in documentation
AndrewSumin authored
91 Быстрый способ вставить значение в атрибут
0c5f959 @AndrewSumin add {} to documentation
AndrewSumin authored
92
93 ```xml
94 <a href="{json.href}">Some link</a>
95 ```
96
e5668b7 readme
Artem Tyurin authored
97 ### fest:value
98
99 Вывести значение js-выражения
100
101 ```xml
102 <fest:value>json.value</fest:value>
103 <fest:value output="text"><![CDATA["<script/>"]]></fest:value>
104 <fest:value output="js">'"'</fest:value>
105 ```
106
107 ### fest:text
108
109 Вывод неформатированного текста
110
111 ```xml
112 <fest:text>"Bla bla bla"</fest:text>
113 ```
114
115 ### fest:space
116
117 Пробел
118
119
120 ## Управляющие конструкции
121
43d4d88 @eprev fixed for and each description
eprev authored
122 ### fest:each
e5668b7 readme
Artem Tyurin authored
123
124 Итерация по объекту
125
126 ```xml
9e86749 @RubaXa * fixed <fest:for/> example
RubaXa authored
127 <fest:script>var obj = {"foo": "bar"}</fest:script>
43d4d88 @eprev fixed for and each description
eprev authored
128 <fest:each iterate="obj" index="i">
9e86749 @RubaXa * fixed <fest:for/> example
RubaXa authored
129 <fest:value>i</fest:value>=<fest:value>obj[i]</fest:value>
43d4d88 @eprev fixed for and each description
eprev authored
130 </fest:each>
131 <fest:each iterate="obj" index="i" value="v">
132 <fest:value>i</fest:value>=<fest:value>v</fest:value>
133 </fest:each>
e5668b7 readme
Artem Tyurin authored
134 ```
135
43d4d88 @eprev fixed for and each description
eprev authored
136 ### fest:for
e5668b7 readme
Artem Tyurin authored
137
a54a944 @AndrewSumin improve foreach, add 'from' 'to'
AndrewSumin authored
138 Итерация по массиву или по числовому ряду
e5668b7 readme
Artem Tyurin authored
139
140 ```xml
141 <fest:script>json.items = ['a', 'b', 'c']</fest:script>
43d4d88 @eprev fixed for and each description
eprev authored
142 <fest:for iterate="json.items" index="i">
e5668b7 readme
Artem Tyurin authored
143 <fest:value>json.items[i]</fest:value>
43d4d88 @eprev fixed for and each description
eprev authored
144 </fest:for>
145 <fest:for iterate="json.items" index="i" value="v">
146 <fest:value>v</fest:value>
147 </fest:for>
148 <fest:for from="1" to="5" index="i">
a54a944 @AndrewSumin improve foreach, add 'from' 'to'
AndrewSumin authored
149 <fest:value>i</fest:value>
43d4d88 @eprev fixed for and each description
eprev authored
150 </fest:for>
e5668b7 readme
Artem Tyurin authored
151 ```
152
153 ### fest:if
154
155 Условный оператор
156
157 ```xml
158 <fest:if test="true">
159 true
160 </fest:if>
161 ```
162
163 ### fest:choose, fest:when, fest:otherwise
164
a54a944 @AndrewSumin improve foreach, add 'from' 'to'
AndrewSumin authored
165 Ветвление. Если ни один ```fest:when``` не выполнен, будет выбрана ветвь ```fest:otherwise```.
e5668b7 readme
Artem Tyurin authored
166
167 ```xml
168 <fest:choose>
169 <fest:when test="1">
170 <fest:text>one</fest:text>
171 </fest:when>
172
173 <fest:when test="2">
174 <fest:text>two</fest:text>
175 </fest:when>
176
177 <fest:otherwise>
178 <fest:text>More than 2</fest:text>
179 </fest:otherwise>
180 </fest:choose>
181 ```
182
183 ## Остальное
184
185 ### fest:cdata
186
187 Блок CDATA
188
189 ```xml
190 <script>
191 <fest:cdata>
192 <![CDATA[alert ("2" < 3);]]>
193 </fest:cdata>
194 </script>
195 ```
196
197 ### fest:comment
198
199 HTML комментарий
200
201 ```xml
202 <fest:comment>comment</fest:comment>
203 ```
204
205 ### fest:doctype
206
207 Объявление doctype страницы
208
209 ```xml
210 <fest:doctype>html</fest:doctype>
211 ```
212
213 ### fest:script
214
215 Выполнить javascript
216
217 ```xml
218 <fest:script>
219 <![CDATA[
220 json.script = 2 < 3;
221 ]]>
222 </fest:script>
223 ```
224
225 ```xml
226 <fest:script src="script.js"/>
227 ```
228
229 ### fest:include
230
a54a944 @AndrewSumin improve foreach, add 'from' 'to'
AndrewSumin authored
231 Вставить содержимое другого шаблона с заданным контекстом.
e5668b7 readme
Artem Tyurin authored
232
233 ```xml
234 <fest:script>json.list = ['a', 'b', 'c'];</fest:script>
235 <fest:include context="json.list" src="./include_foreach.xml"/>
236 ```
237
238 ### fest:insert
239
240 Вставить файл напрямую в шаблон
241
242 ```xml
243 <style type="text/css">
244 <fest:insert src="style.css"/>
245 <style>
246 ```
247
7b39ba3 readme
Artem Tyurin authored
248 # Примеры
249
4a64f1e @eprev installation section moved to examples
eprev authored
250 ## Установка
251
252 ```
253 npm install fest
254 ```
255
e5668b7 readme
Artem Tyurin authored
256 ## Как использовать
257
cd243a2 @eprev added some desc
eprev authored
258 compile():
259
e5668b7 readme
Artem Tyurin authored
260 ```javascript
7a15535 @eprev updated example of usage for NPM
eprev authored
261 var fest = require('fest');
e5668b7 readme
Artem Tyurin authored
262
263 var data = {name: 'Jack "The Ripper"'},
264 template = './templates/basic.xml';
265
4d0d2ec @bobrik beatify -> beautify. grammar nazi ftw!
bobrik authored
266 var compiled = fest.compile(template, {beautify: false}),
e5668b7 readme
Artem Tyurin authored
267 template = (new Function('return ' + compiled))();
a54a944 @AndrewSumin improve foreach, add 'from' 'to'
AndrewSumin authored
268
e5668b7 readme
Artem Tyurin authored
269 console.log(template(data));
270 ```
271
cd243a2 @eprev added some desc
eprev authored
272 render():
273
274 ```javascript
275 var fest = require('fest');
276
277 var data = {name: 'Jack "The Ripper"'},
278 template = './templates/basic.xml';
279
280 console.log(fest.render(template, data, {beautify: false}));
281 ```
282
283
e5668b7 readme
Artem Tyurin authored
284 basic.xml
285
286 ```xml
287 <?xml version="1.0"?>
288 <fest:template xmlns:fest="http://fest.mail.ru" context_name="json">
289 <h1>Hello,<fest:space/><fest:value output="text">json.name</fest:value></h1>
a54a944 @AndrewSumin improve foreach, add 'from' 'to'
AndrewSumin authored
290
e5668b7 readme
Artem Tyurin authored
291 <!-- По умолчанию все значения fest:value экранируются -->
292 <!--
293 Необходимо использовать fest:space или
294 fest:text для явного указания строк с пробелами
295 -->
296 </fest:template>
297 ```
298
299 Результат
300
301 ```html
302 <h1>Hello, Jack "The Ripper"</h1>
303 ```
304
305 ## Вложенные шаблоны
306
307 Данные на вход
308
309 ```javascript
310 var data = {
311 people: [
312 {name: 'John', age: 20},
313 {name: 'Mary', age: 21},
314 {name: 'Gary', age: 55}
315 ],
a54a944 @AndrewSumin improve foreach, add 'from' 'to'
AndrewSumin authored
316
e5668b7 readme
Artem Tyurin authored
317 append: '>>'
318 }
319 ```
320
321 foreach.xml (основной шаблон)
322
323 ```xml
324 <?xml version="1.0"?>
325 <fest:template xmlns:fest="http://fest.mail.ru" context_name="json">
a54a944 @AndrewSumin improve foreach, add 'from' 'to'
AndrewSumin authored
326
f8b7143 @AndrewSumin select in fest:get
AndrewSumin authored
327 <!-- Контекст можно передавать во вложенные шаблоны -->
328 <fest:include context_name="json" src="./person.xml"/>
e5668b7 readme
Artem Tyurin authored
329
f8b7143 @AndrewSumin select in fest:get
AndrewSumin authored
330 <!-- Значением iterate может быть любое js-выражение -->
a8e1acc @eprev removed support for deprecated fest:foreach
eprev authored
331 <fest:for iterate="json.people.reverse()" index="i">
a54a944 @AndrewSumin improve foreach, add 'from' 'to'
AndrewSumin authored
332
e5668b7 readme
Artem Tyurin authored
333 <!-- Передаваемые значения будут доступны в контексте params -->
334 <fest:get name="person">json.people[i]</fest:get>
a8e1acc @eprev removed support for deprecated fest:foreach
eprev authored
335 </fest:for>
e5668b7 readme
Artem Tyurin authored
336 </fest:template>
337 ```
338
339 person.xml
340
341 ```xml
342 <?xml version="1.0"?>
343 <fest:template xmlns:fest="http://fest.mail.ru" context_name="json">
a54a944 @AndrewSumin improve foreach, add 'from' 'to'
AndrewSumin authored
344
e5668b7 readme
Artem Tyurin authored
345 <!--
346 Используем set для объявления переменной,
347 которую используем в родительском шаблоне
348 -->
349 <fest:set name="person">
350 <p>
351 <fest:script><![CDATA[
352 var first = params.name[0],
353 other = params.name.slice(1);
354 ]]></fest:script>
a54a944 @AndrewSumin improve foreach, add 'from' 'to'
AndrewSumin authored
355
e5668b7 readme
Artem Tyurin authored
356 <fest:value>json.append</fest:value>
357 <strong>
358 <fest:value>first</fest:value>
359 </strong>
360 <fest:value>other</fest:value>
361 </p>
362 </fest:set>
363 </fest:template>
364 ```
365
366 Результат
367
368 ```html
369 <p>&gt;&gt;<strong>G</strong>ary</p>
370 <p>&gt;&gt;<strong>M</strong>ary</p>
371 <p>&gt;&gt;<strong>J</strong>ohn</p>
372 ```
373
374 ## Использование set и get
375
376 ```xml
377 <?xml version="1.0"?>
378 <fest:template xmlns:fest="http://fest.mail.ru" context_name="json">
379 <fest:set name="host">http://e.mail.ru</fest:set>
380 <fest:set name="all">msglist</fest:set>
381 <fest:set name="new">sentmsg?compose</fest:set>
382
383 <fest:set name="all_link">
269bf88 @pzinovkin No more cgi-bin
pzinovkin authored
384 <fest:get name="host"/>/<fest:get name="all"/>
e5668b7 readme
Artem Tyurin authored
385 </fest:set>
a54a944 @AndrewSumin improve foreach, add 'from' 'to'
AndrewSumin authored
386
e5668b7 readme
Artem Tyurin authored
387 <fest:set name="new_link">
269bf88 @pzinovkin No more cgi-bin
pzinovkin authored
388 <fest:get name="host"/>/<fest:get name="new"/>
e5668b7 readme
Artem Tyurin authored
389 </fest:set>
390
391 <ul>
392 <!-- fest:attribute добавляет параметр к родительскому тегу -->
a54a944 @AndrewSumin improve foreach, add 'from' 'to'
AndrewSumin authored
393
e5668b7 readme
Artem Tyurin authored
394 <li><a>
395 <fest:attributes>
396 <fest:attribute name="href"><fest:get name="all_link"/></fest:attribute>
397 </fest:attributes>
398 Все сообщения
399 </a></li>
a54a944 @AndrewSumin improve foreach, add 'from' 'to'
AndrewSumin authored
400
e5668b7 readme
Artem Tyurin authored
401 <li><a>
402 <fest:attributes>
403 <fest:attribute name="href"><fest:get name="new_link"/></fest:attribute>
404 </fest:attributes>
405 Написать письмо
406 </a></li>
407 </ul>
408 </fest:template>
409 ```
410
411 Результат
412
413 ```html
414 <ul>
269bf88 @pzinovkin No more cgi-bin
pzinovkin authored
415 <li><a href="http://e.mail.ru/msglist">Все сообщения</a></li>
416 <li><a href="http://e.mail.ru/sentmsg?compose">Написать письмо</a></li>
e5668b7 readme
Artem Tyurin authored
417 </ul>
269bf88 @pzinovkin No more cgi-bin
pzinovkin authored
418 ```
Something went wrong with that request. Please try again.