我有一个需要导入 2 个函数的 Tera(类似 Jinja2)模板。引发需要 2 个参数的预期函数。我该如何处理?
功能之一:
#[derive(Serialize)]
#[derive(Debug)]
enum Color {
White,
Blue,
Green,
Yellow,
Red,
}
fn bootstrap_table_color (e: Color) -> String {
return match e {
White => "".to_string(),
Blue => "table-info".to_string(),
Green => "table-success".to_string(),
Yellow => "table-warning".to_string(),
Red => "table-danger".to_string(),
};
}
模板:
let mut htmlbuilder = tera::Tera::new("src/*.html").unwrap();
let mut context = tera::Context::new();
htmlbuilder.register_filter("bootstrap_table_color", bootstrap_table_color);
context.insert("allentries", &allentries);
let htmldata = htmlbuilder.render_str("
{% for e in allentries[0] %}
<td class='{{ bootstrap_table_color(e=e.color) }}'>
<a class='{{ bootstrap_text_color(e=e.color) }}' href='{{ e.filename }}'>
{{ e.text }}
</a>
</td>
{% endfor %}
编译期间引发的错误:
error[E0593]: function is expected to take 2 arguments, but it takes 1 argument
--> src/main.rs:151:55
|
26 | fn bootstrap_table_color (e: Color) -> String {
| --------------------------------------------- takes 1 argument
...
151 | htmlbuilder.register_filter("bootstrap_table_color", bootstrap_table_color);
| ^^^^^^^^^^^^^^^^^^^^^ expected function that takes 2 arguments
|
= note: required because of the requirements on the impl of `tera::Filter` for `fn(Color) -> std::string::String {bootstrap_table_color}`