Kitamura
Kitamura is an HTML templating engine for rendering HTML based on placeholders defined in your HTML. Placeholders are provided in the form of content parameters that are in JSON.
Overview
Below is the general idea of how to use Kitamura. Formatting of the input HTML is respected, and overall there are no expectations that anything but what Kitamura is looking for will be modified.
Kitamura will return a Result, which will contain either the rendered HTML, or an error message.
Input HTML
Hello ${first_name}!
Input Data
{
"first_name": "Joel"
}
Output HTML
Hello Joel!
Features
Variables
Lists
Examples
use std::collections::HashMap;
use kitamura::render_template;
use serde_json::json;
let input_html = "Hello ${first_name}!";
let mut input_data = HashMap::new();
input_data.insert("first_name".to_string(), json!("Joel"));
let output_html = render_template(input_html.to_string(), input_data);
assert_eq!(output_html, "Hello Joel!");
use std::collections::HashMap;
use kitamura::render_template;
use serde_json::json;
let input_html =
"
{#for fruit in fruits#}
${fruit.name}
${fruit.colour}
${fruit.weight}
{#endfor#}
";
let mut input_data = HashMap::new();
input_data.insert(
fruits".to_string(),
son!([{"name": "Lemon", "colour": "Yellow", "weight": "150g"},
"name": "shiikuwasha", "colour": "Green", "weight": "80g"},
"name": "Lychee", "colour": "Red", "weight": "50g"}]),
;
let output_html = render_template(input_html.to_string(), input_data);
assert_eq!(output_html,
"
Lemon
Yellow
150g
shiikuwasha
Green
80g
Lychee
Red
50g
");
use std::collections::HashMap;
use kitamura::render_template;
use serde_json::json;
let input_html =
"<html>
<ul>
{#for continent in continents#}
<li>${continent.name}</li>
<ul>
{#for country in continent.countries#}
<li>${country.name}</li>
<ul>
{#for city in country.cities#}
<li>
<ul>
<li>${city.name}</li>
<li>${city.time}</li>
<li>${city.tempurature}</li>
<ul>
</li>
{#endfor#}
</ul>
{#endfor#}
</ul>
{#endfor#}
</ul>
</html>";
let mut input_data = HashMap::new();
input_data.insert(
"continents".to_string(),
json!([{"name":"Oceania","countries":[{"name":"Australia","cities":[{"name":"Brisbane","time":"9:26PM","tempurature":"14C"},{"name":"Melbourne","time":"9:26PM","tempurature":"14C"},{"name":"Adelaide","time":"8:56PM","tempurature":"15C"}]},{"name":"New Zealand","cities":[{"name":"Wellington","time":"11:26PM","tempurature":"12C"}]}]},{"name":"Europe","countries":[{"name":"England","cities":[{"name":"Manchester","time":"12:26PM","tempurature":"16C"},{"name":"London","time":"12:26PM","tempurature":"23C"}]}]}]),
);
let expected_rendered_output = "<html>
<ul>
<li>Oceania</li>
<ul>
<li>Australia</li>
<ul>
<li>
<ul>
<li>Brisbane</li>
<li>9:26PM</li>
<li>14C</li>
<ul>
</li>
<li>
<ul>
<li>Melbourne</li>
<li>9:26PM</li>
<li>14C</li>
<ul>
</li>
<li>
<ul>
<li>Adelaide</li>
<li>8:56PM</li>
<li>15C</li>
<ul>
</li>
</ul>
<li>New Zealand</li>
<ul>
<li>
<ul>
<li>Wellington</li>
<li>11:26PM</li>
<li>12C</li>
<ul>
</li>
</ul>
</ul>
<li>Europe</li>
<ul>
<li>England</li>
<ul>
<li>
<ul>
<li>Manchester</li>
<li>12:26PM</li>
<li>16C</li>
<ul>
</li>
<li>
<ul>
<li>London</li>
<li>12:26PM</li>
<li>23C</li>
<ul>
</li>
</ul>
</ul>
</ul>
</html>";
let output_html = render_template(input_html.to_string(), input_data).unwrap();
assert_eq!(output_html, expected_rendered_output);