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 On An Object From A minijinja Template

TODO: Pull subtitle into page object
Code
[package]
name = "minijinji_object_method_call"
version = "0.1.0"
edition = "2021"

[dependencies]
minijinja = "1.0.7"
src/main.rs
use minijinja::value::{Object, Value};
use minijinja::{context, Environment, Error};
use std::fmt::Display;

fn main() {
    let mut env = Environment::new();
    env.add_template("hello", "{{ alfa_value.ping() }}")
        .unwrap();
    let tmpl = env.get_template("hello").unwrap();
    let alfa = Alfa {};
    let alfa_value = Value::from_object(alfa);
    println!(
        "{}",
        tmpl.render(context!(alfa_value => alfa_value)).unwrap()
    );
}

#[derive(Debug)]
pub struct Alfa {}

impl Object for Alfa {
    fn call_method(
        &self,
        _state: &minijinja::State,
        name: &str,
        _args: &[Value],
    ) -> Result<Value, Error> {
        match name {
            // NOTE: May need to do: 
            // Value::from_serializable. 
            // TODO: Look into that
            "ping" => Ok(Value::from(self.ping())),
            _ => {
                // TODO: make this an error possibly
                Ok(Value::from(""))
            }
        }
    }
}

impl Display for Alfa {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "THIS IS ALFA DISPLAY DEFAULT FORMAT")
    }
}

impl Alfa {
    fn ping(&self) -> String {
        "One ping only".to_string()
    }
}

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 On An Object From A minijinja Template

-- todo

[] Switch to using Value::from_serializable as well
which can be used for vecs of objects


[] Update this test with `args`` passing from call
method to `ping`` via `args[0]``


-- code
-- name: Cargo.toml

[package]
name = "minijinji_object_method_call"
version = "0.1.0"
edition = "2021"

[dependencies]
minijinja = "1.0.7"


-- code
-- rust
-- title: src/main.rs

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

fn main() {
    let mut env = Environment::new();
    env.add_template("hello", "{{ alfa_value.ping() }}")
        .unwrap();
    let tmpl = env.get_template("hello").unwrap();
    let alfa = Alfa {};
    let alfa_value = Value::from_object(alfa);
    println!(
        "{}",
        tmpl.render(context!(alfa_value => alfa_value)).unwrap()
    );
}

#[derive(Debug)]
pub struct Alfa {}

impl Object for Alfa {
    fn call_method(
        &self,
        _state: &minijinja::State,
        name: &str,
        _args: &[Value],
    ) -> Result<Value, Error> {
        match name {
            // NOTE: May need to do: 
            // Value::from_serializable. 
            // TODO: Look into that
            "ping" => Ok(Value::from(self.ping())),
            _ => {
                // TODO: make this an error possibly
                Ok(Value::from(""))
            }
        }
    }
}

impl Display for Alfa {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "THIS IS ALFA DISPLAY DEFAULT FORMAT")
    }
}

impl Alfa {
    fn ping(&self) -> String {
        "One ping only".to_string()
    }
}


-- ref
-- title: Minijinja
-- url: https://docs.rs/minijinja/latest/minijinja/

-- ref
-- url: https://docs.rs/minijinja/latest/minijinja/value/trait.Object.html

-- ref
-- url: https://doc.rust-lang.org/std/fmt/trait.Display.html

-- ref
-- url: https://docs.rs/minijinja/latest/minijinja/value/struct.Value.html#method.call_method

-- ref
-- url: https://docs.rs/minijinja/latest/minijinja/value/enum.ObjectKind.html#

-- ref
-- url: https://docs.rs/minijinja/latest/minijinja/value/trait.Object.html#method.call_method

-- ref
-- url: https://docs.rs/minijinja/latest/minijinja/struct.Error.html



-- categories
-- Rust 
-- minijinja 

-- metadata
-- date: 2023-09-09 17:04:01
-- id: 2vayzszc
-- site: aws
-- type: post
-- status: draft