1

I have a plugin with nothing but the following in the init() method other than the basics (just for testing purposes):

craft()->urlManager->setRouteVariables(array(
    'foo' => array('bar' => 'baz')
));

From all of my routes I can access this just fine. It's working for static templates, dynamic entries, etc. To access it I'm using the following code in my main base template /_layouts/base.twig:

{{ foo.bar }}

The output is as expected: baz

However, I cannot access this from a page that does not exist, i.e., a 404. If I go to my-site.com/doesnt-exist I receive the following:

Twig_Error_Runtime

Variable "foo" does not exist in "_layouts/base" at line 1

My 404 template is extending base as well.

What am I doing wrong?

It's probably worth noting that the reason I'm trying to do this is because I want to include a form on the 404 page (that I also include in other areas, so it's setup as a partial) and I'm having trouble passing error validation back and forth with setRouteVariables, but only for 404 pages. I know I could do this with a session and that is my current work around, but I wanted to see if there was a better way.

2

The simple answer is: no, error handling templates don’t have access to the variables that would be available to the template when the request resolves normally. And we won’t be making it possible.

Technically you could get access to them by creating a plugin that has a template hook which is capable of adding those variables:

craft()->templates->hook('mergeRouteVariables', function(&$context)
{
    $routeParams = craft()->urlManager->getRouteParams();

    if (isset($routeParams['variables']))
    {
        $context = array_merge($context, $routeParams['variables']);
    }
}

404.html:

{% hook "mergeRouteVariables" %}
{# ... #}

It's probably worth noting that the reason I'm trying to do this is because I want to include a form on the 404 page (that I also include in other areas, so it's setup as a partial) and I'm having trouble passing error validation back and forth with setRouteVariables, but only for 404 pages. I know I could do this with a session and that is my current work around, but I wanted to see if there was a better way.

While I understand your use case, it's pretty weird that you would have a form that intentionally submits to a 404 page (even though it’s the same page).

Here are a couple alternatives you might want to consider:

  • You could create a dedicated page for the form, and have the form submit to that page’s URL in the event that it’s coming from a 404.

  • You could handle the form submission and validation with Javascript/Ajax.

  • Sounds good, thanks for the info Brandon. I've actually gone exactly that route: Posting to a dedicated form instead which obviously makes more sense. I knew the use case was odd and not great, but then it opened up the question which is good to have a definitive answer for should something similar come up in the future. – Peter Tell Oct 15 '14 at 19:46

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.