Skip to content

Commit

Permalink
Merge pull request #9 from splashsky/implementOneLinerBlocks
Browse files Browse the repository at this point in the history
Implement one liner blocks
  • Loading branch information
splashsky authored Apr 3, 2023
2 parents ca110d6 + 3892298 commit 01fab8a
Showing 1 changed file with 9 additions and 24 deletions.
33 changes: 9 additions & 24 deletions src/Modello.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ class Modello
private array $handlers = [
'handleIncludes',
'handleBlocks',
'handleHasBlock',
'handleBlockMissing',
'handleBlockConditionals',
'handleYields',
'handleEchoes',
'handleEscapedEchoes',
Expand Down Expand Up @@ -208,18 +207,11 @@ private function handleIncludes(string $view): string
private function handleBlocks(string $page): string
{
preg_match_all('/@block\( ?\'(\w*?)\' ?\)(.*?)@endblock/is', $page, $matches, PREG_SET_ORDER);
preg_match_all('/@block\( ?\'(\w*?)\', ?\'(\N*?)\' ?\)/is', $page, $inlineMatches, PREG_SET_ORDER);
$matches = array_merge($matches, $inlineMatches);

foreach ($matches as $match) {
if (!array_key_exists($match[1], $this->blocks)) {
$this->blocks[$match[1]] = '';
}

if (strpos($match[2], '@parent') === false) {
$this->blocks[$match[1]] = trim($match[2]);
} else {
$this->blocks[$match[1]] = trim(str_replace('@parent', $this->blocks[$match[1]], $match[2]));
}

$this->blocks[$match[1]] = $match[2];
$page = str_replace($match[0], '', $page);
}

Expand Down Expand Up @@ -285,24 +277,17 @@ private function handleComment(string $page): string
}

// A directive to test whether we have a block by a given key
function handleHasBlock(string $page): string
function handleBlockConditionals(string $page): string
{
preg_match_all('/@hasblock\( ?\'(\w*?)\' ?\)(.*?)@endif/is', $page, $matches, PREG_SET_ORDER);
preg_match_all('/@hasblock\( ?\'(\w*?)\' ?\)(.*?)@endif/is', $page, $has, PREG_SET_ORDER);
preg_match_all('/@blockmissing\( ?\'(\w*?)\' ?\)(.*?)@endif/is', $page, $missing, PREG_SET_ORDER);

foreach ($matches as $match) {
foreach ($has as $match) {
$replace = array_key_exists($match[1], $this->blocks) ? $match[2] : '';
$page = str_replace($match[0], $replace, $page);
}

return $page;
}

// Directive that does the opposite of @hasblock
function handleBlockMissing(string $page): string
{
preg_match_all('/@blockmissing\( ?\'(\w*?)\' ?\)(.*?)@endif/is', $page, $matches, PREG_SET_ORDER);

foreach ($matches as $match) {
foreach ($missing as $match) {
$replace = !array_key_exists($match[1], $this->blocks) ? $match[2] : '';
$page = str_replace($match[0], $replace, $page);
}
Expand Down

0 comments on commit 01fab8a

Please sign in to comment.