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.

Create A StructObject To Get Fields Directly In minijinja

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

[dependencies]
minijinja = "1.0.7"
Code
use minijinja::value::{StructObject, Value};
use minijinja::{context, Environment};

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_struct_object(alfa);
    println!(
        "{}",
        tmpl.render(context!(alfa_value => alfa_value)).unwrap()
    );
}

pub struct Alfa {}

impl StructObject for Alfa {
    fn get_field(&self, name: &str) -> Option<Value> {
        match name {
            "ping" => Some(Value::from(self.ping())),
            _ => None,
        }
    }
}

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

Create A StructObject To Get Fields Directly In minijinja


-- todo

[] Combine with id: 2tasgoyx

-- code

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

[dependencies]
minijinja = "1.0.7"

-- code
-- name: src/main.rs

use minijinja::value::{StructObject, Value};
use minijinja::{context, Environment};

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_struct_object(alfa);
    println!(
        "{}",
        tmpl.render(context!(alfa_value => alfa_value)).unwrap()
    );
}

pub struct Alfa {}

impl StructObject for Alfa {
    fn get_field(&self, name: &str) -> Option<Value> {
        match name {
            "ping" => Some(Value::from(self.ping())),
            _ => None,
        }
    }
}

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


-- categories
-- Rust 
-- minijinja 

-- metadata
-- date: 2023-09-09 16:25:25
-- id: 2vauitbb
-- site: aws
-- type: post
-- status: scratch