LPH
Well-known member
Within a template, there was a single callback to a single PHP file. An event listener was created as well as a Listener file. The event Listener was for the templateHook.
Listener file.
This all worked but the single PHP file used in the callback was a mixture of HTML and code. I'm wanting to separate things out -- as well as learn more about templates, etc.
This is my new model:
I also built the following view:
I know from other work that a ControllerPublic extending XenForo_ControllerPublic_Abstract can return a responseView.
I am not sure of the interaction between the Listener and ControllerPublic. I started to build this file:
But wasn't sure how the Listener and Controller would interact. I'm used to a route prefix and using getRouteMatch. But since this is a templateHook then I'm confused on how to get these to interact.
I hope that is clear. It's basically a question of the flow ... Listener to Controller and then making sure I'm passing the parameters out of the template and to the view.
Listener file.
PHP:
/**
* Class TRN_XenLate_Listener_Index
*/
class TRN_XenLate_Listener_Index
{
/**
* @param $hookName
* @param $contents
* @param array $hookParams
* @param XenForo_Template_Abstract $template
*/
public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
{
if ($hookName == 'ad_sidebar_below_visitor_panel')
{
$trn_xenword_contents = $template->create('trn_xenlate_latestposts', $template->getParams());
$contents = $trn_xenword_contents . $contents;
}
}
}
This all worked but the single PHP file used in the callback was a mixture of HTML and code. I'm wanting to separate things out -- as well as learn more about templates, etc.
This is my new model:
PHP:
/**
* Class TRN_XenLate_Model_WPPosts
*/
class TRN_XenLate_Model_WPPosts extends XenForo_Model
{
/**
* @return array|false
*/
public function getPosts()
{
define('WP_USE_THEMES', false);
define('DOCUMENT_ROOT', $_SERVER['DOCUMENT_ROOT']);
// Change the path to the correct location
require(DOCUMENT_ROOT . '/wp-blog-header.php');
$args = array(
'numberposts' => '5',
'offset' => 0,
'post_type' => 'post',
'post_status' => 'publish'
);
$LatestWPPosts = wp_get_recent_posts($args);
return $LatestWPPosts;
}
}
I also built the following view:
PHP:
/**
* Class TRN_XenLate_ViewPublic_WPPosts
*/
class TRN_XenLate_ViewPublic_WPPosts extends XenForo_ViewPublic_Base
{
public function renderHtml()
{
echo '<div class="section">
<div class="secondaryContent">
<div class="visitorText">
<h3>Latest Articles</h3>
<div class="LatestWPPosts">';
foreach ($LatestWPPosts as $LatestWPPost) {
$excerpt = apply_filters('the_excerpt', get_post_field('post_excerpt', $LatestWPPost['ID']));
$thumbnail = get_the_post_thumbnail($LatestWPPost['ID'], array(50, 50));
if (empty($thumbnail)) {
echo "<h4><a href='" . esc_url(get_permalink($LatestWPPost['ID'])) . "'>" . $LatestWPPost['post_title'] . '</a></h4>';
} else {
echo '<div class="recent_post">';
echo '<div class="thumbnail">' . $thumbnail . '</div>';
echo "<h4><a href='" . esc_url(get_permalink($LatestWPPost['ID'])) . "'>" . $LatestWPPost['post_title'] . '</a></h4><br />';
echo '<div class="excerpt">' . $excerpt . '</div>';
echo '</div>';
}
}
echo '</div>
</div>
</div>
</div>
';
}
}
I know from other work that a ControllerPublic extending XenForo_ControllerPublic_Abstract can return a responseView.
PHP:
$viewParams = array(
'latestWPPosts' => $latestWPPosts,
); // Pass variables to template
return $this->responseView('TRN_XenLate_ViewPublic_WPPosts', 'trn_xenlate_latestposts', $viewParams);
I am not sure of the interaction between the Listener and ControllerPublic. I started to build this file:
PHP:
/**
* Class TRN_XenLate_ControllerPublic_Index
*/
class TRN_XenLate_ControllerPublic_Index extends XenForo_ControllerPublic_Abstract
{
/**
* @return XenForo_ControllerResponse_View
*/
public function actionIndex()
{
$LatestWPPostsModel = XenForo_Model::create('TRN_XenLate_Model_WPPosts');
$LatestWPPosts = $LatestWPPostsModel->getPosts();
$viewParams = array(
'latestposts' => $LatestWPPosts,
); // Pass variables to template
return $this->responseView('TRN_XenLate_ViewPublic_WPPosts', 'trn_xenlate_latestposts', $viewParams);
}
}
But wasn't sure how the Listener and Controller would interact. I'm used to a route prefix and using getRouteMatch. But since this is a templateHook then I'm confused on how to get these to interact.
I hope that is clear. It's basically a question of the flow ... Listener to Controller and then making sure I'm passing the parameters out of the template and to the view.