Skip to content

Commit

Permalink
Refactor: Use setBuiltin() from constructor instead of direct access
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Jan 2, 2014
1 parent 57111de commit 0146e0a
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/main/php/com/handlebarsjs/HandlebarsEngine.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct() {
parent::__construct();

// Each: Traverse lists and hashes
$this->builtin['each']= function($items, $context, $options) {
$this->setBuiltin('each', function($items, $context, $options) {
$target= $context->lookup($options[0]);
$out= '';
if ($context->isList($target)) {
Expand All @@ -49,46 +49,44 @@ public function __construct() {
}
}
return $out;
};
});

// If: Evaluate content in same context if value is truthy
$this->builtin['if']= function($items, $context, $options) {
$this->setBuiltin('if', function($items, $context, $options) {
$target= $context->lookup($options[0]);
if ($context->isTruthy($target)) {
return $items->evaluate($context);
}
return '';
};
});

// Unless: Evaluate content in same context if value is falsy
$this->builtin['unless']= function($items, $context, $options) {
$this->setBuiltin('unless', function($items, $context, $options) {
$target= $context->lookup($options[0]);
if ($context->isTruthy($target)) {
return '';
}
return $items->evaluate($context);
};
});

// With: Evaluate content in context defined by argument
$this->builtin['with']= function($items, $context, $options) {
$this->setBuiltin('with', function($items, $context, $options) {
$target= $context->lookup($options[0]);
if ($context->isTruthy($target)) {
return $items->evaluate($context->asContext($target));
}
return '';
};
});

// This: Access the current value in the context
$this->builtin['this']= function($items, $context, $options) {
$this->setBuiltin('this', function($items, $context, $options) {
$variable= $context->lookup(null);
if ($context->isHash($variable) || $context->isList($variable)) {
return current($context->asTraversable($variable));
} else {
return $variable;
}
};

$this->helpers= $this->builtin; // Initially
});
}

/**
Expand Down

0 comments on commit 0146e0a

Please sign in to comment.