IfComponent

Component that will handle the if blocks

class IfComponent : HbsComponent!"" {}

Constructors

this
this(bool value)

Members

Functions

render
string render()

Manifest constants

ComponentName
enum ComponentName;
Undocumented in source.

Examples

Render an if block if the condition is satisfied

string mockYeld(Token[] tokens) {
  tokens.length.should.equal(1);
  return tokens[0].value;
}

auto mockLifecycle = new MockLifecycle();
mockLifecycle.onYield = &mockYeld;

auto condition = new IfComponent(true);
condition.lifecycle = mockLifecycle;

condition.content = [ Token(Token.Type.plain, "true") ];

condition.render!(IfComponent).should.equal("true");

Don't render an if block if the condition is not satisfied

bool called;

string mockYeld(Token[] tokens) {
  called = true;
  return tokens[0].value;
}

auto mockLifecycle = new MockLifecycle();
mockLifecycle.onYield = &mockYeld;

auto condition = new IfComponent(false);
condition.lifecycle = mockLifecycle;

condition.content = [ Token(Token.Type.plain, "true") ];

called.should.equal(false);
condition.render!(IfComponent).should.equal("");

Render an if until the else token

string mockYeld(Token[] tokens) {
  tokens.length.should.equal(1);
  return tokens[0].value;
}

auto mockLifecycle = new MockLifecycle();
mockLifecycle.onYield = &mockYeld;

auto condition = new IfComponent(true);
condition.lifecycle = mockLifecycle;

condition.content = [
  Token(Token.Type.plain, "true"),
  Token(Token.Type.value, "else"),
  Token(Token.Type.plain, "false") ];

condition.render!(IfComponent).should.equal("true");

Render the else when the if is not satisfied

string mockYeld(Token[] tokens) {
  tokens.length.should.equal(1);
  return tokens[0].value;
}

auto mockLifecycle = new MockLifecycle();
mockLifecycle.onYield = &mockYeld;

auto condition = new IfComponent(false);
condition.lifecycle = mockLifecycle;

condition.content = [
  Token(Token.Type.plain, "true"),
  Token(Token.Type.value, "else"),
  Token(Token.Type.plain, "false") ];

condition.render!(IfComponent).should.equal("false");

Render the else when the if is not satisfied

string mockYeld(Token[] tokens) {
  tokens.length.should.equal(1);
  return tokens[0].value;
}

bool mockBoolEval(string value) {
  return value == "true";
}

auto mockLifecycle = new MockLifecycle();
mockLifecycle.onYield = &mockYeld;
mockLifecycle.onEvaluateBoolean = &mockBoolEval;

auto condition = new IfComponent(false);
condition.lifecycle = mockLifecycle;

condition.content = [
  Token(Token.Type.plain, "true"),
  Token(Token.Type.helper, "else", Properties("if false")),
  Token(Token.Type.plain, "3"),
  Token(Token.Type.helper, "else", Properties("if true")),
  Token(Token.Type.plain, "2") ];

condition.render!(IfComponent).should.equal("2");

Meta