From 7331124333616caf72bc9ccd1033e45a1f29a695 Mon Sep 17 00:00:00 2001 From: matthes Date: Wed, 25 Jul 2018 14:59:58 +0200 Subject: [PATCH] added custom sections --- docs/README.md | 2 +- src/TextTemplate.php | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/docs/README.md b/docs/README.md index 6683859..031373e 100644 --- a/docs/README.md +++ b/docs/README.md @@ -276,7 +276,7 @@ Some Content To use sections you must just set the callback: ```php -$textTemplate->setSectionCallback("sectionxy", function ($content, $params, $command, $context, $cmdParam) { +$textTemplate->addSection("sectionxy", function ($content, $params, $command, $context, $cmdParam) { return "Content to replace section content with"; }); ``` diff --git a/src/TextTemplate.php b/src/TextTemplate.php index f226b77..64e1b52 100644 --- a/src/TextTemplate.php +++ b/src/TextTemplate.php @@ -222,8 +222,13 @@ function ($matches) use (&$nestingIndex, &$indexCounter, &$li, $blockTags) { return $out; } else if ($slash == "/") { - if ( ! isset ($nestingIndex[$tag])) - throw new TemplateParsingException("Line {$li}: Opening tag not found for closing tag: '{$matches[0]}'"); + if ( ! isset ($nestingIndex[$tag])) { + if ( ! isset ($this->sections[$tag]) && ! in_array($tag, ["if", "for"])) + throw new TemplateParsingException("Line {$li}: No callback registred for section {{$tag}}{/{$tag}}"); + throw new TemplateParsingException( + "Line {$li}: Opening tag not found for closing tag: '{$matches[0]}'" + ); + } if (count ($nestingIndex[$tag]) == 0) throw new TemplateParsingException("Line {$li}: Nesting level does not match for closing tag: '{$matches[0]}'"); $curIndex = array_pop($nestingIndex[$tag]);