spooder 4.5.7 → 4.5.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -0
- package/package.json +1 -1
- package/src/api.ts +20 -1
package/README.md
CHANGED
|
@@ -1215,6 +1215,16 @@ await parse_template('Hello {$world}', replacer);
|
|
|
1215
1215
|
</html>
|
|
1216
1216
|
```
|
|
1217
1217
|
|
|
1218
|
+
`parse_template` supports optional scopes with the following syntax.
|
|
1219
|
+
|
|
1220
|
+
```html
|
|
1221
|
+
{$if:foo}I love {$foo}{/if}
|
|
1222
|
+
```
|
|
1223
|
+
Contents contained inside an `if` block will be rendered providing the given value, in this case `foo` is truthy in the substitution table.
|
|
1224
|
+
|
|
1225
|
+
An `if` block is only removed if `drop_missing` is `true`, allowing them to persist through multiple passes of a template.
|
|
1226
|
+
|
|
1227
|
+
|
|
1218
1228
|
`parse_template` supports looping arrays with the following syntax.
|
|
1219
1229
|
|
|
1220
1230
|
```html
|
package/package.json
CHANGED
package/src/api.ts
CHANGED
|
@@ -173,6 +173,25 @@ export async function parse_template(template: string, replacements: Replacement
|
|
|
173
173
|
}
|
|
174
174
|
i += loop_content.length + 6;
|
|
175
175
|
}
|
|
176
|
+
} else if (buffer.startsWith('if:')) {
|
|
177
|
+
const if_key = buffer.substring(3);
|
|
178
|
+
const if_content_start_index = i + 1;
|
|
179
|
+
const if_close_index = template.indexOf('{/if}', if_content_start_index);
|
|
180
|
+
|
|
181
|
+
if (if_close_index === -1) {
|
|
182
|
+
if (!drop_missing)
|
|
183
|
+
result += '{$' + buffer + '}';
|
|
184
|
+
} else {
|
|
185
|
+
const if_content = template.substring(if_content_start_index, if_close_index);
|
|
186
|
+
const condition_value = is_replacer_fn ? await replacements(if_key) : replacements[if_key];
|
|
187
|
+
|
|
188
|
+
if (!drop_missing) {
|
|
189
|
+
result += '{$' + buffer + '}' + if_content + '{/if}';
|
|
190
|
+
} else if (condition_value) {
|
|
191
|
+
result += await parse_template(if_content, replacements, drop_missing);
|
|
192
|
+
}
|
|
193
|
+
i += if_content.length + 5;
|
|
194
|
+
}
|
|
176
195
|
} else {
|
|
177
196
|
const replacement = is_replacer_fn ? await replacements(buffer) : replacements[buffer];
|
|
178
197
|
if (replacement !== undefined)
|
|
@@ -797,7 +816,7 @@ export function serve(port: number, hostname?: string) {
|
|
|
797
816
|
}
|
|
798
817
|
});
|
|
799
818
|
|
|
800
|
-
log('server started on port {%d}', port);
|
|
819
|
+
log('server started on port {%d} (host: {%s})', port, hostname ?? 'unspecified');
|
|
801
820
|
|
|
802
821
|
return {
|
|
803
822
|
/** Register a handler for a specific route. */
|