You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
HTTP-RPC is an open-source framework for creating and consuming RESTful and REST-like web services in Java. It is extremely lightweight and requires only a Java runtime environment and a servlet container. The entire framework is less than 100KB in size, making it an ideal choice for applications where a minimal footprint is desired.
5
+
HTTP-RPC is an open-source framework for creating and consuming RESTful and REST-like web services in Java. It is extremely lightweight and requires only a Java runtime environment and a servlet container. The entire framework is less than 90KB in size, making it an ideal choice for applications where a minimal footprint is desired.
6
6
7
7
This guide introduces the HTTP-RPC framework and provides an overview of its key features.
Each of these marker types is discussed in more detail below.
12
13
13
-
## Variable Markers
14
+
## Variables
14
15
Variable markers inject a value from the data dictionary into the output. For example:
15
16
16
17
```html
@@ -19,13 +20,11 @@ Variable markers inject a value from the data dictionary into the output. For ex
19
20
<p>Average: {{average}}</p>
20
21
```
21
22
22
-
Variable names represent keys into the data dictionary. When the template is processed, the markers are replaced with the corresponding values from the dictionary.
23
+
Variable names represent keys into the data dictionary. When the template is processed, the markers are replaced with the corresponding values from the dictionary. If a variable value is not defined (i.e. is missing or `null`), it is excluded from the generated output.
23
24
24
-
Nested values can be referred to using path notation; e.g. "name.first". Missing (i.e. `null`) values are replaced with the empty string in the generated output.
25
+
Nested values can be referred to using path notation; e.g. "name.first". Variable names beginning with "@" represent resource references and are obtained from the resource bundle when the template is processed. Variable names beginning with "$" represent context references and are obtained from the template context.
25
26
26
-
Variable names beginning with "@" represent resource references, and are replaced with the corresponding values from the resource bundle when the template is processed.
27
-
28
-
Variable names beginning with "$" represent context references, and are replaced with the corresponding values from the template context when the template is processed.
27
+
The reserved "~" and "." variable names represent key and value references, respectively, and are discussed in more detail below.
29
28
30
29
### Modifiers
31
30
Modifiers are used to transform a variable's representation before it is written to the output stream; for example, to apply an escape sequence.
@@ -86,41 +85,39 @@ Date/time values may be represented by one of the following:
86
85
* an instance of `java.util.Date`
87
86
* an instance of `java.util.time.TemporalAccessor`
88
87
89
-
## Section Markers
90
-
Section markers define a repeating section of content. The marker name must refer to a traversable sequence of elements in the data dictionary (specifically, an instance of either `java.lang.Iterable` or `java.util.Map`). Missing or empty sequences are ignored.
91
-
92
-
Content between the markers is repeated once for each element in the sequence. The element provides the data dictionary for each successive iteration through the section.
88
+
## Repeating Sections
89
+
Repeating section markers define a section of content that is repeated once for every element in a sequence of values. The marker name must refer to an instance of either `java.lang.Iterable` or `java.util.Map` in the data dictionary. The elements of the sequence provide the data dictionaries for successive iterations through the section. Missing or empty sequences are ignored.
93
90
94
91
For example, a data dictionary that contains information about homes for sale might look like this:
95
92
96
93
```json
97
94
{
98
95
"properties": [
99
96
{
100
-
"streetAddress": "17 Cardinal St.",
101
-
"listPrice": 849000,
97
+
"streetAddress": "27 Crescent St.",
98
+
"listPrice": 925000,
102
99
"numberOfBedrooms": 4,
103
100
"numberOfBathrooms": 3
104
101
},
105
102
{
106
-
"streetAddress": "72 Wedgemere Ave.",
107
-
"listPrice": 1650000,
108
-
"numberOfBedrooms": 5,
109
-
"numberOfBathrooms": 3
103
+
"streetAddress": "390 North Elm St.",
104
+
"listPrice": 7650000,
105
+
"numberOfBedrooms": 3,
106
+
"numberOfBathrooms": 1.5
110
107
},
111
108
...
112
109
]
113
110
}
114
111
```
115
112
116
-
A template to transform these results into HTML is shown below. The section markers are enclosed in HTML comments so they will be ignored by syntax-aware text editors, and will simply resolve to empty comment blocks when the template is processed. The HTML encoding modifier is applied to the string values to ensure that the generated output is properly escaped:
113
+
A template to transform these results into HTML is shown below. The section markers are enclosed in HTML comments so they will be ignored by syntax-aware text editors, and will simply resolve to empty comment blocks when the template is processed:
117
114
118
115
```html
119
116
<table>
120
117
<!-- {{#properties}} -->
121
118
<tr>
122
-
<td>{{streetAddress:^html}}</td>
123
-
<td>{{listPrice:format=currency:^html}}</td>
119
+
<td>{{streetAddress}}</td>
120
+
<td>{{listPrice:format=currency}}</td>
124
121
<td>{{numberOfBedrooms}}</td>
125
122
<td>{{numberOfBathrooms}}</td>
126
123
</tr>
@@ -131,18 +128,20 @@ A template to transform these results into HTML is shown below. The section mark
131
128
### Separators
132
129
Section markers may specify an optional separator string that will be automatically injected between the section's elements. The separator text is enclosed in square brackets immediately following the section name.
133
130
134
-
For example, the elements of the "addresses" section specified below will be separated by a comma in the generated output:
131
+
For example, the elements of the "names" section specified below will be separated by a comma in the generated output:
135
132
136
133
```
137
-
{{#addresses[,]}}
134
+
{{#names[,]}}
138
135
...
139
-
{{/addresses}}
136
+
{{/names}}
140
137
```
141
138
142
139
### Key and Value References
143
-
When traversing the contents of a `Map` instance, the "~" variable can be used to refer to the key associated with the current element. Additionally, when a sequence element is not an instance of `Map` (for example, a `Number`, `String`, or `Iterable`), the "." variable can be used to refer to the value of the element itself.
140
+
In most cases, variable names are used to refer to properties of the `Map` instance representing the current data dictionary. However, when traversing the contents of a `Map` sequence, the reserved "~" variable can be used to refer to the key associated with the current element.
141
+
142
+
Additionally, when traversing any type of sequence (`Iterable` or `Map`), if the current element is not a `Map` (for example, a `Number`, `String`, or `Iterable`), the "." variable can be used to refer to the value of the element itself.
144
143
145
-
For example, the following data dictionary associates number names with numeric values:
144
+
For example, the following data dictionary associates a set of number names with their corresponding numeric values:
146
145
147
146
```json
148
147
{
@@ -154,12 +153,32 @@ For example, the following data dictionary associates number names with numeric
154
153
}
155
154
```
156
155
157
-
This simple template could be used to generate a comma-separated list of name/value pairs from the data dictionary:
156
+
This template could be used to generate a comma-separated list of name/value pairs from the data dictionary:
158
157
159
158
```
160
159
{{#numbers[,]}}{{~}}:{{.}}{{/numbers}}
161
160
```
162
161
162
+
## Conditional Sections
163
+
Conditional section markers define a section of content that is only rendered if the named value exists in the data dictionary. When the value exists, it is used as the data dictionary for the section. If the value does not exist or is `null`, the section is excluded from the output.
164
+
165
+
For example, given the following data dictionary:
166
+
167
+
```json
168
+
{
169
+
"name": {
170
+
"first": "John",
171
+
"last": "Smith"
172
+
}
173
+
}
174
+
```
175
+
176
+
the content of "name" section in the following template would be included in the generated output, but the content of the "age" section would not:
177
+
178
+
```
179
+
{{?name}}{{last}}, {{first}}{{/name}}{{?age}}, age {{.}}{{/age}}
180
+
```
181
+
163
182
## Includes
164
183
Include markers import content defined by another template. They can be used to create reusable content modules; for example, document headers and footers.
0 commit comments