Author Topic: I don't understand the template_container plugin hook, can someone please help  (Read 3131 times)

Offline mabj

  • Newbie
  • *
  • Posts: 8
I'm trying to create a plugin and use the `template_container` hook to fill a container like this:

skins/larry/templates/login.html:
Code: [Select]
    <roundcube:container name="testcontainer" id="testcontainer" />

plugins/myplugin/myplugin.php
Code: [Select]
    class myplugin extends rcube_plugin
    {
        function init()
        {
            $this->rcmail = rcmail::get_instance();
            $this->add_hook('template_container', array($this, 'test_hook'));
        }

        function test_hook($attr) {
            if ($attr["name"] === "testcontainer") {
                $content = $attr["content"];
                $content .= html::tag('div', null, "I am testing this");
            }

            return array("content" => $attr["content"]);
        }
    }

I have added the plugin to config.inc.php like
Code: [Select]
    $config['plugins'] = array('xskin', 'managesieve', 'password', 'myplugin');` so that is not the problem.

But when I load the login screen, the div does not show up. Does anyone know what I am doing wrong?

I can also say that it works when I use the "startup" hook and $this->api->add_content(html::tag(...), "testcontainer"); so I know that my plugin works in general.
« Last Edit: February 14, 2019, 07:38:41 AM by mabj »

Offline SKaero

  • Administrator
  • Hero Member
  • *****
  • Posts: 5,876
    • SKaero - Custom Roundcube development
Your adding a hook "add_hook" while trying to fill a container which is incorrect you should be using "register_handler"

Offline mabj

  • Newbie
  • *
  • Posts: 8
Your adding a hook "add_hook" while trying to fill a container which is incorrect you should be using "register_handler"

So what is stated on https://github.com/roundcube/roundcubemail/wiki/Plugin-Hooks#template-hooks is incorrect?

Also, I don't understand this part from https://github.com/roundcube/roundcubemail/wiki/Plugin-API:

Finally, to instruct roundcube to show your template:
Code: [Select]
        $rcmail = rcmail::get_instance();
        $rcmail->output->set_pagetitle('my_title');
        $rcmail->output->send('my_plugin.mytemplate');

Where am I supposed to put that part (which file/method)?

Offline SKaero

  • Administrator
  • Hero Member
  • *****
  • Posts: 5,876
    • SKaero - Custom Roundcube development
The documentation isn't great, I believe the section your looking for is: https://github.com/roundcube/roundcubemail/wiki/Plugin-API#templates

Offline mabj

  • Newbie
  • *
  • Posts: 8
The documentation isn't great, I believe the section your looking for is: https://github.com/roundcube/roundcubemail/wiki/Plugin-API#templates

I finally got it to work, I think. Now to the next question. I would like to use the same method (test_hook) to have several objects with almost the same content. Do I need to do register_handler for each object?

Also, can I sent some attributes to test_hook to distinguish which object to process?

Offline SKaero

  • Administrator
  • Hero Member
  • *****
  • Posts: 5,876
    • SKaero - Custom Roundcube development
If you set attributes in the "roundcube:container" they will be in the $attr array.