The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Call A Function With Arguments In minijinja

TODO: Pull subtitle into page object
Code
//! ```cargo
//! [dependencies]
//! minijinja = "1.0.8"
//! serde = { version = "1.0.167", features = ['derive'] }
//! ```

use minijinja::value::{Object, Value};
use minijinja::{context, Environment, Error};
use serde::Serialize;
use std::fmt::Display;

#[derive(Debug, Serialize)]
struct Page {}

impl Page {
    pub fn ping(&self, args: &[Value]) -> Option<String> {
        Some(format!("Got: {}", args[0]))
    }
}

impl Object for Page {
    fn call_method(
        &self,
        _state: &minijinja::State,
        name: &str,
        args: &[Value],
    ) -> Result<Value, Error> {
        match name {
            "ping" => Ok(Value::from_serializable(&self.ping(args))),
            _ => Ok(Value::from("")),
        }
    }
}
impl Display for Page {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "required display output")
    }
}

fn main() {
    let mut env = Environment::new();
    let page = Page {};
    let page_object = Value::from_object(page);
    env.add_template("ping_test", "{{ page.ping('one') }}").unwrap();
    let tmpl = env.get_template("ping_test").unwrap();
    println!("{}", tmpl.render(context!( page => page_object )).unwrap());
}
Results
Got: one

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Call A Function With Arguments In minijinja


-- code
-- rust

//! ```cargo
//! [dependencies]
//! minijinja = "1.0.8"
//! serde = { version = "1.0.167", features = ['derive'] }
//! ```

use minijinja::value::{Object, Value};
use minijinja::{context, Environment, Error};
use serde::Serialize;
use std::fmt::Display;

#[derive(Debug, Serialize)]
struct Page {}

impl Page {
    pub fn ping(&self, args: &[Value]) -> Option<String> {
        Some(format!("Got: {}", args[0]))
    }
}

impl Object for Page {
    fn call_method(
        &self,
        _state: &minijinja::State,
        name: &str,
        args: &[Value],
    ) -> Result<Value, Error> {
        match name {
            "ping" => Ok(Value::from_serializable(&self.ping(args))),
            _ => Ok(Value::from("")),
        }
    }
}
impl Display for Page {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "required display output")
    }
}

fn main() {
    let mut env = Environment::new();
    let page = Page {};
    let page_object = Value::from_object(page);
    env.add_template("ping_test", "{{ page.ping('one') }}").unwrap();
    let tmpl = env.get_template("ping_test").unwrap();
    println!("{}", tmpl.render(context!( page => page_object )).unwrap());
}

-- results/

Got: one

-- /results


-- categories
-- Rust 
-- minijinja 

-- metadata
-- date: 2023-09-23 15:46:52
-- id: 2vonl0ju
-- site: aws
-- type: post
-- status: scratch