Rendering components with no blocks
enum tpl = `{{Component a=value b=3}}`; struct Controller { int value() { return 2; } } render!(Controller, Component)(tpl, Controller()).should.equal("5");
Rendering components with blocks
enum tpl = `{{#Component a=value b=3}}text 3+{{value}}={{/Component}};`; struct Controller { int value() { return 2; } } render!(Controller, Component)(tpl, Controller()).should.equal("text 3+2=5;");
Rendering if block
enum tpl = `{{#if value}}text{{/if}}`; struct Controller { int value() { return 2; } } render(tpl, Controller()).should.equal("text");
Rendering if block at ctfe
enum tpl = `{{#if value}}text{{/if}}`; struct Controller { int value() { return 2; } } render!(tpl)(Controller()).should.equal("text");
Don't render if blocks if the condition is not satisfied
enum tpl = `{{#if false}}text{{/if}}`; struct Controller { int value() { return 2; } } render!(Controller)(tpl, Controller()).should.equal("");
Rendering if block and stop at the else block if the value is evaluated to true
struct Controller { bool value = true; } enum tpl = `{{#if true}}text{{else value}}other{{/if}}`; render(tpl, Controller()).should.equal("text");
Rendering if block with new lines and stop at the else block if the value is evaluated to true
struct Controller { bool value = true; } enum tpl = " {{#if true}}\n" ~ "text\n" ~ "{{else value}}\n" ~ "other{{/if}}\n"; render(tpl, Controller()).should.equal(" text");
Rendering at ctfe an if block and stop at the else block if the value is evaluated to true
struct Controller { bool value = true; } enum tpl = `{{#if true}}text{{else value}}other{{/if}}`; render!(tpl)(Controller()).should.equal("text");
Rendering at ctfe the right else
struct Controller { bool value = true; } enum tpl = `{{#if false}}text{{else false}}other{{else value}}this one{{else}}default{{/if}}`; render!(tpl)(Controller()).should.equal("this one");
Rendering at ctfe an if block should render the final else
struct Controller { bool value = true; } enum tpl = `{{#if false}}text{{else false}}other{{else}}{{value}}{{/if}}`; render!(tpl)(Controller()).should.equal("true");
Rendering at ctfe nested if blocks
struct Controller { bool value = true; } enum tpl = `{{#if false}}text{{else}} <-- {{#if value}}true{{else}}false{{/if}} --> {{/if}}`; render!(tpl)(Controller()).should.equal(" <-- true --> ");
Rendering an each block
struct Controller { int[] list() { return [1,2,3,4]; } } enum tpl = `{{#each list as |item|}} {{item}} {{/each}}`; render(tpl, Controller()).should.equal(" 1 2 3 4 ");
Rendering an each block with a list of structs
struct Child { string name; } struct Controller { Child[] list; } enum tpl = `{{#each list as |item|}} {{item.name}} {{/each}}`; render(tpl, Controller([Child("name1"), Child("name2")])).should.equal(" name1 name2 ");
Rendering an indexed each block
struct Controller { int[] list() { return [1,2,3,4]; } } enum tpl = `{{#each list as |item index|}} {{index}}{{item}} {{/each}}`; render!(Controller)(tpl, Controller()).should.equal(" 01 12 23 34 ");
Rendering an indexed each block with ctfe parsing
struct Controller { int[] list() { return [1,2,3,4]; } } enum tpl = `{{#each list as |item index|}} {{index}}{{item}} {{/each}}`; render!(tpl)(Controller()).should.equal(" 01 12 23 34 ");
Rendering an indexed each block with ctfe parsing
struct Controller { int[string] list() { return ["a": 1, "b": 2, "c": 3,"d": 4]; } } enum tpl = `{{#each list as |item index|}} {{index}}{{item}} {{/each}}`; render!(tpl)(Controller()).should.equal(" c3 a1 b2 d4 ");
Rendering an nested indexed each block with ctfe parsing
struct Child { int[] numbers() { return [1,2,3,4]; } } struct Controller { Child[] list() { return [Child(), Child()]; } } enum tpl = `{{#each list as |child index|}} {{index}}.[{{#each child.numbers as |number index|}}{{index}}.{{number}} {{/each}}] {{/each}}`; render!(tpl)(Controller()).should.equal(" 0.[0.1 1.2 2.3 3.4 ] 1.[0.1 1.2 2.3 3.4 ] ");
Rendering parent values in an nested indexed each block with ctfe parsing
struct Child { int[] numbers() { return [1,2,3,4]; } } struct Controller { string parentValue1 = "test1"; string parentValue2 = "test2"; Child[] list() { return [Child(), Child()]; } } enum tpl = `{{#each list as |child parent_index|}} {{parentValue1}} [{{#each child.numbers as |number index|}}{{parentValue2}} {{parent_index}}.{{index}}.{{number}} {{/each}}] {{/each}}`; render!(tpl)(Controller()).should.equal(" test1 [test2 0.0.1 test2 0.1.2 test2 0.2.3 test2 0.3.4 ] test1 [test2 1.0.1 test2 1.1.2 test2 1.2.3 test2 1.3.4 ] ");
Rendering scope component with helper
struct Controller { int[] list = [1,2,3,4]; string helper(int a, int b){ return a.to!string ~ ":" ~ b.to!string; } } enum tpl = `{{#scope "list" "item" "1" "index" }} {{helper index item}} {{/scope}}`; render(tpl, Controller()).should.equal(" 1:2 ");