1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Trying to Attain a Variable within a Template

Discussion in 'XenForo Development Discussions' started by CaptainMorgan, Sep 30, 2014.

  1. CaptainMorgan

    CaptainMorgan Active Member

    My Listener contains a function to load the ControllerPublic Index.php I've made. It also contains my templateHook that determines where in the sidebar the admin would like to display this particular block for his users.

    In the ControllerPublic_Index, and for testing purposes, I've stripped the code down to this:

    public actionIndex()
           $viewParams = array('my_variable' => 'FooGoo<br />',
                               'my_second_variable' => 'Bar<br />');
            return $this->responseView('MyAddon_ViewPublic_Index', 'mytemplate_sidebar_main', $viewParams);
    My variables are available in the template yet for some reason which I cannot determine, it becomes the ONLY template on the index page. So, in essence, what was originally intended as only a sidebar block, now displays across the whole page.

    Any ideas what I might be doing wrong?
  2. rainmotorsports

    rainmotorsports Well-Known Member

    If your extending an existing controller you need to return the parent response + yours. I'm at work and honestly don't remember this off the top of my head. But if you look at pretty much any plugin doing something similar you will see how its done.

    Never mind it still sounds like your only returning those variables. I think I missed the fact that it looks like your doing a standalone addon
    Last edited: Sep 30, 2014
  3. CaptainMorgan

    CaptainMorgan Active Member

    Thank you rain.

    Either way I extend:

    class My_Directory_Class extends XFXP_My_Directory_Class {} 

    class My_Directory_Class extends XenForo_ControllerPublic_Abstract {}
    Neither seem to resolve the issue. But further, what is the proper way to return the parents *and* my response? I've seen:

          $response = parent::actionIndex();
            if ($response instanceof XenForo_ControllerResponse_View) { // and then anything in here never gets called because apparently this resolves to false      
                    $response->params += array(
                          'my_variable' => $foo_var,
                          'my_second_variable' => $foo_choo_bar);     
            return $response;
    Using this approach, nothing is returned... based on the dozens of plugins I've looked at.
  4. rainmotorsports

    rainmotorsports Well-Known Member

    Okay I'm a bit confused. So your listener is to add a block to the sidebar of an existing page right? Or is it a stand alone addon page?

    I think you might be checking against the wrong controller. I might be wrong. I guess your trying to target all pages with a sidebar? Have you tried a more narrow scope and just did the forum pages view controller as a test?

    If its a stand alone addon you shouldn't need to check the controller I don't think. I don't on my punk buster viewer
  5. CaptainMorgan

    CaptainMorgan Active Member

    rain - that is probably correct, at least sounds correct.

    If I were to narrow the scope, which controller should I target for just the forum's pages?

  6. CaptainMorgan

    CaptainMorgan Active Member

    Do you have a link to this viewer? I'd like to check it out for comparison.

    So are you saying I can bypass the controller altogether and pass the variables through the listener? How would I do that?
  7. rainmotorsports

    rainmotorsports Well-Known Member

    Hmm pain in my butt doing this from a phone. Wish someone decent would come help lol.

    I sees in addon that adds a sidebar block to our forum page extending this


    And then basically does its work like this

    public function actionIndex()
    $parent = parent::actionIndex();


    $parent->params['something'] = $something;

    return $parent;
  8. CaptainMorgan

    CaptainMorgan Active Member

    If I extend XenForo_ControllerPublic_Forum, my home page becomes the forum's page... ugh
  9. CaptainMorgan

    CaptainMorgan Active Member

    Hah! Thank you rain. I'm just going to bypass the controller completely... I've gotten it to work that way. Cheers!
  10. rainmotorsports

    rainmotorsports Well-Known Member

    I'm just confused. You are saying having your listener extend that controller was overriding the home page? Shouldn't happen.

    Anyways the main reason to check in your controller which controller instance its in is to prevent unnecessary running of the code as well as any errors such things might cause. Like in my one and only public addon I have to make sure its a thread view otherwise redirect threads cause errors etc.

Share This Page