-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathchap-template_xhtml.lyx
3548 lines (2553 loc) · 56.2 KB
/
chap-template_xhtml.lyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#LyX 1.6.10 created this file. For more info see http://www.lyx.org/
\lyxformat 345
\begin_document
\begin_header
\textclass book
\use_default_options true
\language english
\inputencoding auto
\font_roman palatino
\font_sans helvet
\font_typewriter courier
\font_default_family default
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100
\graphics default
\paperfontsize default
\spacing single
\use_hyperref false
\papersize default
\use_geometry false
\use_amsmath 1
\use_esint 1
\cite_engine basic
\use_bibtopic false
\paperorientation portrait
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\defskip medskip
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\author ""
\author ""
\end_header
\begin_body
\begin_layout Chapter
Templates in Lift
\end_layout
\begin_layout Standard
An XHTML page, being the central component of a web application, is likewise
the central component of Lift's request processing.
In Lift, we go a step further and utilize a flexible yet powerful templating
engine that allows us to compose an XHTML
\begin_inset Index
status open
\begin_layout Plain Layout
XHTML
\end_layout
\end_inset
page not only out of one or more XML
\begin_inset Index
status open
\begin_layout Plain Layout
XML
\end_layout
\end_inset
files, but also from methods that can programmaticaly generate template
XML.
Additionally, Lift 2.2
\begin_inset Index
status open
\begin_layout Plain Layout
Lift 2.2
\end_layout
\end_inset
brings designer-friendly templates (Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:Designer-Friendly-Templates"
\end_inset
) and HTML5 support (Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:HTML5-Support"
\end_inset
).
Designer-friendly templates, in particular, can simplify working with a
designer because they allow templates to be fully valid XHTML or HTML5.
\end_layout
\begin_layout Standard
In this chapter we'll discuss template capabilities and syntax, including
built-in tags provided by Lift that perform special template processing
(Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:ArchTags"
\end_inset
).
We will also cover how you can write your own View classes, Scala code
that can programmatically generate template XML (Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:Views"
\end_inset
).
We'll finish up the chapter with some discussion on miscellaneous templating
functionality.
\end_layout
\begin_layout Section
Template XML
\begin_inset CommandInset label
LatexCommand label
name "sec:Templates"
\end_inset
\end_layout
\begin_layout Standard
Templates
\begin_inset Index
status open
\begin_layout Plain Layout
Templates
\end_layout
\end_inset
form the backbone of Lift's flexibility and power.
A template is an XML document that contains Lift-specific tags, see
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:ArchTags"
\end_inset
, as well as whatever content you want returned to the user.
\end_layout
\begin_layout Standard
\align center
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
width "75col%"
special "none"
height "1in"
height_special "totalheight"
status open
\begin_layout Plain Layout
A note on nomenclature: typically when people discuss
\begin_inset Quotes eld
\end_inset
templates
\begin_inset Quotes erd
\end_inset
in books or on the mailing list they're talking about XML files.
We'll cover programmatic generation of template XML in Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:Views"
\end_inset
.
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Lift includes several built-in XML tags for specific actions.
These utilize prefixed XML elements and are of the form
\family typewriter
<lift:tag_name/>
\family default
.
Lift also allows you to define your own tags, which are called
\emph on
snippets
\emph default
\begin_inset Index
status collapsed
\begin_layout Plain Layout
snippets
\end_layout
\end_inset
(Chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "cha:Snippets"
\end_inset
).
These user-defined tags are linked to Scala methods and these methods can
process the XML contents of the snippet tag, or can generate their own
content from scratch.
A simple template is shown in Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Sample-template"
\end_inset
.
\end_layout
\begin_layout Standard
\begin_inset listings
lstparams "language=XML"
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
A Sample Template
\begin_inset CommandInset label
LatexCommand label
name "lst:Sample-template"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
<lift:surround with="default" at="content">
\end_layout
\begin_layout Plain Layout
<head><title>Hello!</title></head>
\end_layout
\begin_layout Plain Layout
<lift:Hello.world />
\end_layout
\begin_layout Plain Layout
</lift:surround>
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Notice the tags that are of the form
\family typewriter
<lift:name>
\family default
which in this case are
\family typewriter
<lift:surround>
\family default
and
\family typewriter
<lift:snippet>.
\family default
These are two examples of Lift-specific tags.
We'll discuss all of the tags that users will use in Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:ArchTags"
\end_inset
, but let's briefly discuss the two shown here.
We use the built-in
\family typewriter
<lift:surround>
\begin_inset Index
status open
\begin_layout Plain Layout
lift:surround
\end_layout
\end_inset
\family default
tag (Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:surround-tag"
\end_inset
) to make Lift embed our current template inside the
\begin_inset Quotes eld
\end_inset
default
\begin_inset Quotes erd
\end_inset
template.
We also use
\family typewriter
<lift:snippet>
\family default
tag (aliased to Hello.world) to execute a snippet that we defined.
In this instance, we execute the method
\family typewriter
world
\family default
in the class
\family typewriter
Hello
\family default
to generate some content.
\end_layout
\begin_layout Subsection
Locating Template XML
\end_layout
\begin_layout Standard
During request processing, Lift first tries to match against the
\family typewriter
LiftRules.viewDispatch
\begin_inset Index
status open
\begin_layout Plain Layout
viewDispatch
\end_layout
\end_inset
\begin_inset Index
status open
\begin_layout Plain Layout
Views ! Explicit dispatch
\end_layout
\end_inset
\family default
function to see if an explicit View method is defined for the request.
If there isn't a
\family typewriter
viewDispatch
\family default
match, then Lift next tries to locate a file in the template directory
tree (typically in a WAR archive) that matches the request.
Lift tries several suffixes (html, xhtml, htm, and no suffix) and also
tries to match based on the client's
\family typewriter
Accept-Language
\family default
header.
The pattern Lift uses is:
\end_layout
\begin_layout LyX-Code
<path to template>[_<language tag>][.<suffix>]
\end_layout
\begin_layout Standard
Because Lift will implicitly search for suffixes, it's best to leave the
suffix off of your links within the web app.
If you have a link with an href of
\family typewriter
/test/template.xhtml
\family default
, it will only match that file, but if you use
\family typewriter
/test/template
\family default
for the href and you have the following templates in your web app:
\end_layout
\begin_layout Itemize
\family typewriter
/test/template.xhtml
\end_layout
\begin_layout Itemize
\family typewriter
/test/template_es_ES.xhtml (Spanish localized for Spain)
\end_layout
\begin_layout Itemize
\family typewriter
/test/template_ja.xhtml
\end_layout
\begin_layout Standard
then Lift will use the appropriate template based on the user's requested
language if a corresponding template is available.
For more information regarding internationalization please see Appendix
\begin_inset CommandInset ref
LatexCommand ref
reference "cha:Internationalization"
\end_inset
.
In addition to normal templates, your application can make use of hidden
templates
\begin_inset Index
status open
\begin_layout Plain Layout
hidden templates
\end_layout
\end_inset
.
These are templates that are located under the
\family typewriter
/templates-hidden
\family default
directory of your web app.
Technically, Lift hides files in any directory ending in
\begin_inset Quotes eld
\end_inset
-hidden
\begin_inset Quotes erd
\end_inset
, but templates-hidden is somewhat of a de facto standard.
Like the
\family typewriter
WEB-INF
\family default
directory, the contents cannot be directly requested by clients.
They can, however, be used by other templates through mechanisms such as
the
\family typewriter
<lift:surround>
\family default
and
\family typewriter
<lift:embed>
\family default
tags (Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:embed-tag"
\end_inset
).
If a static file can't be located then Lift will attempt to locate a View
class (Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:Views"
\end_inset
) that will process the request.
If Lift cannot locate an appropriate template based on the request path
then it will return a 404 to the user.
\end_layout
\begin_layout Subsection
Processing Template XML
\end_layout
\begin_layout Standard
Once Lift has located the correct template, the next step is to process
the contents.
It is important to understand that Lift processes XML tags recursively,
from the outermost tag to the innermost tag.
That means that in our example Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Sample-template"
\end_inset
, the surround tag gets processed first.
In this case the surround loads the default template and embeds our content
at the appropriate location.
The next tag to be processed is the
\family typewriter
<lift:Hello.world/>
\family default
snippet.
This tag is essentially an alias for the lift:snippet tag (specifically,
\family typewriter
<lift:snippet type=
\begin_inset Quotes eld
\end_inset
Hello:world
\begin_inset Quotes erd
\end_inset
>
\family default
) , and will locate the Hello class and execute the world method on it.
If you omit the
\begin_inset Quotes eld
\end_inset
method
\begin_inset Quotes erd
\end_inset
part of the type and only specify the class (
\family typewriter
<lift:Hello>
\family default
or
\family typewriter
<lift:snippet type=
\begin_inset Quotes eld
\end_inset
Hello
\begin_inset Quotes erd
\end_inset
>
\family default
), then Lift will attempt to call the
\family typewriter
render
\family default
method of the class.
\end_layout
\begin_layout Standard
To give a more complex example that illustrates the order of tag processing,
consider Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:A-Recursive-Tag"
\end_inset
.
In this example we have several nested snippet tags, starting with
\family typewriter
<A.snippet />
\family default
.
Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Recursive-Tag-Snippets"
\end_inset
shows the backing code for this example.
Snippets are covered in more detail in Chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "cha:Snippets"
\end_inset
.
\end_layout
\begin_layout Standard
\begin_inset listings
lstparams "language=XML"
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
A Recursive Tag Processing Example
\begin_inset CommandInset label
LatexCommand label
name "lst:A-Recursive-Tag"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
<lift:A.snippet>
\end_layout
\begin_layout Plain Layout
<p>Hello, <A:name />!</p>
\end_layout
\begin_layout Plain Layout
<p>
\end_layout
\begin_layout Plain Layout
<lift:B.snippet>
\end_layout
\begin_layout Plain Layout
<B:title />
\end_layout
\begin_layout Plain Layout
<lift:C.snippet />
\end_layout
\begin_layout Plain Layout
</lift:B.snippet>
\end_layout
\begin_layout Plain Layout
</p>
\end_layout
\begin_layout Plain Layout
</lift:A.snippet>
\end_layout
\end_inset
\end_layout
\begin_layout Standard
The first thing that happens is that the contents of the
\family typewriter
<lift:A.snippet>
\family default
tag are passed as a
\family typewriter
NodeSeq
\family default
argument to the
\family typewriter
A.snippet
\family default
method.
In the
\family typewriter
A.snippet
\family default
method we bind
\begin_inset Index
status open
\begin_layout Plain Layout
bind
\end_layout
\end_inset
, or replace, the
\family typewriter
<A:name />
\family default
tag with an XML Text node of
\begin_inset Quotes eld
\end_inset
The A snippet
\begin_inset Quotes erd
\end_inset
.
The rest of the input is left as-is and is returned to Lift for more processing.
Lift examines the returned
\family typewriter
NodeSeq
\family default
for more lift tags and finds the
\family typewriter
<lift:B.snippet>
\family default
tag.
The contents of the
\family typewriter
<lift:B.snippet>
\family default
tag are passed as a
\family typewriter
NodeSeq
\family default
argument to the
\family typewriter
B.snippet
\family default
method, where the
\family typewriter
<B.title />
\family default
tag is bound with the XML Text node
\begin_inset Quotes eld
\end_inset
The B snippet
\begin_inset Quotes erd
\end_inset
.
The rest of the contents are left unchanged and the transformed
\family typewriter
NodeSeq
\family default
is returned to Lift, which scans for and finds the
\family typewriter
<lift:C.snippet />
\family default
tag.
Since there are no child elements for the
\family typewriter
<lift:C.snippet />
\family default
tag, the
\family typewriter
C.snippet
\family default
method is invoked with an empty
\family typewriter
NodeSeq
\family default
and the
\family typewriter
C.snippet
\family default
returns the Text node
\begin_inset Quotes eld
\end_inset
The C snippet
\begin_inset Quotes erd
\end_inset
.
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
The Recursive Tag Snippets Code
\begin_inset CommandInset label
LatexCommand label
name "lst:Recursive-Tag-Snippets"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
...
standard Lift imports ...
\end_layout
\begin_layout Plain Layout
class A {
\end_layout
\begin_layout Plain Layout
def snippet (xhtml : NodeSeq) : NodeSeq =
\end_layout
\begin_layout Plain Layout
bind("A", xhtml, "name" -> Text("The A snippet"))
\end_layout
\begin_layout Plain Layout
}
\end_layout
\begin_layout Plain Layout
class B {
\end_layout
\begin_layout Plain Layout
def snippet (xhtml : NodeSeq) : NodeSeq =
\end_layout
\begin_layout Plain Layout
bind("B", xhtml, "title" -> Text("The B snippet"))
\end_layout
\begin_layout Plain Layout
}
\end_layout
\begin_layout Plain Layout
class C {
\end_layout
\begin_layout Plain Layout
def snippet (xhtml : NodeSeq) : NodeSeq = Text("The C snippet")
\end_layout
\begin_layout Plain Layout
}
\end_layout
\end_inset
\end_layout
\begin_layout Standard
While the contents of the
\family typewriter
A.snippet
\family default
tag are passed to the
\family typewriter
A.snippet
\family default
method, there's no requirement that the contents are actually used.
For example, consider what would happen if we swapped the B and C snippet
tags in our template, as shown in Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:The-Swapped-Recursive"
\end_inset
.
In this example, the
\family typewriter
C.snippet
\family default
method is called before the
\family typewriter
B.snippet
\family default
method.
Since our
\family typewriter
C.snippet
\family default
method returns straight XML that doesn't contain the B snippet tag, the
B snippet will never be executed! We'll cover how the
\family typewriter
eager_eval
\family default
tag attribute can be used to reverse this behavior in Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:Eager-Eval"
\end_inset
.
\end_layout
\begin_layout Standard
\begin_inset listings
lstparams "language=XML"
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
The Swapped Recursive Snippet Template
\begin_inset CommandInset label
LatexCommand label
name "lst:The-Swapped-Recursive"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
<lift:A.snippet>
\end_layout
\begin_layout Plain Layout
<p>Hello, <A:name />!</p>
\end_layout
\begin_layout Plain Layout
<p>
\end_layout
\begin_layout Plain Layout
<lift:C.snippet>
\end_layout
\begin_layout Plain Layout
<lift:B.snippet>
\end_layout
\begin_layout Plain Layout
<B:title />
\end_layout