Passing Data to Links Template

Cupara

Well-known member
I'm having issues, I need to pass a foreach to a template that I include in my sublink template.

The problem is I can't get any template listeners to work and I can't get a hook to work I added to my own template.

Any suggestions?

Template with include:
HTML:
<ul class="secondaryContent blockLinksList">
<xen:hook name="nr_links_end">
    <li><a href="{xen:link 'news'}">All</a></li>
</xen:hook>
</ul>

Contents of included template:
HTML:
<li>{xen:raw $label}</li>

Listener file (Tried using Ragtek's method also includes my custom template hook):
PHP:
class NewsReader_Listener_Vars
{
    public static function createTemplate(&$templateName, array &$params, XenForo_Template_Abstract $template)
    {
       if($templateName == 'nr_links') {
            $template->preloadTemplate('nr_prefix_links');
        }
    }

    public static function template($name, &$contents, array $params, XenForo_Template_Abstract $template)
    {
        if ($name == 'nr_links_end')
        {
                $templateParams = $template->getParams();
                $search = '</ul>';
                $params = array(
                    'selectedKey' => $templateParams['selectedKey']
                );
                
                $prefixLinks .= $template->create('nr_prefix_links')->render();

                $contents = str_replace($search, $prefixLinks, $contents);
        }
    }
}

ControllerIndex where $label is defined:
PHP:
<?php
class NewsReader_ControllerPublic_Index extends XenForo_ControllerPublic_Abstract
{
    public function actionIndex()
    {    
        // Prepare to grab any options needed
        $options = XenForo_Application::get('options');
        
        // Build the public link
        $this->canonicalizeRequestUrl(
            XenForo_Link::buildPublicLink('news', null)
        );
        
        // Build the Model for caching
        $feedModel = $this->getModelFromCache('NewsReader_Model_Feed');
        
        // Unique for testing only.
        $visitor = XenForo_Visitor::getInstance();
        $visitor_id = $visitor->getUserId();
        $userName = $visitor['username'];
        
        // Lets paginate the results
        $page = max(1, $this->_input->filterSingle('page', XenForo_Input::UINT));
        $perPage = $options->nr_limit;
        $allFeeds = $feedModel->getFeedLog(array('perPage' => $perPage, 'page' => $page));
        $count = $feedModel->getFeedAllCount();
        //$title = new XenForo_Phrase('thread_prefix_');
        //$prefixId = $allFeeds['prefix_id'];
        
        // Grab the data
        //$prefixGroup = $feedModel->getPrefixGroupById();
        
        /*foreach ($prefixGroup AS $prefix)
        {
            $title = new XenForo_Phrase('thread_prefix_' . $allFeeds['prefix_id']);
            $label = '<li><a href="{xen:link \'news/prefix/\', $title}">{xen:raw $title}</a></li>';
        }*/
        
        $feed = array();
        foreach ($allFeeds AS $feeds)
        {
            $prefix = new XenForo_Phrase('thread_prefix_' . $feeds['prefix_id']);
            if (!$feeds['prefix_id'])
            {
            $feed[] = '<li style="border-bottom:1px solid #cecfd0;padding-left:3px;line-height:20px;"><a href="javascript:frameSrc(\''. $feeds['unique_id'].'\'); var waste = \'/threads/\';">'. $feeds['title'] .'</a><span style="float:right;"><a href="threads/'. $feeds['thread_id'] .'/reply">Comment</a></span></li>';
            } else {
            $feed[] = '<li style="border-bottom:1px solid #cecfd0;padding-left:3px;line-height:20px;">['.$prefix.'] <a href="javascript:frameSrc(\''. $feeds['unique_id'].'\'); var waste = \'/threads/\';">'. $feeds['title'] .'</a><span style="float:right;"><a href="threads/'. $feeds['thread_id'] .'/reply">Comment</a></span></li>';
            }
        }
        
        $label = "Test";
        // Register for use in the template
        $viewParams = array(
            'count' => $count,
            'page' => $page,
            'perPage' => $perPage,
            'username' => $userName,
            'visitor_id' => $visitor_id,
            //'feeds' => $allFeeds,
            //'prefixGroup' => $prefixGroup,
            'prefix' => $prefix,
            //'data' => $data,
            'feed' => $feed,
            'feeds' => $feeds,
            'label' => $label
        );

        return $this->responseView('NewsReader_ViewPublic_News', 'nr_index', $viewParams);
    }
 
This is the foreach in question, right?

PHP:
        foreach ($allFeeds AS $feeds)
        {
            $prefix = new XenForo_Phrase('thread_prefix_' . $feeds['prefix_id']);
            if (!$feeds['prefix_id'])
            {
            $feed[] = '<li style="border-bottom:1px solid #cecfd0;padding-left:3px;line-height:20px;"><a href="javascript:frameSrc(\''. $feeds['unique_id'].'\'); var waste = \'/threads/\';">'. $feeds['title'] .'</a><span style="float:right;"><a href="threads/'. $feeds['thread_id'] .'/reply">Comment</a></span></li>';
            } else {
            $feed[] = '<li style="border-bottom:1px solid #cecfd0;padding-left:3px;line-height:20px;">['.$prefix.'] <a href="javascript:frameSrc(\''. $feeds['unique_id'].'\'); var waste = \'/threads/\';">'. $feeds['title'] .'</a><span style="float:right;"><a href="threads/'. $feeds['thread_id'] .'/reply">Comment</a></span></li>';
            }
        }
        
        $label = "Test";
        // Register for use in the template
        $viewParams = array(
            'count' => $count,
            'page' => $page,
            'perPage' => $perPage,
            'username' => $userName,
            'visitor_id' => $visitor_id,
            //'feeds' => $allFeeds,
            //'prefixGroup' => $prefixGroup,
            'prefix' => $prefix,
            //'data' => $data,
            'feed' => $feed,
            'feeds' => $feeds,
            'label' => $label
        );

        return $this->responseView('NewsReader_ViewPublic_News', 'nr_index', $viewParams);

You are building the $feed array and then passing it as a viewParam, so you can use xen:foreach in the nr_index template to parse through that array.

But I disagree with your use of HTML code in the controller. It will work, but HTML belongs in the templates. I would pass the $allFeeds array as a viewParam, then use xen:foreach to process that in the template.
 
The problem I need to call phrases and use a foreach but I cant pass the phrase var through a foreach and attach $prefIx to the end of {xen:phrase thread_prefIx_}$prefIx and It work In the template.
 
Hmm OK. You are correct about the phrases. Phrase names in the templates must be literals.

However, you can pass values into phrases using template syntax. Here is an example:

Code:
{xen:phrase rss_feed_for_x, 'title={$xenOptions.boardTitle}'}

I don't know if that will work for your application.
 
nope cuz thread prefIxs are housed as thread_prefIx_1, thread_prefIx_2, etc. If I can use a foreach In the fIle and Include the phrases then pass to the template.
 
Just an update, I figured it out, I have to use the following to make it work:

HTML:
{xen:helper threadPrefix, $p.prefix_id, escaped, ''}

So if anyone wants to use thread prefixes somewhere else on the site, just run a query to grab the id's then use the above to make it work on pages.
 
Top Bottom