render

Render a template at ctfe

  1. string render(string tplValue, T controller)
  2. string render(T controller)
  3. string render(T controller)
    string
    render
    (
    Token[] tokens
    T
    Components...
    )

Examples

Rendering an empty string

enum tpl = "";
struct Controller {}

render(tpl, Controller()).should.equal("");

Rendering an empty string at ctfe

enum tpl = "";
struct Controller {}

render!(tpl)(Controller()).should.equal("");

Rendering a string value

enum tpl = "{{value}}";

struct Controller {
  string value;
}

render(tpl, Controller("2")).should.equal("2");

Rendering a string value at ctfe

enum tpl = "{{value}}";

struct Controller {
  string value;
}

render!(tpl)(Controller("2")).should.equal("2");

Rendering a string property

enum tpl = "{{value}}";

struct Controller {
  string value() {
    return "3";
  }
}

render(tpl, Controller()).should.equal("3");

Rendering a string property at ctfe

enum tpl = "{{value}}";

struct Controller {
  string value() {
    return "3";
  }
}

render!(tpl)(Controller()).should.equal("3");

Rendering a string property

enum tpl = "{{value}}";

struct Controller {
  string value() {
    return "3";
  }
}

render(tpl, Controller()).should.equal("3");

Rendering a string property at ctfe

enum tpl = "{{value}}";

struct Controller {
  string value() {
    return "3";
  }
}

render!(tpl)(Controller()).should.equal("3");

Rendering a numeric property

enum tpl = "{{value}}";

struct Controller {
  int value() {
    return 3;
  }
}

render(tpl, Controller()).should.equal("3");

Rendering a numeric property at ctfe

enum tpl = "{{value}}";

struct Controller {
  int value() {
    return 3;
  }
}

render!(tpl)(Controller()).should.equal("3");

Rendering a numeric struct subproperty

enum tpl = "{{child.value}}";

struct Child {
  int value = 3;
}

struct Controller {
  Child child() {
    return Child();
  }
}

render(tpl, Controller()).should.equal("3");

Rendering a numeric struct subproperty at ctfe

enum tpl = "{{child.value}}";

struct Child {
  int value = 3;
}

struct Controller {
  Child child() {
    return Child();
  }
}

render!(tpl)(Controller()).should.equal("3");

Rendering a numeric class subproperty

enum tpl = "{{child.value}}";

class Child {
  int value = 3;
}

struct Controller {
  Child child() {
    return new Child();
  }
}

render(tpl, Controller()).should.equal("3");

Rendering a numeric class subproperty

enum tpl = "{{child.value}}";

class Child {
  int value = 3;
}

struct Controller {
  Child child() {
    return new Child();
  }
}

render(tpl, Controller()).should.equal("3");

Rendering a numeric class member

enum tpl = "{{child.value}}";

class Child {
  int value = 3;
}

struct Controller {
  Child child;
}

render(tpl, Controller( new Child() )).should.equal("3");

Rendering a numeric struct member

enum tpl = "{{child.value}}";

struct Child {
  int value = 3;
}

struct Controller {
  Child child;
}

render(tpl, Controller()).should.equal("3");

Rendering a string property from a class controller

enum tpl = "{{value}}";

class Controller {
  string value() {
    return "3";
  }
}

render(tpl, new Controller()).should.equal("3");

it should not render a string method with parameters as value

enum tpl = "{{value}}";

class Controller {
  string value(string param) {
    return param;
  }
}

render(tpl, new Controller())
  .should
  .throwException!RenderException
    .withMessage
      .equal("`Controller.value` can not be rendered as a value. Did you forget the template parameters?");

Rendering a string property from a class controller with plain values

enum tpl = "a {{value}} b {{value}} c";

class Controller {
  string value() {
    return "3";
  }
}

auto result = render(tpl, new Controller());
result.should.equal("a 3 b 3 c");

Rendering a helper with string param

enum tpl = `{{helper "value"}}`;

struct Controller {
  string helper(string value) {
    return value;
  }
}

render!(tpl)(Controller()).should.equal("value");

Rendering a helper with int param

enum tpl = `{{helper 5}}`;

struct Controller {
  int helper(int value) {
    return value;
  }
}

render!(tpl)(Controller()).should.equal("5");

Rendering a nested helper with int param

enum tpl = `{{child.helper 5}}`;

struct Child {
  int helper(int value) {
    return value;
  }
}

struct Controller {
  Child child;
}

render!(tpl)(Controller()).should.equal("5");

Rendering a helper with property value

enum tpl = `{{helper value}}`;

struct Controller {
  int helper(int value) {
    return value;
  }

  int value = 12;
}

render!(tpl)(Controller()).should.equal("12");

Rendering a helper with computed value

enum tpl = `{{helper value}}`;

struct Controller {
  int helper(int value) {
    return value;
  }

  int value() {
    return 8;
  }
}

render!(tpl)(Controller()).should.equal("8");

Rendering a nested helper with bool param

enum tpl = `{{child.helper 5 true "test"}}`;

struct Child {
  string helper(int number, bool value, string str) {
    return number.to!string ~ " " ~ value.to!string ~ " " ~ str;
  }
}

struct Controller {
  Child child;
  string value;
}

render!(tpl)(Controller()).should.equal("5 true test");

Rendering undefined helpers should throw an exception

enum tpl1 = `{{helper 5 true "test"}}`;
enum tpl2 = `{{child.other 5 true "test"}}`;
enum tpl3 = `{{value 5 true "test"}}`;
enum tpl4 = `{{child.value 5 true "test"}}`;

struct Child {
  string helper(int number, bool value, string str) {
    return number.to!string ~ " " ~ value.to!string ~ " " ~ str;
  }

  string value;
}

struct Controller {
  Child child;
}

render(tpl1, Controller())
  .should
  .throwException!RenderHelperException
    .withMessage
      .equal("`helper` can not be rendered becaues it is not defined.");

render(tpl2, Controller())
  .should
  .throwException!RenderHelperException
    .withMessage
      .equal("`child.other` can not be rendered becaues it is not defined.");

render(tpl3, Controller())
  .should
  .throwException!RenderHelperException
    .withMessage
      .equal("`value` can not be rendered becaues it is not defined.");

render(tpl4, Controller())
  .should
  .throwException!RenderHelperException
    .withMessage
      .equal("The helpers must be inside a struct or a class.");

Meta