XF 2.0 How to load controller value in all page of forum

hemant_bhardwaj

Well-known member
well i am trying to load some value from controller but it's not working and show's nothing

My controller
1541144906620.webp

so just wondering how can i get these values global on every page of forum instead of single page with route

i read some where to use templater_macro_pre_render for it but i am not able to get it work

1541145036124.webp

What should i write there >?

help appropriate 😎😃
 
Last edited:
after few try i am able to get value in
i saw that with printr($globalVars);
but they are not seeing it working in template :(
<span>{$viewParams.xtoolbar_title}</span> any help ?
1541160943237.webp
 
I had written a whole essay with all the possible solutions, but based on the code in your last post you should be able to do the following:

Diff:
--- <unnamed>
+++ <unnamed>
@@ -1,5 +1 @@
-$viewParam = [
-    'toolbar_list' => $viewParams,
-];
-
-print_r($globalVars);+ $globalVars['toolbar_list'] = $viewParams;
+$globalVars['toolbar_list'] = $viewParams;

It should be callable in the template like {{ $toolbar_list }}
 
Last edited:
I had written a whole essay with all the possible solutions, but based on the code in your last post you should be able to do the following:

Diff:
--- <unnamed>
+++ <unnamed>
@@ -1,5 +1 @@
-$viewParam = [
-    'toolbar_list' => $viewParams,
-];
-
-print_r($globalVars);+ $globalVars['toolbar_list'] = $viewParams;
+$globalVars['toolbar_list'] = $viewParams;

It should be callable in the template like {{ $toolbar_list }}

Thanks for the reply according to your suggestion i do this
1541164701988.webp
and the code i test with {{ $toolbar_list }} , {$toolbar_list}
1541164835486.webp
The out put i am getting
1541165225982.webp
i am getting qwe instead of asd
and also i am getting the same output about 40 times
about print_r() that was jut for testing. to see the variable.
 
You effectively destroy the referenced $globalVars that way, replacing any existing values it might have.

That listener is called so many times because you probably haven't added a hint in the listener definition.
Any template macro that is being called to get rendered is running the code in your callback.

If you want to make use of that listener, you should create a template macro and inject it, say via a template modification, to a public template once.

Here's another, easier way to make your example print what you want:
Change your listener to: controller_post_dispatch_listener

Change the callback code to:
PHP:
public static function controllerPostDispatchListener(\XF\Mvc\Controller $controller, $action, \XF\Mvc\ParameterBag $params, \XF\Mvc\Reply AbstractReply &$reply)
{
   if (!(\XF::app() instanceof \XF\Pub\App))
   {
      return;
   }

   $finder = \XF::finder('xenbros\customtoolbar:CustomTool');
   $toolbarIds = $finder->order('xftoolbar_id', 'DESC')->fetch();

   $reply->setParam('toolbar_list', $toolbarIds); // {{ $toolbar_list }} is now your template var.
}

BTW, instead of print_r(), you'd better of using this or a proper debugger (such as xdebug).
 
thanks for the all the detailed code and yup i remember it now about the hint in xfdoc and after that code it's just give ooops :D

1541182923509.webp

1541183074385.webp

Edit : i just check that column id was wrong so no issues :)
yeah but now it's @alexD

Code:
An exception occurred: [Error] Call to undefined method XF\Mvc\Reply\Reroute::setParam() in src\addons\xenbros\customtoolbar\Customlistener.php on line 21
 
Last edited:
I'm struggling a little to follow as you've brought controllers into it and I don't think that's relevant, but if you want data to always be available to the templates/templater, you can just use the templater_global_data event. It's specifically designed for this.
 
Also, the 'Oops' style error instead of an exception stack trace usually means you haven't enabled the debug/development mode.

You can still probably see it logged in Admin error log page, but I'd suggest you add any of these lines to your config.php file:
Code:
$config['debug'] = true;
$config['development']['enabled'] = true;
 
Back
Top Bottom