A class that can be used to easily script and render HTML with.
A class that can be used to easily render HTML with. An instance of this class could be one of a traditional web application's View. (From the Model-View-Controller or MVC pattern.)
Some traditional web applications use a templating language to render their final output using a set of variables. Some templating language examples: Blade, Handlebars, Razor, etc. Some of these have features like for loops, ifs, they can even support method calls. This class implements similar functinality, albeit in a different manner.
The render control logic should be implemented by overriding [method _render], variable substitutions can be done using the available helper methods.
This class uses a templating syntax like: [code]{{ EXPRESSION }}[/code]. Everything inbetween [code]{{ }}[/code] is an expression that the system will replace during rendering with actual values from a passed in Dictionary.
Assuming you pass in the following Dictionary to [method render_template]:
[code]var d : Dictionary = {
"var": VALUE,
"var1": VALUE,
"var2": VALUE,
"var_dict": {
"x": VALUE
},
"var_arr": [
VALUE,
]
}[/code]
Supported expressions:
0. To escape [code]{{[/code] use: [code]{\{[/code]. [code]{\\{[/code] will turn into [code]{\{[/code] etc.
1. [code]p(var)[/code] - print, escaped, also includes to string cast.
2. [code]{{ var }}[/code] - equivalent to [code]p(var)[/code].
3. [code](var)[/code] - equivalent to [code]p(var)[/code].
4. [code]pr(var)[/code] - print_raw, not escaped, also includes a cast to string.
5. [code]pb(var)[/code] - print_newline_to_br, escaped, turns newlines into br tags, also includes a to string cast.
6. [code]prb(var)[/code] - print_raw_newline_to_br, not escaped, turns newlines into br tags, also includes a to string cast.
7. [code]vf("%d %d", var1, var2)[/code] - vformat or snprintf or String fromatting using % in gdscript.
8. [code]p(var_arr[0])[/code] - Array indexing.
9. [code]p(var_dict["x"]), p(var_dict[x]), p(var_dict['x'])[/code] - Dictionary indexing.
10. [code]p(var1, var2)[/code] - All methods supports multiple arguments.
11. Let's say [code]var1[/code] is a [Vector3], [code]var["x"][/code] and [code]var[0][/code] and [code]var[x][/code] will also work.
12. Indexing will also call [code]get()[/code] on [Object]s.
Not supported:
0. [code]p(var[var[var[2]]])[/code] Recursive indexing.
1. No actual method calls. Make everything available that your templates need in your controller class!
So this is how your HTML will look like: [code]... HTML markup ... {{ p(var) }} ... {{ pr(var) }} ... {{ pb(var) }} ... {{ prb(var, var2) }} ... {{ vf("%d %d", var1, var2) }} ... etc.[/code]
Note: For a different approach to generating HTML, you can also take a look at [HTMLBuilder].
Override this method to implement your own rendering for any given HTMLTemplate.
Adds a [HTMLTemplateData] template.
Turns an [Array] of Variants into [String] using the given method.
Clears template defaults.
Clears template overrides.
Clears templates.
Gets a template string using [method get_template] and does method substitutions on it.
Gets a template.
Returns the template count.
Returns a default template value previously set.
Returns an override template value previously set.
Returns a template text identified by name. This method first looks into overrides, then it tries to get it from [member templates] (in order), and then it tries default values.
Returns whether a template default denoted by name is set.
Returns whether a template override denoted by name is set.
Processes an expression in a template. An expression in the template look like: [code]{{ EXPRESSION }}[/code].
Processes one variable from an expression in a template. An expression in the template look like: [code]{{ EXPRESSION }}[/code].
Removes a template.
Removes a template default.
Removes a template override.
Use this method to render the final output. This calls [method _render].
Soes does variable substitutions on the given template text.
Sets a template default value.
Sets a template override value.
Returns all template default values.
Returns all template override values.
Returns all templates.
xml_excape() and add value to the final string.
Add value to the final string.
xml_excape() and add value to the final string. This also converts newlines into br html tags.
Add value to the final string. This also converts newlines into br html tags.
vformat the given string, using it's passed variables.