Rust માં Go html/template જેવી રેન્ડરિંગ

go_html_template એક Rust crate છે જે Go html/template ના મુખ્ય workflows ને પ્રતિબિંબિત કરે છે. તેમાં template syntax, pipelines, context-aware escaping અને unsafe URL scheme blocking સાથે પરિચિત API આકાર મળે છે.

Go જેવી Template API

define/template/range/with/pipeline સુવિધાઓ સાથે Template::new(...).parse(...).execute(...) નો ઉપયોગ કરો.

સંદર્ભ-જાણીતું escaping

HTML text, attributes, URL attributes અને script/style contexts માટે context-aware escaping લાગુ કરે છે અને unsafe javascript: URLs ને રોકે છે.

web-rust મોડ

web-rust feature સાથે parse_files/parse_glob/parse_fs નિષ્ક્રિય થાય છે જેથી in-memory template loading ફરજિયાત રાખી શકાય.

સેટઅપ અને મૂળભૂત ઉપયોગ

crate ઉમેરો, પછી ન્યૂનતમ parse + execute_to_string flow ચકાસો. web-rust પર્યાવરણમાં file-loading APIs બદલે in-memory template strings નો ઉપયોગ કરો.

ઇન્સ્ટોલ

cargo add go_html_template

Rust ઉદાહરણ

src/main.rs
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 હાલમાં મૂળ workflows અમલમાં મૂકે છે અને compatibility gaps ધીમે ધીમે બંધ કરી રહ્યું છે . તે હજુ Go html/template સાથે કડક 1:1 compatibility લક્ષ્ય નથી, તેથી production templates માં વર્તન ચકાસો.