# Kitamura 北村
[![Actions](https://github.com/JoelWi/kitamura/actions/workflows/main.yml/badge.svg)](https://github.com/JoelWi/kitamura/actions/workflows/main.yml)
[![Coverage Status](https://coveralls.io/repos/github/JoelWi/kitamura/badge.svg?branch=main)](https://coveralls.io/github/JoelWi/kitamura?branch=main)
Kitamura is a templating engine for rendering templates based on placeholders
defined in your templates. 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 template
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 template, or
an error message.
```text
Input HTML
Hello ${first_name}!
Input Data
{
"first_name": "Joel"
}
Output HTML
Hello Joel!
```
# Features
Variables
Lists
Conditionals - builtins:
- ==
- !=
- ?exists
- ?not_empty
- ?contains('some substring value')
# 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 of 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,
"
");
```
```
use std::collections::HashMap;
use kitamura::render_template;
use serde_json::json;
let input_html =
"
{#for continent of continents#}
- ${continent.name}
{#for country of continent.countries#}
- ${country.name}
{#for city of country.cities#}
-
- ${city.name}
- ${city.time}
- ${city.tempurature}
{#endfor#}
{#endfor#}
{#endfor#}
";
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 = "
";
let output_html = render_template(input_html.to_string(), input_data).unwrap();
assert_eq!(output_html, expected_rendered_output);
```
```
use std::collections::HashMap;
use kitamura::render_template;
use serde_json::json;
let input_html = "Hello{#if first_name?exists && first_name?not_empty #} ${first_name}{#endif#}!".to_owned();
let params = HashMap::from([("first_name".to_owned(), serde_json::json!("Joel"))]);
let expected_rendered_output = "Hello Joel!".to_owned();
let output_html = render_template(input_html, params).unwrap();
assert_eq!(output_html, expected_rendered_output);
```
```
use std::collections::HashMap;
use kitamura::render_template;
use serde_json::json;
let input_html = "Hello{#if first_name?exists && first_name?not_empty #} ${first_name}{#endif#}!".to_owned();
let params = HashMap::from([("first_name".to_owned(), serde_json::json!(""))]);
let expected_rendered_output = "Hello!".to_owned();
let output_html = render_template(input_html, params).unwrap();
assert_eq!(output_html, expected_rendered_output);
```
```
use std::collections::HashMap;
use kitamura::render_template;
use serde_json::json;
let input_html = "Hello{#if first_name?exists && first_name?not_empty && last_name?exists #} ${first_name}{#endif#}!".to_owned();
let params = HashMap::from([("first_name".to_owned(), serde_json::json!("Joel"))]);
let expected_rendered_output = "Hello!".to_owned();
let output_html = render_template(input_html, params).unwrap();
assert_eq!(output_html, expected_rendered_output);
```
```
use std::collections::HashMap;
use kitamura::render_template;
use serde_json::json;
let input_html = "Hello{#if (first_name?exists && first_name?not_empty) || (last_name?exists && last_name?not_empty) #} inner body{#endif#}!".to_owned();
let params = HashMap::from([("first_name".to_owned(), serde_json::json!("Joel"))]);
let expected_rendered_output = "Hello inner body!".to_owned();
let output_html = render_template(input_html, params).unwrap();
assert_eq!(output_html, expected_rendered_output);
```