2

Trying to set-up a Craft site on a local environment. The site is currently live and functions well on prod. On the local environment, the site seems to work fine, except that every time I try to access an admin page, I get the following error:

Twig_Error_Runtime

Variable "entry" does not exist in "layouts/main/_layout" at line 29 (C:\wamp\www\stage\craft\app\vendor\twig\twig\lib\Twig\Template.php:332)

#0 C:\wamp\www\stage\craft\storage\runtime\compiled_templates\b3\54\0e5036bda0f90c30db509f858e96127617afb2773e83d81aa61dc5b4e29b.php(47): Twig_Template->getContext(Array, 'entry')
#1 C:\wamp\www\stage\craft\app\vendor\twig\twig\lib\Twig\Template.php(279): __TwigTemplate_b3540e5036bda0f90c30db509f858e96127617afb2773e83d81aa61dc5b4e29b->doDisplay(Array, Array)
#2 C:\wamp\www\stage\craft\app\vendor\twig\twig\lib\Twig\Template.php(253): Twig_Template->displayWithErrorHandling(Array, Array)
#3 C:\wamp\www\stage\craft\storage\runtime\compiled_templates\95\c9\45c04614017a210d5882c1a2a1f87467f3a26a51d13ab2f7ee1ff3b62ffe.php(26): Twig_Template->display(Array, Array)
#4 C:\wamp\www\stage\craft\app\vendor\twig\twig\lib\Twig\Template.php(279): __TwigTemplate_95c945c04614017a210d5882c1a2a1f87467f3a26a51d13ab2f7ee1ff3b62ffe->doDisplay(Array, Array)
#5 C:\wamp\www\stage\craft\app\vendor\twig\twig\lib\Twig\Template.php(253): Twig_Template->displayWithErrorHandling(Array, Array)
#6 C:\wamp\www\stage\craft\app\vendor\twig\twig\lib\Twig\Template.php(264): Twig_Template->display(Array)
#7 C:\wamp\www\stage\craft\app\vendor\twig\twig\lib\Twig\Environment.php(292): Twig_Template->render(Array)
#8 C:\wamp\www\stage\craft\app\services\TemplatesService.php(220): Twig_Environment->render('404', Array)
#9 C:\wamp\www\stage\craft\app\controllers\BaseController.php(74): Craft\TemplatesService->render('404', Array)
#10 C:\wamp\www\stage\craft\app\controllers\TemplatesController.php(203): Craft\BaseController->renderTemplate('404', Array)
#11 C:\wamp\www\stage\craft\app\framework\web\actions\CInlineAction.php(49): Craft\TemplatesController->actionRenderError()
#12 C:\wamp\www\stage\craft\app\framework\web\CController.php(308): CInlineAction->runWithParams(Array)
#13 C:\wamp\www\stage\craft\app\framework\web\CController.php(286): CController->runAction(Object(CInlineAction))
#14 C:\wamp\www\stage\craft\app\framework\web\CController.php(265): CController->runActionWithFilters(Object(CInlineAction), Array)
#15 C:\wamp\www\stage\craft\app\framework\web\CWebApplication.php(282): CController->run('renderError')
#16 C:\wamp\www\stage\craft\app\framework\base\CErrorHandler.php(331): CWebApplication->runController('templates/rende...')
#17 C:\wamp\www\stage\craft\app\framework\base\CErrorHandler.php(204): CErrorHandler->render('error', Array)
#18 C:\wamp\www\stage\craft\app\etc\errors\ErrorHandler.php(128): CErrorHandler->handleException(Object(Craft\HttpException))
#19 C:\wamp\www\stage\craft\app\framework\base\CErrorHandler.php(129): Craft\ErrorHandler->handleException(Object(Craft\HttpException))
#20 C:\wamp\www\stage\craft\app\framework\base\CApplication.php(732): CErrorHandler->handle(Object(CExceptionEvent))
#21 [internal function]: CApplication->handleException(Object(Craft\HttpException))
#22 {main}

And here is the layouts/main/_layouts code:

