Rendering í stíl Go html/template í Rust.
go_html_template er Rust-crate sem speglar helstu vinnuflæði Go html/template. Það styður template-málfræði, pipelines, samhengi-vitað escaping og lokun á óöruggum URL-schemes, en heldur kunnuglegu API-formi.
Template API í Go-stíl
Notaðu Template::new(...).parse(...).execute(...) með define/template/range/with/pipeline eiginleikum.
Samhengi-vitað escaping
Beitir samhengi-vituðu escaping fyrir HTML-texta, attribute, URL-attribute og script/style samhengi, og lokar óöruggum javascript: URL-um.
web-rust hamur
Með web-rust eiginleikanum eru parse_files/parse_glob/parse_fs óvirk svo hægt sé að þvinga template-hleðslu úr minni.
Uppsetning og grunnnotkun
Bættu crateinu við og staðfestu síðan lágmarks parse + execute_to_string flæði. Í web-rust umhverfum skaltu nota template-strengi í minni í stað API-a sem hlaða skrám.
Setja upp
cargo add go_html_template
Rust-dæmi
use go_html_template::{Template, Value};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let tpl = Template::new("page")
.parse(r#"<h1>{{.Title}}</h1>\n<p>{{.Body | safe_html}}</p>"#)?;
let out = tpl.execute_to_string(&serde_json::json!({
"Title": "go_html_template",
"Body": "<em>trusted</em>"
}))?;
println!("{out}");
Ok(())
}
Staða samhæfni
go_html_template útfærir kjarnaflæði og heldur áfram að loka samhæfnibilum . Það er ekki enn strangt 1:1 samhæfnimarkmið við Go html/template, svo staðfestu hegðun í framleiðslu-templateunum þínum.
Fyrra
OSS listi