Ad Templates Issue

Quiver

Active member
#21
You are close.

Create a new template e.g MyTemplate
Paste your template code into it.

In your template hook code use this:

$contents .= $template->create('MyTemplate', array());

Now that also won't work heh...

You see where I've got array() above? You should replace that with your TopUsers array. That means you'll need to grab that in exactly the same way you get it in your controller.

Overall there's much better ways of doing this but I suspect you probably want to just get this working first then we can improve on it.
Code:
public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
    {
        //if ($hookName == 'ad_above_top_breadcrumb')
        //{
            //$params = $template->getParams();
            //$params += $hookParams;
            //$contents .= $template->create('twistedpromotion_banner', $params, array('TwistedPromotion_TopUser' => $TwistedPromotion_TopUser));
        //}
    }
This is also not working. :(
(even when it's uncommented)
 

Chris D

XenForo developer
Staff member
#22
This is kind of the idea I was talking about. I've based this on your Controller code somewhat. I have to say, it's not exactly the right way to do things, but this is based on the code you have so far in your Controller:

PHP:
	public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
	{
		if ($hookName == 'ad_header')
		{
			$TwistedPromotion_TopUser = TwistedPromotion_TopUser_Model_XTopUser::TwistedPromotion_TopUserArray();
			
			$contents .= $template->create('MyTemplate', $template->getParams() += $TwistedPromotion_TopUser);
		}
	}
 

Quiver

Active member
#23
This is kind of the idea I was talking about. I've based this on your Controller code somewhat. I have to say, it's not exactly the right way to do things, but this is based on the code you have so far in your Controller:

PHP:
	public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
	{
		if ($hookName == 'ad_header')
		{
			$TwistedPromotion_TopUser = TwistedPromotion_TopUser_Model_XTopUser::TwistedPromotion_TopUserArray();
			
			$contents .= $template->create('MyTemplate', $template->getParams() += $TwistedPromotion_TopUser);
		}
	}
Thanks I'll try this when I get home.
I'll let you know if it works, Chris. :)
 

Chris D

XenForo developer
Staff member
#24
Just a side note:

You need to ensure "MyTemplate" exists as a template in Admin CP > Appearance > Templates and contains the code that you wish to appear in the ad_header location.
 

Quiver

Active member
#25
This is kind of the idea I was talking about. I've based this on your Controller code somewhat. I have to say, it's not exactly the right way to do things, but this is based on the code you have so far in your Controller:

PHP:
 public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
{
if ($hookName == 'ad_header')
{
$TwistedPromotion_TopUser = TwistedPromotion_TopUser_Model_XTopUser::TwistedPromotion_TopUserArray();
 
$contents .= $template->create('MyTemplate', $template->getParams() += $TwistedPromotion_TopUser);
}
}
Fatal error: Can't use method return value in write context in /home/<my username>/public_html/library/TwistedPromotion/TopUser/Listener/Listener.php on line 17
 

Chris D

XenForo developer
Staff member
#27
It's ok was pretty much a lazy, copy paste error.

Correct way would be something like:

PHP:
	public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
	{
		if ($hookName == 'ad_header')
		{
			$TwistedPromotion_TopUser = TwistedPromotion_TopUser_Model_XTopUser::TwistedPromotion_TopUserArray();

			$params = $template->getParams();
			$params['TwistedPromotion_TopUser'] = $TwistedPromotion_TopUser;
			
			$contents .= $template->create('MyTemplate', $params);
		}
	}
 

Quiver

Active member
#28
It's ok was pretty much a lazy, copy paste error.

Correct way would be something like:

PHP:
 public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
{
if ($hookName == 'ad_header')
{
$TwistedPromotion_TopUser = TwistedPromotion_TopUser_Model_XTopUser::TwistedPromotion_TopUserArray();
 
$params = $template->getParams();
$params['TwistedPromotion_TopUser'] = $TwistedPromotion_TopUser;
 
$contents .= $template->create('MyTemplate', $params);
}
}
That fixed the error. But now it's still not displaying the banner.
I used the following (as wish to use ad_above_top_breadcrumb).
I've put something else in ad_header (paid PublicityClerks advert).

Code:
<?php
class TwistedPromotion_TopUser_Listener_Listener
{
    public static function listen($class, array &$extend)
    {
        if ($class == 'XenForo_ControllerPublic_Index')
        {
            $extend[] = 'TwistedPromotion_TopUser_Controller_Public';
        }
    }
    public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
    {
        if ($hookName == 'ad_above_top_breadcrumb')
        {
            $TwistedPromotion_TopUser = TwistedPromotion_TopUser_Model_XTopUser::TwistedPromotion_TopUserArray();
 
            $params = $template->getParams();
            $params['TwistedPromotion_TopUser'] = $TwistedPromotion_TopUser;
           
            $contents .= $template->create('twistedpromotion_banner', $params);
        }
    }
}
 
?>
Now if you visit the top breadcrumb there's nothing there. :(
I have the template twistedpromotion_banner containing everything I need:

Code:
<a href="{$TwistedPromotion_TopUser.homepage}" target="_blank"><img src="{$TwistedPromotion_TopUser.userFieldValues.banner}" alt="{$TwistedPromotion_TopUser.username}"></a>
 

Chris D

XenForo developer
Staff member
#29
In your template type this:

{xen:helper dump, $TwistedPromotion_TopUser}

Save the template and check again - does it output anything? If so, what?
 

Chris D

XenForo developer
Staff member
#31
Exactly... as I thought...

So consider that you have exactly the same data as you have in the sidebar...

What do you think needs to be done differently in the twistedpromotion_banner template to make it work, just like you have it working in the sidebar? Is there anything different in the sidebar block code compared to your banner template?
 

Quiver

Active member
#32
Exactly... as I thought...

So consider that you have exactly the same data as you have in the sidebar...

What do you think needs to be done differently in the twistedpromotion_banner template to make it work, just like you have it working in the sidebar? Is there anything different in the sidebar block code compared to your banner template?
This is twistedpromotion_topuser template:

Code:
<xen:if is="{$TwistedPromotion_TopUser}">
<div class="section staffOnline avatarList">
  <div class="secondaryContent">
    <h3>{xen:phrase twistedpromotion_topuser}</h3>
    <ol>
    <xen:foreach loop="$TwistedPromotion_TopUser" value="$XTopUser">
    <li>
    <xen:avatar user="$XTopUser.user" size="s" text="{$XTopUser.username} ({xen:number $XTopUser.totalPosts})" class="Tooltip" title="{$XTopUser.username}" />
    <xen:username user="$XTopUser.user" rich="true" />
    <xen:if is="{$XTopUser.homepage} == ''">
<i>has not set their homepage.</i><br />
<a href="{$homeLink}account/personal-details">Click to set your homepage.</a>
<xen:else />
Visit {$XTopUser.username}'s website:<br />
<a href="{$XTopUser.homepage}" target="_blank">{$XTopUser.homepage}</a>
</xen:if>
    </li>
    </xen:foreach>
    </ol>
  </div>
  </div>
</xen:if>
???
 

Chris D

XenForo developer
Staff member
#33
This is your clue:

Code:
		<xen:foreach loop="$TwistedPromotion_TopUser" value="$XTopUser">
				<li>
						<xen:avatar user="$XTopUser.user" size="s" text="{$XTopUser.username} ({xen:number $XTopUser.totalPosts})" class="Tooltip" title="{$XTopUser.username}" />
						<xen:username user="$XTopUser.user" rich="true" />
						<xen:if is="{$XTopUser.homepage} == ''">
							<i>has not set their homepage.</i><br />
							<a href="{$homeLink}account/personal-details">Click to set your homepage.</a>
						<xen:else />
							Visit {$XTopUser.username}'s website:<br />
							<a href="{$XTopUser.homepage}" target="_blank">{$XTopUser.homepage}</a>
						</xen:if>
				</li>
		</xen:foreach>
 

Quiver

Active member
#34
Code:
<xen:if is="{$TwistedPromotion_TopUser}">
<xen:foreach loop="$TwistedPromotion_TopUser" value="$XTopUser">
<a href="{$XTopUser.homepage}" target="_blank"><img src="{$XTopUser.userFieldValues.banner}" alt="{$XTopUser.username}"></a>
</xen:foreach>
</xen:if>
This works! :D :D :D :D :D
Thanks so much Chris. :D :D
Now to test it for a few hours.
 
Top