14 hours ago · Tech · hide · 0 comments

This is how I'm grabbing optional keyword arguments in MiniJinja with a fallback to a default if the arg doesn't exist. Rust Cargo.toml[package] name = "get_optional_keyword_arg" version = "0.1.0" edition = "2024" [dependencies] anyhow = "1.0.103" minijinja = { version = "2.21.0", features = ["custom_syntax"] } src/main.rsuse anyhow::Error as AnyError; use minijinja::syntax::SyntaxConfig; use minijinja::{Environment, Error, Value, context}; fn main() -> Result<(), AnyError> { let env = get_env()?; let template = env.get_template("example.txt")?; let context = context!(); let output = template.render(context)?; println!("{}", output); Ok(()) } pub fn get_env() -> Result<Environment<'static>, Error> { let mut env = Environment::new(); env.set_syntax( SyntaxConfig::builder() .block_delimiters("[!", "!]") .variable_delimiters("[@", "@]") .comment_delimiters("[#", "#]") .build() .unwrap(), ); env.add_template_owned( "example.txt", r#" [@ arg_test() @] [@ arg_test(example_arg = "This is…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.