Actix web and Liquid Templating language

How to use actix web and access the liquid file

for example using handlebars and access file and passing data is

fn hi(){
let mut handlebars= handlebars::Handlebars::new();
let index_template = fs::read_to_string("templates/message_display.hbs").unwrap();
handlebars
.register_template_string("message_display", &index_template).expect("TODO: panic message");

let html = handlebars.render("message_display", &json!({"message":success_message})).unwrap() ;

HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(html)
}

How to perform similar operation for liquid templating language

Thank you

There's the liquid crate you could try.

Hello

But this create does not have any functions to load file and pass data

The example in the crate docs looks like it can parse a liquid template and accepts arbitrary data to use while rendering.

let template = liquid::ParserBuilder::with_stdlib()
    .build().unwrap()
    .parse("Liquid! {{num | minus: 2}}").unwrap();

let mut globals = liquid::object!({
    "num": 4f64
});

let output = template.render(&globals).unwrap();
assert_eq!(output, "Liquid! 2".to_string());

In that example, the "Liquid! {{num | minus: 2}}" is the template string. It doesn't really matter where you got the template string from, so you could just as easily pass in a reference to the string you get from std::fs::read_to_string().

The template.render(&globals) bit lets you render a template to a String, using globals as the rendering context.

1 Like

There's also the Parser::parse_file method.

1 Like