21 public function parseFile(
string $source, array $variables = [], array $rawVariables = []): string
23 return rtrim($this->
parse(file_get_contents($source), $variables, $rawVariables), PHP_EOL);
37 public function parse(
string $content, array $variables = [], array $rawVariables = []): string
39 $content = preg_replace_callback(
'#\{\s*if\s*(\w+)\s*\}(.*)(?:\{\s*else\s*\}(.*))?\{\s*endif\s*\}#Ums',
function ($matches) use ($variables, $rawVariables) {
40 $condition = $variables[$matches[1]] ?? $rawVariables[$matches[1]] ??
false;
42 return $this->
parse($matches[2], $variables, $rawVariables);
44 if (count($matches) > 3) {
45 return $this->
parse($matches[3], $variables, $rawVariables);
50 foreach ($variables as $key => $value) {
51 if (is_array($value)) {
52 $content = preg_replace_callback(
'#\{\s*' . $key .
'\s*\}((?:.|[\n\r])*)\{/\s*' . $key .
'\s*\}#Um',
function (array $matches) use ($value, $variables, $rawVariables) {
54 foreach ($value as $innerElement => $attributes) {
55 $innerContent .= $this->
parse($matches[1], array_merge($variables, $attributes), $rawVariables);
61 $content = preg_replace(
'#\{' . $key .
'\}#', htmlspecialchars($value), $content);
65 foreach ($rawVariables as $key => $value) {
66 if (is_array($value)) {
67 $content = preg_replace_callback(
'#\{\s*' . $key .
'\s*\}((?:.|[\n\r])*)\{/\s*' . $key .
'\s*\}#Um',
function (array $matches) use ($value, $variables, $rawVariables) {
69 foreach ($value as $innerElement => $attributes) {
70 $innerContent .= $this->
parse($matches[1], $variables, array_merge($rawVariables, $attributes));
76 $content = preg_replace(
'#\{' . $key .
'\}#', $value, $content);
Parses files to insert data.
parseFile(string $source, array $variables=[], array $rawVariables=[])
parse(string $content, array $variables=[], array $rawVariables=[])