Function polly::std_functions [] [src]

pub fn std_functions() -> HashMap<String, PollyFn>

Standard Functions of Polly

Note: This shouldn't be imported in into your crate, but is exposed as a way to incorporate Polly's functions into the documentation.

std.each

Arguments

  • array - The array of JSON to be iterated over.
  • component - The component to render for each element in array.

Applies a component to each element in the array. If the component takes more than one argument, the component can only be used with objects(in which case the function will attempt to find the object's property based on the argument name), or arrays of the same length as the number of arguments.

extern crate serde_json;
extern crate polly;
 
use serde_json::Value;
use polly::Template;
fn main() {
    const EXPECTED: &'static str = "\
    <html>\
        <body>\
            <ul>\
                <li>Item 1</li>\
                <li>Item 2</li>\
                <li>Item 3</li>\
            </ul>\
        </body>\
    </html>";
    let json: Value = serde_json::from_str(r#"{"array": ["Item 1","Item 2","Item 3"]}"#).unwrap();
    let json = json.as_object().unwrap().clone();
    let template = Template::load_from_source("documentation", r#"
    &listItem(@item) {/li{@item}}
    /html {
       /body {
           /ul{$std.each(component = &listItem, array = @array)}
       }
    }
    "#);
     
     assert_eq!(template.json(json).no_locales().render("en").unwrap(), EXPECTED);
}

std.if

Arguments

  • condition - The condition to be checked.
  • component - The component to be rendered based on the trueness of the condition.
  • json(optional) - The JSON passed to the component if needed.

Checks the JSON value of condition, and if it is true, renders the component, optinally passing in JSON for the component. It is important to note that what counts as true is similar to \ JavaScript, so it doesn't have to be strictly a boolean.

Boolean conditions

  • Array - false if the array's length is 0.
  • Null - Always false.
  • Bool - The literal value.
  • I64 - false if = 0.
  • U64 - false if = 0.
  • F64 - false if = 0.0.
  • String - false if the string is empty.
  • Object - false if the object is empty.
extern crate serde_json;
extern crate polly;
 
use serde_json::Value;
use polly::Template;
fn main() {
    const EXPECTED: &'static str = "\
    <html>\
        <body>\
        </body>\
    </html>";
    let json: Value = serde_json::from_str(r#"{"bool": false}"#).unwrap();
    let json = json.as_object().unwrap().clone();
    let template = Template::load_from_source("documentation", r#"
    &component {/h1{Hello World!}}
    /html {
       /body {
           $std.if(component = &component, condition = @bool)
       }
    }
    "#);
     
     assert_eq!(template.json(json).no_locales().render("en").unwrap(), EXPECTED);
}

std.if_else

Arguments

  • condition - The condition to be checked.
  • component - The component to be rendered based on the trueness of the condition.
  • else - The component rendered if the condition is false.
  • json(optional) - The JSON passed to the component, or the else component if needed.

Follows the same rules as std.if except with the ability to render a component if the value is false.

extern crate serde_json;
extern crate polly;
 
use serde_json::Value;
use polly::Template;
fn main() {
    const EXPECTED: &'static str = "\
    <html>\
        <body>\
            <h1>Goodbye World!</h1>\
        </body>\
    </html>";
    let json: Value = serde_json::from_str(r#"{"bool": false}"#).unwrap();
    let json = json.as_object().unwrap().clone();
    let template = Template::load_from_source("documentation", r#"
    &hello {Hello}
    &goodbye {Goodbye}
    /html {
       /body {
           /h1 {$std.if_else(component = &hello, condition = @bool, else = &goodbye) World!}
       }
    }
    "#);
     
     assert_eq!(template.json(json).no_locales().render("en").unwrap(), EXPECTED);
}