diff options
Diffstat (limited to 'vendor/handlebars/examples')
20 files changed, 767 insertions, 0 deletions
diff --git a/vendor/handlebars/examples/decorator.rs b/vendor/handlebars/examples/decorator.rs new file mode 100644 index 000000000..81aa6fe8d --- /dev/null +++ b/vendor/handlebars/examples/decorator.rs @@ -0,0 +1,195 @@ +extern crate env_logger; +extern crate handlebars; +#[macro_use] +extern crate serde_derive; +extern crate serde_json; +use std::error::Error; + +use serde_json::value::{Map, Value as Json}; + +use handlebars::{ + to_json, Context, Decorator, Handlebars, Helper, JsonRender, Output, RenderContext, RenderError, +}; + +// default format helper +fn format_helper( + h: &Helper, + _: &Handlebars, + _: &Context, + _: &mut RenderContext, + out: &mut dyn Output, +) -> Result<(), RenderError> { + // get parameter from helper or throw an error + let param = h + .param(0) + .ok_or(RenderError::new("Param 0 is required for format helper."))?; + let rendered = format!("{} pts", param.value().render()); + out.write(rendered.as_ref())?; + Ok(()) +} + +// a decorator registers helpers +fn format_decorator( + d: &Decorator, + _: &Handlebars, + _: &Context, + rc: &mut RenderContext, +) -> Result<(), RenderError> { + let suffix = d + .param(0) + .map(|v| v.value().render()) + .unwrap_or("".to_owned()); + rc.register_local_helper( + "format", + Box::new( + move |h: &Helper, + _: &Handlebars, + _: &Context, + _: &mut RenderContext, + out: &mut dyn Output| { + // get parameter from helper or throw an error + let param = h + .param(0) + .ok_or(RenderError::new("Param 0 is required for format helper."))?; + let rendered = format!("{} {}", param.value().render(), suffix); + out.write(rendered.as_ref())?; + Ok(()) + }, + ), + ); + Ok(()) +} + +// a decorator mutates current context data +fn set_decorator( + d: &Decorator, + _: &Handlebars, + ctx: &Context, + rc: &mut RenderContext, +) -> Result<(), RenderError> { + // get the input of decorator + let data_to_set = d.hash(); + // retrieve the json value in current context + let ctx_data = ctx.data(); + + if let Json::Object(m) = ctx_data { + let mut new_ctx_data = m.clone(); + + for (k, v) in data_to_set { + new_ctx_data.insert(k.to_string(), v.value().clone()); + } + + rc.set_context(Context::wraps(new_ctx_data)?); + Ok(()) + } else { + Err(RenderError::new("Cannot extend non-object data")) + } +} + +// another custom helper +fn rank_helper( + h: &Helper, + _: &Handlebars, + _: &Context, + _: &mut RenderContext, + out: &mut dyn Output, +) -> Result<(), RenderError> { + let rank = h + .param(0) + .and_then(|v| v.value().as_u64()) + .ok_or(RenderError::new( + "Param 0 with u64 type is required for rank helper.", + ))? as usize; + let total = h + .param(1) + .as_ref() + .and_then(|v| v.value().as_array()) + .map(|arr| arr.len()) + .ok_or(RenderError::new( + "Param 1 with array type is required for rank helper", + ))?; + if rank == 0 { + out.write("champion")?; + } else if rank >= total - 2 { + out.write("relegation")?; + } else if rank <= 2 { + out.write("acl")?; + } + Ok(()) +} + +static TYPES: &'static str = "serde_json"; + +// define some data +#[derive(Serialize)] +pub struct Team { + name: String, + pts: u16, +} + +// produce some data +pub fn make_data() -> Map<String, Json> { + let mut data = Map::new(); + + data.insert("year".to_string(), to_json("2015")); + + let teams = vec![ + Team { + name: "Jiangsu Suning".to_string(), + pts: 43u16, + }, + Team { + name: "Shanghai SIPG".to_string(), + pts: 39u16, + }, + Team { + name: "Hebei CFFC".to_string(), + pts: 27u16, + }, + Team { + name: "Guangzhou Evergrand".to_string(), + pts: 22u16, + }, + Team { + name: "Shandong Luneng".to_string(), + pts: 12u16, + }, + Team { + name: "Beijing Guoan".to_string(), + pts: 7u16, + }, + Team { + name: "Hangzhou Greentown".to_string(), + pts: 7u16, + }, + Team { + name: "Shanghai Shenhua".to_string(), + pts: 4u16, + }, + ]; + + data.insert("teams".to_string(), to_json(&teams)); + data.insert("engine".to_string(), to_json(TYPES)); + data +} + +fn main() -> Result<(), Box<dyn Error>> { + env_logger::init(); + // create the handlebars registry + let mut handlebars = Handlebars::new(); + + // register template from a file and assign a name to it + // deal with errors + handlebars.register_template_file("table", "./examples/decorator/template.hbs")?; + + // register some custom helpers + handlebars.register_helper("format", Box::new(format_helper)); + handlebars.register_helper("ranking_label", Box::new(rank_helper)); + handlebars.register_decorator("format_suffix", Box::new(format_decorator)); + handlebars.register_decorator("set", Box::new(set_decorator)); + + // make data and render it + let data = make_data(); + println!("{}", handlebars.render("table", &data)?); + Ok(()) +} diff --git a/vendor/handlebars/examples/decorator/template.hbs b/vendor/handlebars/examples/decorator/template.hbs new file mode 100644 index 000000000..5b965182b --- /dev/null +++ b/vendor/handlebars/examples/decorator/template.hbs @@ -0,0 +1,21 @@ +{{*format_suffix "分"}} + +<html> + <head> + <title>CSL {{year}}</title> + </head> + <body> + <h1>CSL {{year}}</h1> + <ul> + {{#each teams as |t|}} + <li class="{{ranking_label @index ../teams}}"> + {{~log @index~}} + <b>{{t.name}}</b>: {{format t.pts ~}} + </li> + {{/each}} + </ul> + + {{*set version="v3.0"}} + <p>Rendered by Handlebars {{version}} from {{engine}} data.</p> + </body> +</html> diff --git a/vendor/handlebars/examples/dev_mode.rs b/vendor/handlebars/examples/dev_mode.rs new file mode 100644 index 000000000..99017474f --- /dev/null +++ b/vendor/handlebars/examples/dev_mode.rs @@ -0,0 +1,27 @@ +use std::sync::Arc; + +use handlebars::Handlebars; +use serde_json::json; +use warp::{self, Filter}; + +#[tokio::main] +async fn main() { + let mut reg = Handlebars::new(); + // enable dev mode for template reloading + reg.set_dev_mode(true); + // register a template from the file + // modified the file after the server starts to see things changing + reg.register_template_file("tpl", "./examples/dev_mode/template.hbs") + .unwrap(); + + let hbs = Arc::new(reg); + let route = warp::get().map(move || { + let result = hbs + .render("tpl", &json!({"model": "t14s", "brand": "Thinkpad"})) + .unwrap_or_else(|e| e.to_string()); + warp::reply::html(result) + }); + + println!("Edit ./examples/dev_mode/template.hbs and request http://localhost:3030 to see the change on the run."); + warp::serve(route).run(([127, 0, 0, 1], 3030)).await; +} diff --git a/vendor/handlebars/examples/dev_mode/template.hbs b/vendor/handlebars/examples/dev_mode/template.hbs new file mode 100644 index 000000000..19bb80657 --- /dev/null +++ b/vendor/handlebars/examples/dev_mode/template.hbs @@ -0,0 +1,8 @@ +<html> + <head> + <title>My Laptop</title> + </head> + <body> + <p>My current laptop is {{brand}}: <b>{{model}}</b></p> + </body> +</html> diff --git a/vendor/handlebars/examples/error.rs b/vendor/handlebars/examples/error.rs new file mode 100644 index 000000000..3fb874e2b --- /dev/null +++ b/vendor/handlebars/examples/error.rs @@ -0,0 +1,40 @@ +extern crate env_logger; +extern crate handlebars; +#[macro_use] +extern crate serde_json; + +use std::error::Error; + +use handlebars::Handlebars; + +fn main() -> Result<(), Box<dyn Error>> { + env_logger::init(); + let mut handlebars = Handlebars::new(); + + // template not found + println!( + "{}", + handlebars + .register_template_file("notfound", "./examples/error/notfound.hbs") + .unwrap_err() + ); + + // an invalid templat + println!( + "{}", + handlebars + .register_template_file("error", "./examples/error/error.hbs") + .unwrap_err() + ); + + // render error + let e1 = handlebars + .render_template("{{#if}}", &json!({})) + .unwrap_err(); + let be1 = Box::new(e1); + println!("{}", be1); + println!("{}", be1.source().unwrap()); + println!("{:?}", be1.source().unwrap().source()); + + Ok(()) +} diff --git a/vendor/handlebars/examples/error/error.hbs b/vendor/handlebars/examples/error/error.hbs new file mode 100644 index 000000000..89537e71e --- /dev/null +++ b/vendor/handlebars/examples/error/error.hbs @@ -0,0 +1,21 @@ +{{! this is an invalid template }} + +<html> + <head> + <title>中超联赛 {{year}}</title> + </head> + <body> + <h1>CSL {{year}}</h1> + <ul> + {{#each teams as |t| ~}} + <li class="{{ranking_label true ../teams}}"> + {{~log @index~}} + <b>{{t.name}}</b>: {{format t.pts ~}} + </li> + {{! mismatched helper close tag }} + {{/arch~}} + </ul> + + <p>Rendered by Handlebars from {{engine}} data.</p> + </body> +</html> diff --git a/vendor/handlebars/examples/error/template.hbs b/vendor/handlebars/examples/error/template.hbs new file mode 100644 index 000000000..9ed5b7bde --- /dev/null +++ b/vendor/handlebars/examples/error/template.hbs @@ -0,0 +1,19 @@ +<html> + <head> + <title>中超联赛 {{year}}</title> + </head> + <body> + <h1>CSL {{year}}</h1> + <ul> + {{#each teams as |t|}} + {{! ranking_label will produce a render error when first parameter is not a number }} + <li class="{{ranking_label true ../teams}}"> + {{~log @index~}} + <b>{{t.name}}</b>: {{format t.pts ~}} + </li> + {{/each}} + </ul> + + <p>Rendered by Handlebars from {{engine}} data.</p> + </body> +</html> diff --git a/vendor/handlebars/examples/partials.rs b/vendor/handlebars/examples/partials.rs new file mode 100644 index 000000000..80b20c2f7 --- /dev/null +++ b/vendor/handlebars/examples/partials.rs @@ -0,0 +1,35 @@ +extern crate env_logger; +extern crate handlebars; +#[macro_use] +extern crate maplit; + +use handlebars::Handlebars; +use std::error::Error; + +fn main() -> Result<(), Box<dyn Error>> { + env_logger::init(); + let mut handlebars = Handlebars::new(); + + handlebars.register_template_file("template", "./examples/partials/template2.hbs")?; + + handlebars.register_template_file("base0", "./examples/partials/base0.hbs")?; + handlebars.register_template_file("base1", "./examples/partials/base1.hbs")?; + + let data0 = btreemap! { + "title".to_string() => "example 0".to_string(), + "parent".to_string() => "base0".to_string() + }; + let data1 = btreemap! { + "title".to_string() => "example 1".to_string(), + "parent".to_string() => "base1".to_string() + }; + + println!("Page 0"); + println!("{}", handlebars.render("template", &data0)?); + println!("======================================================="); + + println!("Page 1"); + println!("{}", handlebars.render("template", &data1)?); + + Ok(()) +} diff --git a/vendor/handlebars/examples/partials/base0.hbs b/vendor/handlebars/examples/partials/base0.hbs new file mode 100644 index 000000000..34b38e763 --- /dev/null +++ b/vendor/handlebars/examples/partials/base0.hbs @@ -0,0 +1,7 @@ +<html> + <head>{{title}}</head> + <body> + <div><h1>Derived from base0.hbs</h1></div> + {{> page}} + </body> +</html> diff --git a/vendor/handlebars/examples/partials/base1.hbs b/vendor/handlebars/examples/partials/base1.hbs new file mode 100644 index 000000000..8f771dfad --- /dev/null +++ b/vendor/handlebars/examples/partials/base1.hbs @@ -0,0 +1,7 @@ +<html> + <head>{{title}}</head> + <body> + <div><h1>Derived from base1.hbs</h1></div> + {{> page}} + </body> +</html> diff --git a/vendor/handlebars/examples/partials/template2.hbs b/vendor/handlebars/examples/partials/template2.hbs new file mode 100644 index 000000000..934416eb7 --- /dev/null +++ b/vendor/handlebars/examples/partials/template2.hbs @@ -0,0 +1,4 @@ +{{#*inline "page"}} + <p>Rendered in partial, parent is {{parent}}</p> +{{/inline}} +{{> (lookup this "parent")}} diff --git a/vendor/handlebars/examples/quick.rs b/vendor/handlebars/examples/quick.rs new file mode 100644 index 000000000..ff1c94ca6 --- /dev/null +++ b/vendor/handlebars/examples/quick.rs @@ -0,0 +1,18 @@ +use std::error::Error; + +use handlebars::Handlebars; +use serde_json::json; + +fn main() -> Result<(), Box<dyn Error>> { + let mut reg = Handlebars::new(); + // render without register + println!( + "{}", + reg.render_template("Hello {{name}}", &json!({"name": "foo"}))? + ); + + // register template using given name + reg.register_template_string("tpl_1", "Good afternoon, {{name}}")?; + println!("{}", reg.render("tpl_1", &json!({"name": "foo"}))?); + Ok(()) +} diff --git a/vendor/handlebars/examples/render.rs b/vendor/handlebars/examples/render.rs new file mode 100644 index 000000000..112ec504d --- /dev/null +++ b/vendor/handlebars/examples/render.rs @@ -0,0 +1,135 @@ +extern crate env_logger; +extern crate handlebars; + +#[macro_use] +extern crate serde_derive; +extern crate serde_json; + +use serde_json::value::{Map, Value as Json}; +use std::error::Error; + +use handlebars::{ + to_json, Context, Handlebars, Helper, JsonRender, Output, RenderContext, RenderError, +}; + +// define a custom helper +fn format_helper( + h: &Helper, + _: &Handlebars, + _: &Context, + _: &mut RenderContext, + out: &mut dyn Output, +) -> Result<(), RenderError> { + // get parameter from helper or throw an error + let param = h + .param(0) + .ok_or(RenderError::new("Param 0 is required for format helper."))?; + let rendered = format!("{} pts", param.value().render()); + out.write(rendered.as_ref())?; + Ok(()) +} + +// another custom helper +fn rank_helper( + h: &Helper, + _: &Handlebars, + _: &Context, + _: &mut RenderContext, + out: &mut dyn Output, +) -> Result<(), RenderError> { + let rank = h + .param(0) + .and_then(|v| v.value().as_u64()) + .ok_or(RenderError::new( + "Param 0 with u64 type is required for rank helper.", + ))? as usize; + let total = h + .param(1) + .as_ref() + .and_then(|v| v.value().as_array()) + .map(|arr| arr.len()) + .ok_or(RenderError::new( + "Param 1 with array type is required for rank helper", + ))?; + if rank == 0 { + out.write("champion")?; + } else if rank >= total - 2 { + out.write("relegation")?; + } else if rank <= 2 { + out.write("acl")?; + } + Ok(()) +} + +static TYPES: &'static str = "serde_json"; + +// define some data +#[derive(Serialize)] +pub struct Team { + name: String, + pts: u16, +} + +// produce some data +pub fn make_data() -> Map<String, Json> { + let mut data = Map::new(); + + data.insert("year".to_string(), to_json("2015")); + + let teams = vec![ + Team { + name: "Jiangsu Suning".to_string(), + pts: 43u16, + }, + Team { + name: "Shanghai SIPG".to_string(), + pts: 39u16, + }, + Team { + name: "Hebei CFFC".to_string(), + pts: 27u16, + }, + Team { + name: "Guangzhou Evergrand".to_string(), + pts: 22u16, + }, + Team { + name: "Shandong Luneng".to_string(), + pts: 12u16, + }, + Team { + name: "Beijing Guoan".to_string(), + pts: 7u16, + }, + Team { + name: "Hangzhou Greentown".to_string(), + pts: 7u16, + }, + Team { + name: "Shanghai Shenhua".to_string(), + pts: 4u16, + }, + ]; + + data.insert("teams".to_string(), to_json(&teams)); + data.insert("engine".to_string(), to_json(TYPES)); + data +} + +fn main() -> Result<(), Box<dyn Error>> { + env_logger::init(); + // create the handlebars registry + let mut handlebars = Handlebars::new(); + + // register template from a file and assign a name to it + handlebars.register_template_file("table", "./examples/render/template.hbs")?; + + // register some custom helpers + handlebars.register_helper("format", Box::new(format_helper)); + handlebars.register_helper("ranking_label", Box::new(rank_helper)); + + // make data and render it + let data = make_data(); + println!("{}", handlebars.render("table", &data)?); + Ok(()) +} diff --git a/vendor/handlebars/examples/render/template.hbs b/vendor/handlebars/examples/render/template.hbs new file mode 100644 index 000000000..2a62d014f --- /dev/null +++ b/vendor/handlebars/examples/render/template.hbs @@ -0,0 +1,18 @@ +<html> + <head> + <title>中超联赛 {{year}}</title> + </head> + <body> + <h1>CSL {{year}}</h1> + <ul> + {{#each teams as |t|}} + <li class="{{ranking_label @index ../teams}}"> + {{~log @index~}} + <b>{{t.name}}</b>: {{format t.pts ~}} + </li> + {{/each}} + </ul> + + <p>Rendered by Handlebars from {{engine}} data.</p> + </body> +</html> diff --git a/vendor/handlebars/examples/render_cli/simple.hbs b/vendor/handlebars/examples/render_cli/simple.hbs new file mode 100644 index 000000000..6b9e01247 --- /dev/null +++ b/vendor/handlebars/examples/render_cli/simple.hbs @@ -0,0 +1 @@ +Data: {{ . }} diff --git a/vendor/handlebars/examples/render_file.rs b/vendor/handlebars/examples/render_file.rs new file mode 100644 index 000000000..7b7599672 --- /dev/null +++ b/vendor/handlebars/examples/render_file.rs @@ -0,0 +1,140 @@ +#![allow(unused_imports, dead_code)] +extern crate env_logger; +extern crate handlebars; +extern crate serde; +#[macro_use] +extern crate serde_derive; +extern crate serde_json; +use serde::Serialize; +use serde_json::value::{self, Map, Value as Json}; + +use std::error::Error; +use std::fs::File; +use std::io::{Read, Write}; + +use handlebars::{ + to_json, Context, Handlebars, Helper, JsonRender, Output, RenderContext, RenderError, +}; + +// define a custom helper +fn format_helper( + h: &Helper, + _: &Handlebars, + _: &Context, + _: &mut RenderContext, + out: &mut dyn Output, +) -> Result<(), RenderError> { + let param = h + .param(0) + .ok_or(RenderError::new("Param 0 is required for format helper."))?; + let rendered = format!("{} pts", param.value().render()); + out.write(rendered.as_ref())?; + Ok(()) +} + +// another custom helper +fn rank_helper( + h: &Helper, + _: &Handlebars, + _: &Context, + _: &mut RenderContext, + out: &mut dyn Output, +) -> Result<(), RenderError> { + let rank = h + .param(0) + .and_then(|ref v| v.value().as_u64()) + .ok_or(RenderError::new( + "Param 0 with u64 type is required for rank helper.", + ))? as usize; + let total = h + .param(1) + .as_ref() + .and_then(|v| v.value().as_array()) + .map(|arr| arr.len()) + .ok_or(RenderError::new( + "Param 1 with array type is required for rank helper", + ))?; + if rank == 0 { + out.write("champion")?; + } else if rank >= total - 2 { + out.write("relegation")?; + } else if rank <= 2 { + out.write("acl")?; + } + Ok(()) +} + +static TYPES: &'static str = "serde_json"; + +// define some data +#[derive(Serialize)] +pub struct Team { + name: String, + pts: u16, +} + +// produce some data +pub fn make_data() -> Map<String, Json> { + let mut data = Map::new(); + + data.insert("year".to_string(), to_json("2015")); + + let teams = vec![ + Team { + name: "Jiangsu Suning".to_string(), + pts: 43u16, + }, + Team { + name: "Shanghai SIPG".to_string(), + pts: 39u16, + }, + Team { + name: "Hebei CFFC".to_string(), + pts: 27u16, + }, + Team { + name: "Guangzhou Evergrand".to_string(), + pts: 22u16, + }, + Team { + name: "Shandong Luneng".to_string(), + pts: 12u16, + }, + Team { + name: "Beijing Guoan".to_string(), + pts: 7u16, + }, + Team { + name: "Hangzhou Greentown".to_string(), + pts: 7u16, + }, + Team { + name: "Shanghai Shenhua".to_string(), + pts: 4u16, + }, + ]; + + data.insert("teams".to_string(), to_json(&teams)); + data.insert("engine".to_string(), to_json(TYPES)); + data +} + +fn main() -> Result<(), Box<dyn Error>> { + env_logger::init(); + let mut handlebars = Handlebars::new(); + + handlebars.register_helper("format", Box::new(format_helper)); + handlebars.register_helper("ranking_label", Box::new(rank_helper)); + // handlebars.register_helper("format", Box::new(FORMAT_HELPER)); + + let data = make_data(); + + handlebars + .register_template_file("template", "./examples/render_file/template.hbs") + .unwrap(); + + let mut output_file = File::create("target/table.html")?; + handlebars.render_to_write("template", &data, &mut output_file)?; + println!("target/table.html generated"); + Ok(()) +} diff --git a/vendor/handlebars/examples/render_file/template.hbs b/vendor/handlebars/examples/render_file/template.hbs new file mode 100644 index 000000000..84a0d6bc0 --- /dev/null +++ b/vendor/handlebars/examples/render_file/template.hbs @@ -0,0 +1,19 @@ +<html> + <head> + <title>中超联赛 {{year}}</title> + </head> + <body> + <h1>CSL {{year}}</h1> + <ul> + {{#each teams as |t| }} + <li class="{{ranking_label @index ../teams}}"> + {{~log @index~}} + {{!-- I'm comment --}} + <b>{{t.name}}</b>: {{format t.pts ~}} + </li> + {{/each}} + </ul> + + <p>Rendered by Handlebars from {{engine}} data.</p> + </body> +</html> diff --git a/vendor/handlebars/examples/script.rs b/vendor/handlebars/examples/script.rs new file mode 100644 index 000000000..bedd426cd --- /dev/null +++ b/vendor/handlebars/examples/script.rs @@ -0,0 +1,39 @@ +#![allow(unused_imports)] + +use handlebars::Handlebars; +use std::error::Error; +#[macro_use] +extern crate serde_json; + +#[cfg(feature = "script_helper")] +fn main() -> Result<(), Box<dyn Error>> { + let mut handlebars = Handlebars::new(); + + handlebars.register_template_file("tpl", "./examples/script/template.hbs")?; + handlebars.register_script_helper_file("score", "./examples/script/goals.rhai")?; + + let data = json! {[ + [{ + "name": "Dortmund", + "goals": ["Haaland", "Guerreiro", "Hazard", "Guerreiro"] + }, { + "name": "Schalke", + "goals": [] + }], + [{ + "name": "RB Leipzig", + "goals": ["Poulsen"] + }, { + "name": "SC Feriburg", + "goals": ["Gulde"] + }] + ]}; + println!("{}", handlebars.render("tpl", &data)?); + Ok(()) +} + +#[cfg(not(feature = "script_helper"))] +fn main() -> Result<(), Box<dyn Error>> { + println!("Please enable feature flag script_helper for this example"); + Ok(()) +} diff --git a/vendor/handlebars/examples/script/goals.rhai b/vendor/handlebars/examples/script/goals.rhai new file mode 100644 index 000000000..65828202e --- /dev/null +++ b/vendor/handlebars/examples/script/goals.rhai @@ -0,0 +1,3 @@ +let goals = params[0]; + +goals.len() diff --git a/vendor/handlebars/examples/script/template.hbs b/vendor/handlebars/examples/script/template.hbs new file mode 100644 index 000000000..c04253c72 --- /dev/null +++ b/vendor/handlebars/examples/script/template.hbs @@ -0,0 +1,10 @@ +Bundesliga Match Day +{{#each this as |match|}} + {{#each match as |team|}} + {{team.name}} - {{score team.goals}} + {{#each team.goals as |scorer|}} + > {{scorer}} + {{/each}} + {{/each}} + --- +{{/each}} |