Rust ਵਿੱਚ Go html/template-ਸਟਾਈਲ ਰੈਂਡਰਿੰਗ।

go_html_template ਇੱਕ Rust crate ਹੈ ਜੋ Go html/template ਦੇ ਮੁੱਖ workflows ਨੂੰ ਦਰਸਾਉਂਦਾ ਹੈ। ਇਹ template syntax, pipelines, context-aware escaping ਅਤੇ unsafe URL scheme blocking ਨੂੰ ਸਮਰਥਨ ਦਿੰਦਾ ਹੈ ਅਤੇ ਜਾਣ-ਪਛਾਣ ਵਾਲੀ API shape ਕਾਇਮ ਰੱਖਦਾ ਹੈ।

Go-ਵਰਗੀ Template API

Template::new(...).parse(...).execute(...) ਨੂੰ define/template/range/with/pipeline ਫੀਚਰਾਂ ਨਾਲ ਵਰਤੋਂ।

ਸੰਦਰਭ-ਜਾਣੂ escaping

HTML text, attributes, URL attributes ਅਤੇ script/style contexts ਲਈ context-aware escaping ਲਾਗੂ ਕਰਦਾ ਹੈ ਅਤੇ unsafe javascript: URLs ਨੂੰ block ਕਰਦਾ ਹੈ।

web-rust ਮੋਡ

web-rust feature ਨਾਲ parse_files/parse_glob/parse_fs ਅਯੋਗ ਹੋ ਜਾਂਦੇ ਹਨ, ਤਾਂ ਜੋ in-memory template loading ਨੂੰ ਲਾਗੂ ਕੀਤਾ ਜਾ ਸਕੇ।

ਸੈੱਟਅੱਪ ਅਤੇ ਮੂਲ ਵਰਤੋਂ

crate ਜੋੜੋ, ਫਿਰ ਇੱਕ minimal parse + execute_to_string flow ਦੀ ਪੁਸ਼ਟੀ ਕਰੋ। web-rust environments ਵਿੱਚ 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 ਨੂੰ implement ਕਰਦਾ ਹੈ ਅਤੇ compatibility gaps ਨੂੰ ਲਗਾਤਾਰ ਘਟਾ ਰਿਹਾ ਹੈ . ਇਹ ਹਾਲੇ Go html/template ਨਾਲ ਕੜੀ 1:1 compatibility ਦਾ ਟਾਰਗੇਟ ਨਹੀਂ ਹੈ, ਇਸ ਲਈ ਆਪਣੇ production templates ਵਿੱਚ behavior ਦੀ ਪੁਸ਼ਟੀ ਕਰੋ।