Liquid with Option in a struct



examples/liquid/liquid-with-option/Cargo.toml
[package]
name = "liquid-with-option"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
liquid = "0.26"
serde = { version = "1.0", features = ["derive"] }

examples/liquid/liquid-with-option/src/main.rs
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct Details {
    weight: u32,
    length: u32,
}

#[derive(Serialize, Deserialize)]
struct Car {
    manufacturer: String,
    color: Option<String>,
    details: Option<Details>,
}

fn main() {
    let template = liquid::ParserBuilder::with_stdlib()
        .build()
        .unwrap()
        .parse(
            "
            Car manufacturer: {{car.manufacturer}}
            Always color: {{ car.color }}
            {% if car.color %}
                Color: {{ car.color }}
            {% else %}
                no color
            {% endif %}

            {% if car.details %}
                Weight: {{ car.details.weight }}
            {% endif %}
        ",
        )
        .unwrap();

    let car = Car {
        manufacturer: String::from("Ford"),
        color: Some(String::from("blue")),
        details: Some(Details {
            weight: 1000,
            length: 400,
        }),
    };

    let globals = liquid::object!(
    {
        "car": car,
    });
    let output = template.render(&globals).unwrap();

    println!("{output}");

    let car = Car {
        manufacturer: String::from("Ford"),
        color: None,
        details: None,
    };

    let globals = liquid::object!(
    {
        "car": car,
    });
    let output = template.render(&globals).unwrap();

    println!("{output}");
}