0#import "Basic";
2html_template :: #import "htmltemplate";
5/**
6 jai 02_with_comments.jai -quiet && ./02_with_comments
7*/
10/** Below is a template. This would you read from a file normally, but
11 this is up to you. */
13YOUR_TEMPLATE_FILE :: #string STR_END
14<!DOCTYPE html>
15<html>
16<head>
17 <meta charset="UTF-8">
18 <meta name="viewport" content="width=device-width, initial-scale=1.0">
20 <title>{{ :page_title: }}</title>
21 <style>
22 body {
23 background-color: {{ :bg_color: }};
24 color: {{ :font_color: }};
25 font-family: Verdana, Geneva, Tahoma, sans-serif;
26 }
27 </style>
28</head>
29<body>
31 <h1>{{ :article_title: % % }}</h1>
33 <p>This demonstrates a {{ :example1: % % }}</p>
35 <p>{{ :example2: <a href="%" target="%">This</a> demonstrates % % }}</p>
37 <p>{{ :example3: Or <b>%1</b> numbered <i>%2</i> that can be reused: <b>%1</b> }}</p>
39 <p>Lets demonstrate looping!</p>
41 <ul>
42{{ loop:example4: <li><a href="#%1" %2>%1</a> %3</li> }}
43 </ul>
45</body>
46</html>
47STR_END;
50main :: () {
51 using html_template;
53 /** Now lets create some data, so we can populate the placeholders with it.
54 For that, we need an dynamic array, which stores our data: */
56 queue_action: [..]Action;
58 /** If you keep this exact name you don't have to pass it later to the
59 `commit()` proc, since one of its overloads is a macro.
60 But you can always chose a different name for your array and pass
61 it directly to the `commit()` proc if you want.
63 Now lets create the data for the placeholders: */
65 commit(
66 id = "page_title",
67 value = "Example 01",
68 );
70 commit(
71 id = "bg_color",
72 value = "#323240",
73 );
75 commit(
76 id = "font_color",
77 value = "#DFDFFF",
78 );
80 // You can pass multiple values as static array
81 commit(
82 id = "article_title",
83 value = .[ "Example", "01" ],
84 );
86 // Or via variadic procs
87 commit("example1", "simple", "replacement");
89 commit("example2",
90 "#some_path",
91 "_self",
92 "positional",
93 "placeholders"
94 );
96 commit("example3",
97 "even",
98 "placeholders"
99 );
101 // This identifier `example4` is a loop. The amount of iterations
102 // is defined by the amount of sub-arrays:
103 commit("example4", .[
104 .[ "articles", "target=\"_self\"", "to read" ],
105 .[ "about", "target=\"_self\"", "me" ],
106 .[ "books", "target=\"_self\"", "" ],
107 .[ "extern", "", "website" ],
108 ]);
111 /** Now we can generate the html file.
112 We have to declare if the template if from a raw string or file(path).
113 In this case we're reading the template from a string, so we pass `.STRING`.
115 If you omit the very last parameter, like in this case, it does not write
116 the generated content into a file.
117 */
119 success, // If something went wrong, it's indicated here.
120 html_string, // Your output as string. (You have to free it).
121 exit_code, // More needed for debugging, consult the `lib.jai` for more information.
122 error_message // This returns the exact error message if !success. (You have to free it)
123 := generate(queue_action, YOUR_TEMPLATE_FILE, .STRING);
125 defer {
126 free(html_string);
127 free(error_message);
128 }
130 if !success {
131 log_error(error_message);
132 return;
133 }
135 log("%", html_string);
138 #if false {
139 #import "File";
140 write_entire_file("01_example.html", html_string);
141 }
143 /** If you want to know, what happens if you supply wrong paramaters,
144 visit `test/test.jai`. It contains several test cases with brief
145 explainations. */
147}
index : htmltemplate
---