XF 2.2 Better solution for passing array through viewparam

emiya

Member
Hi,

I want pass an array through the viewparam and I'm asking if there's a better solution for this instead of this what I did:

PHP:
 $UpgradeFinder = $this->finder('XF:UserUpgrade')
            ->order('user_upgrade_id', 'asc');

/** @var \XF\Entity\UserUpgrade $upgrades */
$upgrades = $UpgradeFinder->fetch();

$upgrade1 = $upgrades[1];
$upgrade2 = $upgrades[2];
$upgrade3 = $upgrades[3];

$viewParams = [
    'costs_1' => $upgrade1['cost_amount'],
    'costs_2' => $upgrade2['cost_amount'],
    'costs_3' => $upgrade3['cost_amount'],
    'title_1' => $upgrade1['title'],
    'title_2' => $upgrade2['title'],
    'title_3' => $upgrade3['title'],
    'descr_1' => $upgrade1['description'],
    'descr_2' => $upgrade2['description'],
    'descr_3' => $upgrade3['description']
    ];

The thing is that the template is created individually for each upgrade pack, that's why I can't use the <xf:foreach> tag or maybe I can, but I don't know how to solve this properly.

I look forward to your help.

Kind regards
 
You can access array variables within a template just fine. So let's say you just had this:

PHP:
$viewParams = [
    'upgrades' =>  $UpgradeFinder->fetch();
];

In a XenForo template you can get at the variables within an array like so...
Code:
{$upgrades.1.title}
{$upgrades.1.description}
{$upgrades.1.cost_amount|number}
 
Back
Top Bottom