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

Including html/php to templates...

Umit

Active member
#1
Hi, I've created a very basic addon and trying to includes a html file (it is a weather forecast data) to sidebar.

here is my addon;
Code:
<?php
class XenForo_Umiweather_kusweather
{
    public static function container_public_params(array &$params, XenForo_Dependencies_Abstract $dependencies)
    {
        ob_start();
        include('/Applications/MAMP/htdocs/mydomaintest/weather/cache.html');
        $kusweather = ob_get_contents();
        ob_end_clean();
 
        $params = array(
            'kusweather'  => $kusweather
        );

/*$dependencies->params = array_merge(
            $dependencies->params,
            $params
        );*/
    }
}
Code Event Listener: container_public_params
XenForo_Umiweather_kusweather::container_public_params

I can add this to page_include template and it is working but breaking javascript of the website.

I dont know what i am doing wrong but first i want to get rid of the errors and second i want to add the output to any template (for example sidebar templates).

Please help...
 

Jeremy P

Well-known member
#2
Just a thought, try $params += array(...

Otherwise you're probably going to need to provide more information. How is the javascript broken? Are there messages in the console? What is the contents of the weather html?
 

Umit

Active member
#3
Ohhh! That sorted out some errors. Thank you so much Jeremy.

But still, i can only use it on page_container, how can i use it in sidebar related templates?
 

Jeremy P

Well-known member
#4
Hmm. You could try adding a <xen:sidebar> block to page_container, not sure if that would override or just append/prepend to pages where one already exists. Otherwise you'd probably have to use template hooks, or extend load_class_controller for the controllers of the templates you want the variable to be available in.
 

Umit

Active member
#5
Thank you again Jeremy,

just want to add this {xen:raw $kusweather} to sidebar_online_users template, dont want to use page_container.

should i change "listen to event: container_public_params" to something else?
 

Jeremy P

Well-known member
#6
Maybe adapt your listener for template_create?
Then when that template is created pass that as a param? Not 100% that would work though, it might not catch <xen:include> statements, I haven't looked much into how templates work/are compiled.

Otherwise you'd have to extend the controllers, pass that variable to the view parameters, and then edit the templates to include <xen:set var="$kusweather">{$kusweather}</xen:set> between the includes for sidebar_online_users..
 

Umit

Active member
#7
Maybe adapt your listener for template_create?
Then when that template is created pass that as a param? Not 100% that would work though, it might not catch <xen:include> statements, I haven't looked much into how templates work/are compiled.

Otherwise you'd have to extend the controllers, pass that variable to the view parameters, and then edit the templates to include <xen:set var="$kusweather">{$kusweather}</xen:set> between the includes for sidebar_online_users..
Thank you Jeremy, how can i extend the controllers?
 

Umit

Active member
#10
Thank you so much Jeremy and Ragtek. Template hooks working perfectly. Cant believe it is so easy.

But i have a question; what if the template does not contain any hook? then can we create something like this in the template;

our_custom_template:
<xen:hook name="our_custom_hook_name" />
 

Jeremy P

Well-known member
#13
Should if your code is right.
PHP:
public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
	{
		switch ($hookName)
		{
			case 'your_hook_name':
				$weather = 'weather';
				$contents .= $weather;
				break;
		}
	}