XF 2.0 How to pass a variable to the advertsing HTML

AndyB

Well-known member
I'm creating a XF2 add-on called Banner and I would like to pass a variable called $banners into the Advertising HTML like this:

1508302810372.webp

My PHP code is the following:

PHP:
<?php

namespace Andy\Banner;

class Listener
{
    public static function appPubRenderPage(
        \XF\Pub\App $app,
        array &$params,
        \XF\Mvc\Reply\AbstractReply $reply,
        \XF\Mvc\Renderer\AbstractRenderer $renderer
    ) 
    {
        // get options
        $options = \XF::options();
        
        // get options from Admin CP -> Options -> Banner -> Limit
        $limit = $options->bannerLimit;
        
        // get db
        $db = \XF::db();        
        
        // run query
        $banners = $db->fetchAll("
        SELECT *
        FROM xf_banner
        ORDER BY RAND()
        LIMIT ?
        ", $limit);
        
        // update params
        $params['banners'] = $banners;
    }
}

My code event listener:

1508302994519.webp
 
Ads don't have access to any external variables other than the standard global stuff (what's in $xf) and what's explicitly passed into them.

That said, they will generally have access to the global scope within $__globals, {$__globals.banners} may work.
 
Top Bottom