protected function renderTemplateString()
{
$this->output = $this->templateString;
if (null !== $this->data) {
// Render nested arrays first
foreach ($this->data as $key => $value) {
if (is_array($value) || $value instanceof \ArrayObject) {
$start = '[{' . $key . '}]';
$end = '[{/' . $key . '}]';
if (strpos($this->templateString, $start) !== false && strpos($this->templateString, $end) !== false) {
$loopCode = substr($this->templateString, strpos($this->templateString, $start));
$loopCode = substr($loopCode, 0, strpos($loopCode, $end) + strlen($end));
$loop = str_replace($start, '', $loopCode);
$loop = str_replace($end, '', $loop);
$outputLoop = '';
$i = 0;
foreach ($value as $ky => $val) {
if (is_array($val) || $val instanceof \ArrayObject) {
$l = $loop;
foreach ($val as $k => $v) {
// Check is value is stringable
if (is_object($v) && method_exists($v, '__toString') || !is_object($v) && !is_array($v)) {
$l = str_replace('[{' . $k . '}]', $v, $l);
}
}
$outputLoop .= $l;
} else {
// Check is value is stringable
if (is_object($val) && method_exists($val, '__toString') || !is_object($val) && !is_array($val)) {
$replace = !is_numeric($ky) ? '[{' . $ky . '}]' : '[{value}]';
$outputLoop .= str_replace($replace, $val, $loop);
}
}
$i++;
if ($i < count($value)) {
$outputLoop .= PHP_EOL;
}
}
$this->output = str_replace($loopCode, $outputLoop, $this->output);
}
}
}
// Render scalar values
foreach ($this->data as $key => $value) {
if (!is_array($value) && !$value instanceof \ArrayObject) {
// Check is value is stringable
if (is_object($value) && method_exists($value, '__toString') || !is_object($value) && !is_array($value)) {
$this->output = str_replace('[{' . $key . '}]', $value, $this->output);
}
}
}
}
}