Rust තුළ Go html/template-ශෛලියේ රෙන්ඩරින්.
go_html_template යනු Go html/template හි ප්රධාන workflow-වලට සමාන වන Rust crate එකකි. මෙය template syntax, pipelines, context-aware escaping සහ අනාරක්ෂිත URL scheme අවහිර කිරීම හුරුපුරුදු API හැඩයක් සමඟ සහය දක්වයි.
Go වැනි Template API
define/template/range/with/pipeline විශේෂාංග සමඟ Template::new(...).parse(...).execute(...) භාවිතා කරන්න.
සන්දර්භය-දැනුම්වත් escaping
HTML පෙළ, attributes, URL attributes සහ script/style contexts සඳහා context-aware escaping යොදයි, සහ අනාරක්ෂිත javascript: URL අවහිර කරයි.
web-rust ප්රකාරය
web-rust feature එක සමඟ parse_files/parse_glob/parse_fs අක්රිය වන නිසා in-memory template loading බල කළ හැක.
සැකසීම සහ මූලික භාවිතය
crate එක එක් කරන්න, ඉන්පසු අවම parse + execute_to_string ප්රවාහය තහවුරු කරන්න. web-rust පරිසරවල file-loading API වෙනුවට in-memory template strings භාවිතා කරන්න.
ස්ථාපනය
cargo add go_html_template
Rust උදාහරණය
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(())
}
අනුකූලතා තත්ත්වය
go_html_template දැනට මූලික වැඩ ප්රවාහ ක්රියාත්මක කරමින් අනුකූලතා හිඩැස් තවදුරටත් පුරවමින් සිටී . එබැවින් මෙය තවමත් Go html/template සමඟ දැඩි 1:1 අනුකූලතා ඉලක්කයක් නොවන අතර, production templates තුළ හැසිරීම තහවුරු කරන්න.