Skip to content

Missing variable outside CATCH causing a double-fault in Renderer.php  #27009

Closed
@HeiderSati

Description

@HeiderSati

OK

Preconditions (*)

  1. Magento 2.3.4
  2. MacBook Pro (OSX Catalina)
  3. -and- Linux (Ubuntu)

Steps to reproduce (*)

  1. Create a new Theme, with web/css folder empty inside.
  2. Create your default.xml and catalog_product_view.xml
  3. (Actually, you don't need to worry about re-producing this issue, I attach where exactly the bug is in your code, if you read my notes, you should see that it would fail causing blank-pages or errors sometimes due to double-fault), please see below:

Expected result (*)

  1. All pages to be rendered or errors reported in the log files. (standard)

Actual result (*)

  1. getting an error in: vendor/magento/framework/View/Page/Config/Renderer.php file with missing variable of $template.

  2. The issue is to do with the following code (line 429) on the above file, I attach the function below:

    protected function renderAssetHtml(\Magento\Framework\View\Asset\PropertyGroup $group)
    {
    $assets = $this->processMerge($group->getAll(), $group);
    $attributes = $this->getGroupAttributes($group);

     $template = "";
    
     $result = '';
     try {
         /** @var $asset \Magento\Framework\View\Asset\AssetInterface */
         foreach ($assets as $asset) {
             $template = $this->getAssetTemplate(
                 $group->getProperty(GroupedCollection::PROPERTY_CONTENT_TYPE),
                 $this->addDefaultAttributes($this->getAssetContentType($asset), $attributes)
             );
             $result .= sprintf($template, $asset->getUrl());
         }
     } catch (LocalizedException $e) {
         $this->logger->critical($e);
         $result .= sprintf($template, $this->urlBuilder->getUrl('', ['_direct' => 'core/index/notFound']));
     }
     return $result;
    

    }


The issue is that in this code, there is an exception occurring (which is fine/expected), however, inside the catch notice that there is a $template variable being referenced and PHP is throwing the error saying $template does not exists.

This is because $template only exists inside the foreach {} block and would not be accessible from the catch {} block, this causes Magento to render a blank page or come out with an error inside the catch which is a double-fault.

The solution is to add just before the try {} block the following:

 $template = "";

this should fix the issue, I already fixed it on mine and now Magento-2 is working better.

I hope this helps.

Regards
Heider

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions