Skip to content

In table can not process by function and other limation #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
damody opened this issue Jul 13, 2018 · 0 comments
Open

In table can not process by function and other limation #2

damody opened this issue Jul 13, 2018 · 0 comments

Comments

@damody
Copy link

damody commented Jul 13, 2018

#[derive(Clone, ZapperRunner, Serialize)]
#[filter = "checkshow/0s"]
struct Team {
    idx: i32,
    name: String,
    score: u8,
}

fn checkshow(_data: &Team, _args: &[f64], input: &str, buffer: &mut String) {
    if (_data.idx == 0) {
        buffer.push_str("champion");
    }
}

#[derive(ZapperEnv)]
#[runner = "Team"]
struct Provider {
    year: u32,
}

pub fn teams(b: &mut criterion::Bencher, _: &usize) {
    use serde_json::value::Map;
    let template = "<html>
  <head>
    <title>{{year}}</title>
  </head>
  <body>
    <h1>CSL {{year}}</h1>
    <ul>
    {{#each group as |p| ~}}
      <li class=\"{{p.name | checkshow}}\">
      <b>{{p.name}}</b>: {{p.score}}
      </li>
    {{/each~}}
    </ul>
  </body>
</html>";
    // build up a group of 1000 (similar) people
    let mut group = vec![
        Team { idx: 0, name: "Jiangsu".into(), score: 43 },
        Team { idx: 1, name: "Beijing".into(), score: 27 },
        Team { idx: 2, name: "Guangzhou".into(), score: 22 },
        Team { idx: 3, name: "Shandong".into(), score: 12 },
    ];


    let mut data = Map::new();
    data.insert("year".to_string(), to_json(&"2015".to_string()));
    let mut handlebars = Handlebars::new();
    handlebars
        .register_template_string("table", template)
        .unwrap();

    b.iter(|| {
        let mut data = data.clone();
        data.insert("group".to_string(), to_json(&group));
        handlebars.render("table", &data).unwrap()
    });
}

In this template, I just need for loop index and input constant string
like {{"champion" | checkshow}} or move champion to ZapperEnv
But 1. ZapperEnv can't use filter function
But 2. the template can't input constant string,
So I modify struct "Team", add member called idx.
But I found only input string can output string.
So I modify {{p.idx | checkshow}} to {{p.name | checkshow}}
It's not work.

{{#each group as |p| ~}}
      <li class=\"{{p.name | checkshow}}\">
      <b>{{p.name}}</b>: {{p.score}}
      </li>
{{/each~}}
// this part always get Error:"InvalidSyntax"

Why I do that?
To add zapper into this https://github.com/damody/template-benchmarks-rs
please fix zapper functional to run the benchmark.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant