2

I have an issue with rendering a template from within my controller in my plugin. It just seems to re-direct to the dashboard instead of bringing up the template.

I have a widget on the dashboard that calls an action in my plugin's controller file. This works as I can print_r the object I am creating. However when it gets to the render code it just keeps re-directing back to the dashboard. I've even tried stripping out all the code apart from the render call and it still does it. The code making the call is:

return craft()->templates->render('pluginName/index.html');

Which is currently the only code in the plugin function. The plugin has a templates folder where the index.html is held. I've also stripped the template down so it only contains the following:

{% extends "_layouts/cp" %}
{% set centered = true %}
{% set title = "The Title"|t %}

{% set content %}

<p>List the users</p>

{% endset %}

Any clues anyone.

0

1 Answer 1

6

I haven't tried calling a template from a controller, so I may not be any help but I've used

...->render(...)

in my main plugin Class like this:

MyPlugin.php

return craft()->templates->render('myplugin/_settings', array('settings' => $this->getSettings()));

Maybe the render method is looking for another argument?

EDIT

Oh wait... I was wrong, it looks like I do...

MyPluginController.php

$vars['users'] = craft()->user->listUsers();
$this->renderTemplate('myplugin/index', $vars);

Then I have a variables file:

MyPluginVariable.php

public function getContacts()
{
    return craft()->contactForm->getUsers();
}

Which ties back to the my service:

MyPluginService.php

public function getUsers()
{
    return MyPluginRecord::model()->findAll(array('order' => 'dateCreated desc'));
}

Maybe that will help get you rolling?

3
  • cheers that has helped somewhat. How are you accessing the variable object in the template so you can use the variables?
    – Lettie
    Commented Aug 13, 2015 at 10:17
  • Ignore last comment I've worked it out. Just had to make couple of changes to my service. Thanks for the help.
    – Lettie
    Commented Aug 13, 2015 at 10:24
  • Awesome! Glad it helped.
    – Damon
    Commented Aug 13, 2015 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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