public function __toString() { try { if (isset($this->Result) && !is_string($this->Result)) { return View::ViewReplaceTags($this->Result->Template, View::KeysToTags(['CSS' => '<style>' . $this->CSS . '</style>', 'JS' => '<script type="text/javascript">' . $this->JS . '</script>'])); } else { return $this->Result; } } catch (Exception $e) { die('Error: ' . $e->getMessage()); } return ''; }
static function ViewReplaceTags($tpl, $data) { // This will replace tags of the {Tag}{/Tag} kind which will loop over the given array and handle the inner template preg_match_all('/{{([\\w]+) ?([^}]*?)(?:}}((?:[^{]*?|(?R)|{[\\w]*?})*){{\\/\\1}})|{([\\w]*?)}/s', $tpl, $matches); // |{[\w]*?} // If we have a match for a {Tag}{/Tag} then lets handle them first if (isset($matches[0][0])) { foreach ($matches[0] as $matchI => $ToBeReplaced) { //$ToBeReplaced = isset($matches[0][0])?$matches[0][0]:$matches[0]; $Tag = isset($matches[1][$matchI]) ? $matches[1][$matchI] : $matches[1]; $Condition = isset($matches[2][$matchI]) ? $matches[2][$matchI] : $matches[2]; $NewTpl = isset($matches[3][$matchI]) ? $matches[3][$matchI] : $matches[3]; $TempTPL = ''; if (is_string($ToBeReplaced)) { if ($Tag == '') { $Tag = $ToBeReplaced; } else { $Tag = '{' . $Tag . '}'; } if (isset($data[$Tag])) { $value = $data[$Tag]; if (is_array($value)) { $TempTPL = ''; $c = 0; if (count($value) != count($value, COUNT_RECURSIVE)) { foreach ($value as $TmpKey => $TmpVal) { $TempNewTpl = ltrim($NewTpl, " \t"); if (is_int($TmpKey) && is_array($TmpVal)) { $TmpKey = $Tag; $TmpArray = $TmpVal; } else { $TmpKey = '{' . $TmpKey . '}'; $TmpArray = [$TmpKey => $TmpVal]; } if (is_string($TmpVal)) { $TempNewTpl = str_replace($TmpKey, $TmpVal, $TempNewTpl); } if (is_string($TmpKey) && is_array($TmpVal)) { $TempNewTpl = self::ViewReplaceTags($TempNewTpl, View::KeysToTags($TmpArray)); } $TempTPL .= $TempNewTpl; } } else { foreach ($value as $i => $val) { if (is_int($i)) { $TempValue = $value; $TempNewTpl = ''; $TempValue['_'] = $val; $TempNewTpl = ltrim($NewTpl, " \t"); $TempValue = View::KeysToTags($TempValue); $Keys = array_keys($TempValue); $Vals = array_values($TempValue); $TempTPL .= str_replace($Keys, $Vals, $TempNewTpl); } $c++; } } } else { $TempTPL = (string) $value; // This forces objects like controllers to a string } $tpl = str_replace($ToBeReplaced, $TempTPL, $tpl); } } } } return $tpl; }