Skip to content

Commit

Permalink
[BUGFIX] Allow renderSection() to be called directly (#252)
Browse files Browse the repository at this point in the history
Refactors AbstractTemplateView slightly so that the parsed template reading code can be called from both render() and renderSection(), causing the template to be loaded correctly.

Without this patch it is not possible to call renderSection() via the View's API without getting
an error that no parsed template exists.

Also adds a public method on TemplateCompiler to reset the current processing state.
  • Loading branch information
NamelessCoder authored and Marc Neuhaus committed Jan 16, 2017
1 parent 2551139 commit 79ec2af
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
10 changes: 10 additions & 0 deletions src/Core/Compiler/TemplateCompiler.php
Expand Up @@ -168,6 +168,16 @@ public function get($identifier)
return $this->syntaxTreeInstanceCache[$identifier];
}

/**
* Resets the currently processing state
*
* @return void
*/
public function reset()
{
$this->currentlyProcessingState = null;
}

/**
* @param string $identifier
* @param ParsingState $parsingState
Expand Down
48 changes: 32 additions & 16 deletions src/View/AbstractTemplateView.php
Expand Up @@ -167,26 +167,19 @@ public function assignMultiple(array $values)
*/
public function render($actionName = null)
{
$controllerName = $this->baseRenderingContext->getControllerName();
$templateParser = $this->baseRenderingContext->getTemplateParser();
$templatePaths = $this->baseRenderingContext->getTemplatePaths();
if ($actionName === null) {
$actionName = $this->baseRenderingContext->getControllerAction();
$renderingContext = $this->getCurrentRenderingContext();
$templateParser = $renderingContext->getTemplateParser();
$templatePaths = $renderingContext->getTemplatePaths();
if ($actionName) {
$actionName = ucfirst($actionName);
$renderingContext->setControllerAction($actionName);
}
$actionName = ucfirst($actionName);
try {
$parsedTemplate = $templateParser->getOrParseAndStoreTemplate(
$templatePaths->getTemplateIdentifier($controllerName, $actionName),
function($parent, TemplatePaths $paths) use ($controllerName, $actionName) {
return $paths->getTemplateSource($controllerName, $actionName);;
}
);
$parsedTemplate = $this->getCurrentParsedTemplate();
} catch (PassthroughSourceException $error) {
return $error->getSource();
}

$parsedTemplate->addCompiledNamespaces($this->baseRenderingContext);

if (!$parsedTemplate->hasLayout()) {
$this->startRendering(self::RENDERING_TEMPLATE, $parsedTemplate, $this->baseRenderingContext);
$output = $parsedTemplate->render($this->baseRenderingContext);
Expand Down Expand Up @@ -233,7 +226,11 @@ public function renderSection($sectionName, array $variables = [], $ignoreUnknow
$renderingTypeOnNextLevel = $this->getCurrentRenderingType();
}

$parsedTemplate = $this->getCurrentParsedTemplate();
try {
$parsedTemplate = $this->getCurrentParsedTemplate();
} catch (PassthroughSourceException $error) {
return $error->getSource();
}

if ($parsedTemplate->isCompiled()) {
$methodNameOfSection = 'section_' . sha1($sectionName);
Expand Down Expand Up @@ -349,7 +346,26 @@ protected function getCurrentRenderingType()
protected function getCurrentParsedTemplate()
{
$currentRendering = end($this->renderingStack);
return $currentRendering['parsedTemplate'] ? $currentRendering['parsedTemplate'] : $this->getCurrentRenderingContext()->getTemplateCompiler()->getCurrentlyProcessingState();
$renderingContext = $this->getCurrentRenderingContext();
$parsedTemplate = $currentRendering['parsedTemplate'] ? $currentRendering['parsedTemplate'] : $renderingContext->getTemplateCompiler()->getCurrentlyProcessingState();
if ($parsedTemplate) {
return $parsedTemplate;
}
$templatePaths = $renderingContext->getTemplatePaths();
$templateParser = $renderingContext->getTemplateParser();
$controllerName = $renderingContext->getControllerName();
$actionName = $renderingContext->getControllerAction();
$parsedTemplate = $templateParser->getOrParseAndStoreTemplate(
$templatePaths->getTemplateIdentifier($controllerName, $actionName),
function($parent, TemplatePaths $paths) use ($controllerName, $actionName) {
return $paths->getTemplateSource($controllerName, $actionName);
}
);
if ($parsedTemplate->isCompiled()) {
$parsedTemplate->addCompiledNamespaces($this->baseRenderingContext);
}
$renderingContext->getTemplateCompiler()->reset();
return $parsedTemplate;
}

/**
Expand Down

0 comments on commit 79ec2af

Please sign in to comment.