1

If I set

'suppressTemplateErrors' => true 

in the config/general.php

This (templates/_pages/_entry.twig):

{% extends [
'_pages/'~entry.slug|default,
"_pages/" ~ entry.type|default,
"_pages/default"
] %}

Gets me a blank page, since apparently it is seen as a mistake, and the execution of the twig template is stopped. However, when suppressTemplateErrors is set to false, there is no problem. the code is supposed to search for a template with the name of an entry slug, if that doesn't exist look for one with the name of the type, and if that doesn't exist, load the default template.

Logs (web.log):

2017-12-26 15:54:02 [::1][1][-][error][craft\web\twig\TemplateLoaderException] craft\web\twig\TemplateLoaderException: Coud not find template “_pages/about” . in /Users/username/Code/mywebsite/vendor/craftcms/cms/src/web/twig/TemplateLoader.php:125
Stack trace:
#0 /Users/username/Code/mywebsite/vendor/craftcms/cms/src/web/twig/TemplateLoader.php(76): craft\web\twig\TemplateLoader->_resolveTemplate('_pages/about')
#1 /Users/username/Code/mywebsite/vendor/twig/twig/lib/Twig/Environment.php(270): craft\web\twig\TemplateLoader->getCacheKey('_pages/about')
#2 /Users/username/Code/mywebsite/vendor/twig/twig/lib/Twig/Environment.php(350): Twig_Environment->getTemplateClass('_pages/about')
#3 /Users/username/Code/mywebsite/vendor/craftcms/cms/src/web/twig/Environment.php(30): Twig_Environment->loadTemplate('_pages/about', NULL)
#4 /Users/username/Code/mywebsite/vendor/twig/twig/lib/Twig/Environment.php(478): craft\web\twig\Environment->loadTemplate('_pages/about')
#5 /Users/username/Code/mywebsite/vendor/twig/twig/lib/Twig/Template.php(318): Twig_Environment->resolveTemplate(Array)
#6 /Users/username/Code/mywebsite/storage/runtime/compiled_templates/74/74f99748b2c43647e59f94561c58e670362eb7351e4ca94b81c3fcb1a4265158.php(10): Twig_Template->loadTemplate(Array, '_pages/_entry', 2)
#7 /Users/username/Code/mywebsite/vendor/twig/twig/lib/Twig/Template.php(99): __TwigTemplate_49b39b86eb9a0dbc650dce81e0d17fe48175bd75d54e21236890fb8d13a20492->doGetParent(Array)
#8 /Users/username/Code/mywebsite/storage/runtime/compiled_templates/74/74f99748b2c43647e59f94561c58e670362eb7351e4ca94b81c3fcb1a4265158.php(16): Twig_Template->getParent(Array)
#9 /Users/username/Code/mywebsite/vendor/twig/twig/lib/Twig/Template.php(389): __TwigTemplate_49b39b86eb9a0dbc650dce81e0d17fe48175bd75d54e21236890fb8d13a20492->doDisplay(Array, Array)
#10 /Users/username/Code/mywebsite/vendor/craftcms/cms/src/web/twig/Template.php(51): Twig_Template->displayWithErrorHandling(Array, Array)
#11 /Users/username/Code/mywebsite/vendor/twig/twig/lib/Twig/Template.php(366): craft\web\twig\Template->displayWithErrorHandling(Array, Array)
#12 /Users/username/Code/mywebsite/vendor/craftcms/cms/src/web/twig/Template.php(32): Twig_Template->display(Array, Array)
#13 /Users/username/Code/mywebsite/vendor/twig/twig/lib/Twig/Template.php(374): craft\web\twig\Template->display(Array)
#14 /Users/username/Code/mywebsite/vendor/twig/twig/lib/Twig/Environment.php(289): Twig_Template->render(Array)
#15 /Users/username/Code/mywebsite/vendor/craftcms/cms/src/web/View.php(292): Twig_Environment->render('_pages/_entry', Array)
#16 /Users/username/Code/mywebsite/vendor/craftcms/cms/src/web/View.php(340): craft\web\View->renderTemplate('_pages/_entry', Array)
#17 /Users/username/Code/mywebsite/vendor/craftcms/cms/src/web/Controller.php(121): craft\web\View->renderPageTemplate('_pages/_entry', Array)
#18 /Users/username/Code/mywebsite/vendor/craftcms/cms/src/controllers/TemplatesController.php(79): craft\web\Controller->renderTemplate('_pages/_entry', Array)
#19 [internal function]: craft\controllers\TemplatesController->actionRender('_pages/_entry', Array)
#20 /Users/username/Code/mywebsite/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#21 /Users/username/Code/mywebsite/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#22 /Users/username/Code/mywebsite/vendor/craftcms/cms/src/web/Controller.php(80): yii\base\Controller->runAction('render', Array)
#23 /Users/username/Code/mywebsite/vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller->runAction('render', Array)
#24 /Users/username/Code/mywebsite/vendor/craftcms/cms/src/web/Application.php(242): yii\base\Module->runAction('templates/rende...', Array)
#25 /Users/username/Code/mywebsite/vendor/yiisoft/yii2/web/Application.php(103): craft\web\Application->runAction('templates/rende...', Array)
#26 /Users/username/Code/mywebsite/vendor/craftcms/cms/src/web/Application.php(207): yii\web\Application->handleRequest(Object(craft\web\Request))
#27 /Users/username/Code/mywebsite/vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest(Object(craft\web\Request))
#28 /Users/username/Code/mywebsite/www/index.php(35): yii\base\Application->run()
#29 {main}
  • What does the craft.log say when suppressTemplateErrors is true? – RitterKnight Dec 26 '17 at 15:41
  • 2017-12-26 15:54:02 [::1][1][-][error][craft\web\twig\TemplateLoaderException] craft\web\twig\TemplateLoaderException: Coud not find template “_pages/about” . in /Users/username/Code/mywebsite/vendor/craftcms/cms/src/web/twig/TemplateLoader.php:125 – manomoul Dec 26 '17 at 16:42
  • Why do you need suppressTemplateErrors set to true? What is the purpose of adding the "|default" part to the first two template calls? – AbbeyDesign Dec 27 '17 at 14:35
