8 个版本

0.2.0 2024年1月26日
0.1.7 2023年10月1日
0.1.5 2023年9月29日

103模板引擎

每月下载 41

MIT 许可证

52KB
918

Kitamura 北村

Actions Coverage Status

Kitamura 是一个基于您模板中定义的占位符来渲染模板的模板引擎。占位符以 JSON 格式的内容参数形式提供。

概述

以下是如何使用 Kitamura 的一般思路。模板的格式将被尊重,总体上不会对任何除了 Kitamura 寻找的内容之外的修改有期望。

Kitamura 将返回一个 Result,其中将包含已渲染的模板或错误消息。

Input HTML
<html>Hello ${first_name}!</html>

Input Data
{
  "first_name": "Joel"
}

Output HTML
<html>Hello Joel!</html>

功能

变量

列表

条件 - 内置函数

  • ==
  • !=
  • ?exists
  • ?not_empty
  • ?contains('some substring value')

示例

use std::collections::HashMap;
use kitamura::render_template;
use serde_json::json;

let input_html = "<html>Hello ${first_name}!</html>";
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, "<html>Hello Joel!</html>");
use std::collections::HashMap;
use kitamura::render_template;
use serde_json::json;

let input_html =
"<html>
 <ul>
   {#for fruit of fruits#}
   <ul>
     <li>${fruit.name}</li>
     <li>${fruit.colour}</li>
     <li>${fruit.weight}</li>
   </ul>
   {#endfor#}
 </ul>
</html>";
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,
"<html>
 <ul>
   <ul>
     <li>Lemon</li>
     <li>Yellow</li>
     <li>150g</li>
   </ul>
   <ul>
     <li>shiikuwasha</li>
     <li>Green</li>
     <li>80g</li>
   </ul>
   <ul>
     <li>Lychee</li>
     <li>Red</li>
     <li>50g</li>
   </ul>
 </ul>
</html>");
use std::collections::HashMap;
use kitamura::render_template;
use serde_json::json;

let input_html =
"<html>
  <ul>
  {#for continent of continents#}
    <li>${continent.name}</li>
    <ul>
    {#for country of continent.countries#}
      <li>${country.name}</li>
      <ul>
      {#for city of 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);
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);

依赖关系

~0.7–1.4MB
~33K SLoC