maciejhirsz / ramhorns

Fast Mustache template engine implementation in pure Rust.
https://crates.io/crates/ramhorns
Mozilla Public License 2.0
293 stars 29 forks source link

Add support for dot notation #43

Open thesurlydev opened 3 years ago

thesurlydev commented 3 years ago

For example,

{{#some_value}}
{{.}}
{{/some_value}}
Th3Whit3Wolf commented 3 years ago

I am looking to add this to the rust template benchmark but I think dot notation is required to do it properly.

maciejhirsz commented 3 years ago

It shouldn't be too hard to do, I've commented on this in previous issue.

@Th3Whit3Wolf I assume this is mostly for repeated sections?

Th3Whit3Wolf commented 3 years ago

@maciejhirsz yeah I have something like this

#[derive(Content)]
struct BigTable {
    table: Vec<Vec<usize>>,
}

static BIG_TABLE_TEMPLATE: &'static str = "<table>\
{{#table}}\
<tr>{{#.}}<td>{{.}}</td>{{/.}}</tr>\
{{/table}}\
</table>";

pub fn big_table(b: &mut criterion::Bencher<'_>, size: &usize) {
    let mut table = Vec::with_capacity(*size);
    for _ in 0..*size {
        let mut inner = Vec::with_capacity(*size);
        for i in 0..*size {
            inner.push(Value::Scalar((i as i32).into()));
        }
        table.push(Value::Array(inner));
    }

    let big_table_data = BigTable {
        table
    };

    let template = Template::new(BIG_TABLE_TEMPLATE).unwrap();

    b.iter(|| template.render(&big_table_data {

    }));
}

Where I need to access the value of a Vec<Vec<usize>>

tusharmath commented 12 months ago

Is there a way to do something like {{a.b.c}}, right now it doesn't seem to work.