1

So just as an information architecture kind of thing, you might want to have just one _layout.twig that all of your entry types extends, and just include various {% block %} tags in the right places for the templates to fill in.

That way, if you make major changes to the abstract pages (say, adding meta info, or changing JavaScript that's loaded site-wide, etc.) you don't have to make those changes in a number of places.

You can also leverage include and embed and you'll likely end up with a more manageable template hierarchy in the end.

That said, the problem you're having is that it's hitting this code block:

```

public function loadTemplate($name, $index = null)
{
    try
    {
        return parent::loadTemplate($name, $index);
    }
    catch (\Twig_Error $e)
    {
        if (craft()->config->get('suppressTemplateErrors'))
        {
            // Just log it and return an empty template
            craft()->errorHandler->logException($e);

            $twig = craft()->templates->getTwig('Twig_Loader_String');
            return $twig->loadTemplate('');
        }
        else
        {
            throw $e;
        }
    }
}

```

So while it normally would throw an exception, which Twig handles and then moves on to try the next template in the array you've passed it, with suppressTemplateErrors => true it just immediately returns an empty template.

You can file a bug report if you like, because this does appear to short-circuit the normal Twig fallback mechanism for template rendering, but I'd honestly re-examine how you're doing things. Depending on suppressing template errors for the site to work feels wrong.

  • we actually are extending a _layout.twig file, with a {% block %} and everything :) We just use this method for when we have entries that are slightly different from the others, to load a slightly different template. We do this is stead of creating new singles for everything. Seemed like a neat way to keep the cms clean. Unless you know a better method? For example: In the cms, the contact page has the exact same fields as the other pages, but in the front it needs to output a contact form as well, so we create a template: pages/contact.twig . – manomoul Dec 26 '17 at 17:03
  • I am with @khalwat, you probably don't want to do this on an extends level but your approach is fine for includes and embeds... I use it all the time. – AbbeyDesign Dec 27 '17 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.