11package app .views .pages ;
22
3+ import app .views .MainView ;
34import java .util .Arrays ;
45import java .util .List ;
5-
6- import app .views .MainView ;
7-
8- import static app .views .Partials .*;
9- import static j2html .TagCreator .*;
6+ import static app .views .Partials .codeSnippet ;
7+ import static app .views .Partials .javaComparison ;
8+ import static j2html .TagCreator .a ;
9+ import static j2html .TagCreator .attrs ;
10+ import static j2html .TagCreator .each ;
11+ import static j2html .TagCreator .em ;
12+ import static j2html .TagCreator .fileAsEscapedString ;
13+ import static j2html .TagCreator .h2 ;
14+ import static j2html .TagCreator .p ;
15+ import static j2html .TagCreator .section ;
16+ import static j2html .TagCreator .table ;
17+ import static j2html .TagCreator .tbody ;
18+ import static j2html .TagCreator .td ;
19+ import static j2html .TagCreator .text ;
20+ import static j2html .TagCreator .tr ;
1021
1122public class ExamplesView {
1223 private static List <Integer > numbers = Arrays .asList (1 , 2 , 3 , 4 , 5 , 6 , 7 , 9 , 10 );
@@ -16,93 +27,93 @@ public static String render() {
1627 "Examples of how to use j2html" ,
1728 "Reclaim control of your HTML" ,
1829 section (attrs ("#examples" ),
19- h2 (attrs ("#basic-example" ), "Basic example" ),
20- p ("Creating a basic HTML structure in j2html is pretty similar to plain HTML. This Java code:" ),
21- javaComparison ("basic" ),
22- p ("Becomes this HTML:" ),
23- codeSnippet ("markup" , fileAsEscapedString ("/codeExamples/basic.html" )),
24- p (
25- text (
26- "It's literally impossible to forget to close a div, mistype an attribute name, or forget an attribute quote! " +
27- "Remember to include the Java wrapping code though, j2html is not a template language, all files are .java. " +
28- "To see how the wrapping code could look, check out the "
29- ),
30- a ("getting started example" ).withHref ("/" ),
31- text ("." )
30+ h2 (attrs ("#basic-example" ), "Basic example" ),
31+ p ("Creating a basic HTML structure in j2html is pretty similar to plain HTML. This Java code:" ),
32+ javaComparison ("basic" ),
33+ p ("Becomes this HTML:" ),
34+ codeSnippet ("markup" , fileAsEscapedString ("/codeExamples/basic.html" )),
35+ p (
36+ text (
37+ "It's literally impossible to forget to close a div, mistype an attribute name, or forget an attribute quote! " +
38+ "Remember to include the Java wrapping code though, j2html is not a template language, all files are .java. " +
39+ "To see how the wrapping code could look, check out the "
3240 ),
41+ a ("getting started example" ).withHref ("/" ),
42+ text ("." )
43+ ),
3344
34- h2 (attrs ("#core-concepts" ), "Core concepts" ),
35- codeSnippet ("java" , fileAsEscapedString ("/codeExamples/coreConcepts.java" )),
45+ h2 (attrs ("#core-concepts" ), "Core concepts" ),
46+ codeSnippet ("java" , fileAsEscapedString ("/codeExamples/coreConcepts.java" )),
3647
37- h2 (attrs ("#loops" ), "Loops, each() and filter()" ),
38- p ("Using Java 8's lambda syntax, you can write loops (via streams) inside your HTML-builder:" ),
39- javaComparison ("forLoopLambda" ),
48+ h2 (attrs ("#loops" ), "Loops, each() and filter()" ),
49+ p ("Using Java 8's lambda syntax, you can write loops (via streams) inside your HTML-builder:" ),
50+ javaComparison ("forLoopLambda" ),
4051
41- p ("j2html also offers a custom each method, which is slightly more powerful:" ),
42- javaComparison ("each" ),
52+ p ("j2html also offers a custom each method, which is slightly more powerful:" ),
53+ javaComparison ("each" ),
4354
44- p ("If you need to filter your collection, j2html has a built in filter function too:" ),
45- javaComparison ("filter" ),
55+ p ("If you need to filter your collection, j2html has a built in filter function too:" ),
56+ javaComparison ("filter" ),
4657
47- p (
48- "Since this is pure Java, all the Employee methods (getName, getImgPath, getTitle) are available to you, " +
49- "and you get autocomplete suggestions and compile time errors."
50- ),
51- p ("Given three random employees, all the above approaches would give the same HTML:" ),
52- codeSnippet ("markup" , fileAsEscapedString ("/codeExamples/forLoop.html" )),
58+ p (
59+ "Since this is pure Java, all the Employee methods (getName, getImgPath, getTitle) are available to you, " +
60+ "and you get autocomplete suggestions and compile time errors."
61+ ),
62+ p ("Given three random employees, all the above approaches would give the same HTML:" ),
63+ codeSnippet ("markup" , fileAsEscapedString ("/codeExamples/forLoop.html" )),
5364
54- h2 (attrs ("#2d-table-example" ), "Two dimensional table example" ),
55- javaComparison ("2dTable" ),
56- p ("The code above is generating this table:" ),
57- table (attrs ("#table-example" ),
58- tbody (
59- each (numbers , i -> tr (
60- each (numbers , j -> td (
61- String .valueOf (i * j )
62- ))
63- ))
64- )
65- ),
65+ h2 (attrs ("#2d-table-example" ), "Two dimensional table example" ),
66+ javaComparison ("2dTable" ),
67+ p ("The code above is generating this table:" ),
68+ table (attrs ("#table-example" ),
69+ tbody (
70+ each (numbers , i -> tr (
71+ each (numbers , j -> td (
72+ String .valueOf (i * j )
73+ ))
74+ ))
75+ )
76+ ),
6677
67- h2 (attrs ("#partials" ), "Partials" ),
68- p ("You can create partials for elements you use a lot:" ),
69- javaComparison ("partial" ),
70- p ("The equivalent HTML would be:" ),
71- codeSnippet ("markup" , fileAsEscapedString ("/codeExamples/partial.html" )),
72- p ("You can then use these partials, for example in a registration form:" ),
73- javaComparison ("view" ),
74- p ("Pretty clean, right? The rendered HTML is more verbose:" ),
75- codeSnippet ("markup" , fileAsEscapedString ("/codeExamples/view.html" )),
76- p (
77- text ("Imagine if you wanted labels in addition. The Java snippet would look almost identical: You could create a partial called" ),
78- em (" passwordAndLabel() " ),
79- text (
80- "and nothing but the method name would change. The resulting HTML however, would be twice or thrice as big, " +
81- "depending on whether or not you wrapped the input and label in another tag."
82- )
83- ),
78+ h2 (attrs ("#partials" ), "Partials" ),
79+ p ("You can create partials for elements you use a lot:" ),
80+ javaComparison ("partial" ),
81+ p ("The equivalent HTML would be:" ),
82+ codeSnippet ("markup" , fileAsEscapedString ("/codeExamples/partial.html" )),
83+ p ("You can then use these partials, for example in a registration form:" ),
84+ javaComparison ("view" ),
85+ p ("Pretty clean, right? The rendered HTML is more verbose:" ),
86+ codeSnippet ("markup" , fileAsEscapedString ("/codeExamples/view.html" )),
87+ p (
88+ text ("Imagine if you wanted labels in addition. The Java snippet would look almost identical: You could create a partial called" ),
89+ em (" passwordAndLabel() " ),
90+ text (
91+ "and nothing but the method name would change. The resulting HTML however, would be twice or thrice as big, " +
92+ "depending on whether or not you wrapped the input and label in another tag."
93+ )
94+ ),
8495
85- h2 (attrs ("#dynamic-views" ), "Dynamic views" ),
86- p (
87- "Once you've set up partials, you can call them from wherever, which greatly reduces potential errors. " +
88- "Let's say we want to include the form from the partials-example in our webpage. " +
89- "We want a header above and a footer below. A lot of templating languages make you do this: "
90- ),
91- codeSnippet ("java" , fileAsEscapedString ("/codeExamples/otherTemplates.vm" )),
92- p (
93- "This is a pain to work with. You have no idea what the header and footer expects, and you have no way to affect how they treat your content. " +
94- "You can easily break the site by forgetting to close divs, or by forgetting to include either the header or the footer in one of your views. " +
95- "In j2html you can specify the context in which a view is rendered, and supply the rendering method with type safe parameters! " +
96- "If we want to insert our form in a header/footer frame, we simply create a MainView and make it take our view as an argument:"
97- ),
96+ h2 (attrs ("#dynamic-views" ), "Dynamic views" ),
97+ p (
98+ "Once you've set up partials, you can call them from wherever, which greatly reduces potential errors. " +
99+ "Let's say we want to include the form from the partials-example in our webpage. " +
100+ "We want a header above and a footer below. A lot of templating languages make you do this: "
101+ ),
102+ codeSnippet ("java" , fileAsEscapedString ("/codeExamples/otherTemplates.vm" )),
103+ p (
104+ "This is a pain to work with. You have no idea what the header and footer expects, and you have no way to affect how they treat your content. " +
105+ "You can easily break the site by forgetting to close divs, or by forgetting to include either the header or the footer in one of your views. " +
106+ "In j2html you can specify the context in which a view is rendered, and supply the rendering method with type safe parameters! " +
107+ "If we want to insert our form in a header/footer frame, we simply create a MainView and make it take our view as an argument:"
108+ ),
98109
99- javaComparison ("main" ),
100- p ("Which will result in the rendered HTML:" ),
101- codeSnippet ("markup" , fileAsEscapedString ("/codeExamples/main.html" )),
102- p (
103- "We would now get a compilation error if we forgot to include a title, and there is 0 chance of forgetting either header or footer, mistyping paths" +
104- ", forgetting to close divs, or anything else."
105- )
110+ javaComparison ("main" ),
111+ p ("Which will result in the rendered HTML:" ),
112+ codeSnippet ("markup" , fileAsEscapedString ("/codeExamples/main.html" )),
113+ p (
114+ "We would now get a compilation error if we forgot to include a title, and there is 0 chance of forgetting either header or footer, mistyping paths" +
115+ ", forgetting to close divs, or anything else."
116+ )
106117
107118 )
108119 );
0 commit comments