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

controllerName & viewName in templates listener

cclaerhout

Well-known member
#1
When we want to make a simple conditional based on the controller name or the view name it's easy inside the templates (see here). But to do the same inside the templates listeners is not as easy (hook & postrender). I've found the solution of DigitalPoint (here) & the one of Rigel (here).

What is the best solution? Isn't there a simpler way?

Purpose: having two different kinds of layout for the editor (full - simple/mobile). Simple layout would be activated when a certain controller/view type is matched or if the user is on a mobile device. Otherwise, it will be the full layout that will be displayed. This function could be integrated in the buttons manager.
 

Chris D

XenForo developer
Staff member
#2
Well, there's nothing more simple than digitalpoint's method.

Rigel's may be a little bit more robust, though.

It might be a toss a coin moment.
 

MOZ

Well-known member
#4
When we want to make a simple conditional based on the controller name or the view name it's easy inside the templates (see here). But to do the same inside the templates listeners is not as easy (hook & postrender). I've found the solution of DigitalPoint (here) & the one of Rigel (here).

What is the best solution? Isn't there a simpler way?

Purpose: having two different kinds of layout for the editor (full - simple/mobile). Simple layout would be activated when a certain controller/view type is matched or if the user is on a mobile device. Otherwise, it will be the full layout that will be displayed. This function could be integrated in the buttons manager.
I am liking where this is going ;)
 

cclaerhout

Well-known member
#5
The solution was simpler:
PHP:
$controllerName = $template->getParam('controllerName');
$controllerAction = $template->getParam('controllerAction');
$viewName = $template->getParam('viewName');
 

Robbo

Well-known member
#6
All those ways are just eww...

Use a single Listener class. Have a protected static $frontController variable. Create a listener that has access to the front controller. Do self::$frontController = $frontController;. Use it in other listeners with self::$frontController.

Although the last way you said is okay. The ways in your first post are hacky and I expect better from those guys...
 

cclaerhout

Well-known member
#7
All those ways are just eww...

Use a single Listener class. Have a protected static $frontController variable. Create a listener that has access to the front controller. Do self::$frontController = $frontController;. Use it in other listeners with self::$frontController.
I didn't see your reply. The method to use a single class with different listeners is clever. Thanks !
 

cclaerhout

Well-known member
#8
Just a feedback, here is an example of the Robbo solution (I'm not sure it's 100% the same since there were frontController listeners):

Use this listener front_controller_pre_view and a code like this:
PHP:
    protected static $_controllerInfo = array('controllerName' => NULL, 'controllerAction' => NULL, 'viewName' => NULL);

    public static function getController(XenForo_FrontController $fc, XenForo_ControllerResponse_Abstract &$controllerResponse, XenForo_ViewRenderer_Abstract &$viewRenderer, array &$containerParams)
    {
        self::$_controllerInfo['controllerName'] = (isset($controllerResponse->controllerName)) ? $controllerResponse->controllerName : NULL;
        self::$_controllerInfo['controllerAction'] = (isset($controllerResponse->controllerAction)) ? $controllerResponse->controllerAction : NULL;
        self::$_controllerInfo['viewName'] = (isset($controllerResponse->viewName)) ? $controllerResponse->viewName : NULL;

        //Zend_Debug::dump(self::$_controllerInfo);
    }
 
    //Rest of the code of the class where the self::$_controllerInfo can be used anywhere
 

Robbo

Well-known member
#9
That code could look a little better but that is what I meant pretty much. For example there is no need to use an array there, it serves no purpose other than to compact the code. This reminds me of why I hate listeners.
 

cclaerhout

Well-known member
#10
Another way to do it:
PHP:
$fc = XenForo_Application::get('fc');
$route = $fc->route();
$controllerName = $route->getControllerName();
$jsonResponse = ($route->getResponseType() == 'json');
/*If more data is needed*/
$data = $fc->dispatch($route);
$controllerName = $data->controllerName;
$controllerAction = $data->controllerAction;
$viewName = $data->viewName;