Deep Blue 76 Posted May 5, 2019 Posted May 5, 2019 I've created a custom static method in \IPS\Member: static function hello() { $hello = "something"; return $hello; } Now I need to retrieve it in a template: {{$hello = \IPS\Member::hello();}} but forums/topicRow template, where I'm working, start with a foreach and I can't hook before it to assign passing $hello. I've found a dirty workaround but what is the best way to do it?
HeadStand Posted May 5, 2019 Posted May 5, 2019 You need to create a theme hook on the template and add the code in the beginning. So you'd set up a theme hook on forums -> front -> forums. Add one to topicRow. Your selector will be li.ipsDataItem Then you can insert php code before the selector. Something like {{expression="\IPS\Member::hello();"}}
InvisionHQ Posted May 5, 2019 Posted May 5, 2019 There are no selector to hook in this template before the foreach
Deep Blue 76 Posted May 5, 2019 Author Posted May 5, 2019 <ips:template parameters="$table, $headers, $rows" /> {{$rowIds = array();}} {{foreach $rows as $row}} {{$idField = $row::$databaseColumnId;}} {{$rowIds[] = $row->$idField;}} {{endforeach}} {{if \count( $rows )}} {{$rowCount=0;}} {{foreach $rows as $row}} {{if $rowCount == 1 AND $advertisement = \IPS\core\Advertisement::loadByLocation( 'ad_forum_listing' )}} <li class="ipsDataItem"> {$advertisement|raw} </li> {{endif}} {{$rowCount++;}} {{$idField = $row::$databaseColumnId;}} {{if $row->mapped('moved_to')}} {{if $movedTo = $row->movedTo() AND $movedTo->container()->can('view')}} <li class="ipsDataItem"> If I've a query inside my hello() then if I hook on li.ipsDataItem I've 1 query x num topics
bfarber Posted May 6, 2019 Posted May 6, 2019 If I'm understanding you correctly, you want to hook in to the topicRow template before the foreach loop, is that right? Create a theme hook on the appropriate template file, and this will create a plugin file in your plugin directory with nothing but a hookData() method in it. This plugin file extends the template code, so you can then manually define the topicRow template and call the parent like so //<?php /* To prevent PHP errors (extending class does not exist) revealing path */ if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) ) { exit; } class hook347 extends _HOOK_CLASS_ { /* !Hook Data - DO NOT REMOVE */ public static function hookData() { return array_merge_recursive( array (), parent::hookData() ); } /* End Hook Data */ public function topicRow( $table, $headers, $rows ) { // Do whatever you need to do here return parent::topicRow( $table, $headers, $rows ); } }
Recommended Posts
Archived
This topic is now archived and is closed to further replies.