1

I'm trying to write a plugin that lives in the admin interface, that reads external data (a cinema program), parses it, and creates entries based on this (movie info and screening times).

While doing so I build a log of the additions/updates made, that I want to return to the template (the plugin page from where I trigger the update) and display the changes listed in a table. But I have trouble reading anything out with craft.request.getPost() from the template.

The log, basically an array of custom models (log items), seems to be fine while still in the controller. I try to pass this array via craft()->urlManager->setRouteVariables(), but at the template, it seems the only variables it finds are the action and redirect parameters from the original form.

The code in the controller looks like this:

$log = craft()->updateProgram_program->update();
craft()->urlManager->setRouteVariables(array(
   'log' => $log
));

And then I try to read the log variable in my template like this:

{% set log = craft.request.getPost('log') %}

Whatever I do, it seems to always be NULL. I've also tried to set a simple text string as the value, in case it's not possible to post an array like this, but it's still NULL when it comes to the template. It seems the log parameter is simply not found in the POST request at all.

Is there anything obvious I'm doing wrong?

I've scratched my head all day over this, so any tips are extremely welcome!

2

I believe you are overwriting the log variable you are sending back to the template when you set log equal to the response of getPost.

From what I can see of your code, when you return variables to your template via setRouteVariables, you should be providing the variables you define in that array to your template, without having to take a second step and retrieve them. So, when your controller returns:

craft()->urlManager->setRouteVariables(array(
   'log' => $log
));

You should then be able to access that variable in your template if it exists:

{% if log is defined %}
    {{ dump(log) }}
{% endif %}

setRouteVariables adds your variables to Twig's context, which you can see in your template by dumping _context:

{{ dump(_context) }}

Or, a handy way to just see the variables available in the context and not the full models is using the keys filter:

{{ dump(_context|keys) }}

In your case, I believe when your page reloads, your log variable is available, but then you set it to null when you try to retrieve it.

Your current code:

{# Sets log to null, because no post variable exists named `log` #}
{% set log = craft.request.getPost('log') %}

{% if log is defined %}
    {{ dump(log) }} {# outputs null #}
{% endif %}

Try outputting the log variable before setting your getPost value:

{# Outputs log if it exists in Twig's context #}
{% if log is defined %}
    {{ dump(log) }} {# will probably output your value #}
{% endif %}

{# Delete this. It just overwrites your existing log variable #}
{% set log = craft.request.getPost('log') %}
  • You are hero! This works like a charm. Thanks for not only saving this concrete situation, but now I also understand way better how things work, and what is accessible where, which I'm sure will save me a lot of headaches also down the road. – KSP Oct 23 '16 at 22:08

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.