{#
 # Layout template
 # ---------------
 #
 # This template defines your site�s global layout. It can be
 # �extended� by other templates to fill in the gaps.
 #
 # See Twig�s Template Inheritance documentation for more details:
 # http://twig.sensiolabs.org/doc/templates.html#template-inheritance
 #}

<!DOCTYPE html>
<html lang="en-US">
<head>
  <meta charset="utf-8" />
  <!--[if IE]>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <![endif]-->

  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta property="og:locale" content="en_US" />
  <meta property="og:type" content="website" />

  {% block meta %}{% endblock %}

  <head profile="http://www.w3.org/2005/10/profile">
  <link rel="icon" type="image/png" href="{{ seo.favicon[0].getUrl() }}">

  {% if entry.canonicalUrl %}
    <link rel="canonical" href="{{ entry.canonicalUrl }}" />
  {% endif %}

  <title>{% if entry.pageTitle %}{{ entry.pageTitle | raw }}{% else %}{{ entry.title | raw }}{% endif %}</title>
  <meta name="description" content="{% if entry.seoDescription is defined %}{{ entry.seoDescription }}{% else %}{{ seo.seoDescription }}{% endif %}" />
  <meta name="keywords" content="{% if entry.seoKeywords is defined %}{{ entry.seoKeywords }}{% else %}{{ seo.seoKeywords }}{% endif %}" />

  <link rel="home" href="{{ siteUrl }}" />


    <link rel="stylesheet" href="{{ craft.bust.er('/lib/css/dev/base.css') }}">





    <link href="{{ craft.bust.er('/lib/css/parallax.css') }}" type="text/css" rel="stylesheet" data-skrollr-stylesheet />




  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
    <script src="{{ craft.bust.er('/lib/js/ie8.min.js') }}"></script>
  <![endif]-->

  <!-- SHARING -->
  {% if entry is defined %}

    {% if entry.sharingText %}
      {% set description = entry.sharingText %}
    {% elseif entry.seoDescription %}
      {% set description = entry.seoDescription %}
    {% else %}
      {% if entry.section == 'blogPosts' %}
        {% set description = entry.entryContent | trimit(600) %}
      {% elseif socialMedia.sharingText %}
        {% set description = socialMedia.sharingText %}
      {% elseif seo.seoDescription %}
        {% set description = seo.seoDescription %}
      {% else %}
        {% set description = '' %}
      {% endif %}
    {% endif %}

    {%if entry.pageTitle %}
      {% set title = entry.pageTitle %}
    {% else %}
      {% set title = entry.title %}
    {% endif %}

    <meta property="og:title" content="{{ title }}" />
    <meta property="og:description" content="{{ description }}" />
    {# SHARING IMAGE // defaults to global.  Otherwise set the block in your template #}
    {% include "layouts/main/partials/_sharingImage" %}
  {% endif %}
    <meta property="og:url" content="{{ entry.Url }}" />

<!-- Start Visual Website Optimizer Asynchronous Code -->
<script type='text/javascript'>
var _vwo_code=(function(){
var account_id=126816,
settings_tolerance=2000,
library_tolerance=2500,
use_existing_jquery=false,
// DO NOT EDIT BELOW THIS LINE
f=false,d=document;return{use_existing_jquery:function(){return use_existing_jquery;},library_tolerance:function(){return library_tolerance;},finish:function(){if(!f){f=true;var a=d.getElementById('_vis_opt_path_hides');if(a)a.parentNode.removeChild(a);}},finished:function(){return f;},load:function(a){var b=d.createElement('script');b.src=a;b.type='text/javascript';b.innerText;b.onerror=function(){_vwo_code.finish();};d.getElementsByTagName('head')[0].appendChild(b);},init:function(){settings_timer=setTimeout('_vwo_code.finish()',settings_tolerance);this.load('//dev.visualwebsiteoptimizer.com/j.php?a='+account_id+'&u='+encodeURIComponent(d.URL)+'&r='+Math.random());var a=d.createElement('style'),b='body{opacity:0 !important;filter:alpha(opacity=0) !important;background:none !important;}',h=d.getElementsByTagName('head')[0];a.setAttribute('id','_vis_opt_path_hides');a.setAttribute('type','text/css');if(a.styleSheet)a.styleSheet.cssText=b;else a.appendChild(d.createTextNode(b));h.appendChild(a);return settings_timer;}};}());_vwo_settings_timer=_vwo_code.init();
</script>
<!-- End Visual Website Optimizer Asynchronous Code -->

</head>

<body class="craft page-{{ craft.request.lastSegment() }} section-{{ craft.request.firstSegment() }}">

<!-- Google Tag Manager -->
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-5N2WB8"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-5N2WB8');</script>
<!-- End Google Tag Manager -->

<div>

  {# COOKIE NOTIFICATION FOR EU USERS - Hidden by default.  Ajax call to /api/showCookieNotification.html may cause this to appear if user is visiting site for first time in EU (uses session cookie) #}
  <div id="cookieNotice" class="alert alert-success alert-dismissible">
    <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
    <div class="container">
      <div class="row">
        <div class="col-md-12">
          {{ default.cookieNotice }}
        </div>
      </div>
    </div>
  </div>

  {% include "layouts/main/partials/_mainNav.html" %}

  <div id="skrollr-body">

      {% set _layout = block('layout') %}
      {% if _layout is not empty %}
          {{ _layout|raw }}
       {% else %}

          {% block content %}
            <p>If you see me, you haven�t set your <code>{% verbatim %}{% block content %}�{% endblock %}{% endverbatim %}</code> yet.</p>
            <p>See Twig�s <a href="http://twig.sensiolabs.org/doc/templates.html#template-inheritance">Template Inheritance</a> documentation for an explanation.</p>
          {% endblock %}


      {% endif %}

      {% include "layouts/main/partials/_mobileNav.html" %}

      {% include "layouts/main/partials/_footerNav.html" %}

  </div><!-- END #skrollr-body -->


    <script type="text/javascript" src="{{ craft.bust.er('/lib/js/dev/vendor.js') }}"></script>




  <script type="text/javascript" defer="defer" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-53a9b2ec75518a77"></script>


  <!-- TYPEKIT -->
  <script src="//use.typekit.net/nfj8gcg.js"></script>
  <script>try{Typekit.load();}catch(e){}</script>

  {{ getFootHtml() }}


    <script type="text/javascript" src="{{ craft.bust.er('/lib/js/dev/main.js') }}"></script>







</div>

</body>
</html>

Any idea what's going on here and possible fixes?

Thanks!

2
  • 1
    If I had to guess, you've got devMode disabled on production and that template error is being hidden, but enabled locally where you can see it. But it looks like a simple template logic error. Can you edit your original question and share the contents of your layouts/main/_layout file?
    – Brad Bell
    Oct 27 '15 at 18:06
  • Sure thing - layouts file has been added to the question above. Thanks!
    – dpayne
    Oct 27 '15 at 18:33
3

The problem is that you're using an entry variable in the layout template without it having actually been defined.

You can be defensive about it everywhere you're using it:

{% if entry is defined and entry.canonicalUrl %}

Or you can be proactive with it and set the entry to something known ahead of time:

{% set entry = craft.entries....first() %}

But it kind of depends on the behavior you're looking for on your site.

3
  • Thanks, Brad. When I define the entries, I'm taken to a 404 for any admin pages (but the actual site pages seem to work fine). Maybe this is some sort of hybrid issue between what you pointed out and what Lindsey D pointed out below?
    – dpayne
    Oct 27 '15 at 20:37
  • Do the first thing I mentioned and guard against entry not being defined.
    – Brad Bell
    Oct 27 '15 at 20:52
  • I did that and after doing so, I'm now getting the 404 page, rather than the error I was previously getting. Sorry that wasn't clear in my last message.
    – dpayne
    Oct 27 '15 at 21:42
1

This seems like one of two things:

  • A bug in Craft
  • A corrupt installation

Given that you're not doing anything terribly complex here, it's highly unlikely that you've uncovered an obscure bug in Craft. It's far more likely that certain files weren't downloaded properly.

I'd recommend replacing your entire craft/app folder. The heart of Craft lies within that folder, simply replace it to ensure there are no corrupt file issues.

Also, you may want to completely delete the contents of your craft/storage/runtime folder... It's possible that you downloaded some temp files in there which are confusing your local installation.

1
  • Thanks, Lindsey! I deleted the contents of my /runtime folder and that wasn't the issue. I also deleted and then re downloaded the contents of the craft/app folder from our live prod site and that didn't fix the issue either. Note that I just downloaded the files and the database and updated my db folder to point to that new database, meaning that I didn't install Craft first and then add files retroactively. My understanding is that is the right way to do it and Craft would install on its own in that scenario, but I may have that wrong.
    – dpayne
    Oct 27 '15 at 18:39
0

The Twig runtime error was essentially answered by Brad. We used an entry variable without it having been defined for that particular element in our layout template and by adding {% if entry is defined and entry.canonicalUrl %}, it fixed the issue and the Twig error went away. The reason that the message appeared on local and not on the live site is because the local site was in developer mode.

As to the 404 page that appeared on admin pages after fixing the issue, that seems to be a separate issue and I'll create a separate thread. Here's the thread: Craft CMS Shows 404 Error on Admin Pages

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.