0

我有一个自定义的 Joomla 投资组合组件,它输出以下代码(如下)。我想要做的是在同一页面上的模块中回显名称变量,但我目前的尝试没有奏效。这是可能的,如果是的话,我哪里错了?

我的组件 default.php 输出...

// no direct access
defined('_JEXEC') or die;

 //Load admin language file
 $lang = JFactory::getLanguage();
 $lang->load('com_portfolio', JPATH_ADMINISTRATOR);

?>
<?php if ($this->item) : ?>

<div class="item_fields">

    <ul class="fields_list">

                    <li><?php echo JText::_('COM_PORTFOLIO_FORM_LBL_TEMPLATE_ID'); ?>:
        <?php echo $this->item->id; ?></li>
        <li><?php echo JText::_('COM_PORTFOLIO_FORM_LBL_TEMPLATE_ORDERING'); ?>:
        <?php echo $this->item->ordering; ?></li>
        <li><?php echo JText::_('COM_PORTFOLIO_FORM_LBL_TEMPLATE_STATE'); ?>:
        <?php echo $this->item->state; ?></li>
        <li><?php echo JText::_('COM_PORTFOLIO_FORM_LBL_TEMPLATE_CHECKED_OUT'); ?>:
        <?php echo $this->item->checked_out; ?></li>
        <li><?php echo JText::_('COM_PORTFOLIO_FORM_LBL_TEMPLATE_CHECKED_OUT_TIME'); ?>:
        <?php echo $this->item->checked_out_time; ?></li>
        <li><?php echo JText::_('COM_PORTFOLIO_FORM_LBL_TEMPLATE_CREATED_BY'); ?>:
        <?php echo $this->item->created_by; ?></li>
        <li><?php echo JText::_('COM_PORTFOLIO_FORM_LBL_TEMPLATE_NAME'); ?>:
        <?php echo $this->item->name; ?></li>
        <li><?php echo JText::_('COM_PORTFOLIO_FORM_LBL_TEMPLATE_CUSTOM_CLASS'); ?>:
        <?php echo $this->item->custom_class; ?></li>
        <li><?php echo JText::_('COM_PORTFOLIO_FORM_LBL_TEMPLATE_CATEGORY'); ?>:
        <?php echo $this->item->category_title; ?></li>
        <li><?php echo JText::_('COM_PORTFOLIO_FORM_LBL_TEMPLATE_THUMB'); ?>:
        <?php echo $this->item->thumb; ?></li>
        <li><?php echo JText::_('COM_PORTFOLIO_FORM_LBL_TEMPLATE_IMAGE1'); ?>:
        <?php echo $this->item->image1; ?></li>
        <li><?php echo JText::_('COM_PORTFOLIO_FORM_LBL_TEMPLATE_IMAGE2'); ?>:
        <?php echo $this->item->image2; ?></li>
        <li><?php echo JText::_('COM_PORTFOLIO_FORM_LBL_TEMPLATE_IMAGE3'); ?>:
        <?php echo $this->item->image3; ?></li>
        <li><?php echo JText::_('COM_PORTFOLIO_FORM_LBL_TEMPLATE_IMAGE4'); ?>:
        <?php echo $this->item->image4; ?></li>
        <li><?php echo JText::_('COM_PORTFOLIO_FORM_LBL_TEMPLATE_DESCRIPTION'); ?>:
        <?php echo $this->item->description; ?></li>
        <li><?php echo JText::_('COM_PORTFOLIO_FORM_LBL_TEMPLATE_STATS'); ?>:
        <?php echo $this->item->stats; ?></li>
        <li><?php echo JText::_('COM_PORTFOLIO_FORM_LBL_TEMPLATE_DEMO_LINK'); ?>:
        <?php echo $this->item->demo_link; ?></li>
        <li><?php echo JText::_('COM_PORTFOLIO_FORM_LBL_TEMPLATE_BUY_LINK'); ?>:
        <?php echo $this->item->buy_link; ?></li>
        <li><?php echo JText::_('COM_PORTFOLIO_FORM_LBL_TEMPLATE_TAGS'); ?>:
        <?php echo $this->item->tags; ?></li>


    </ul>

</div>

我想做的是将 name 变量回显到一个模块中 - 这就是我尝试过的......

<?php 
defined('_JEXEC') or die('Access Deny');
$lang = JFactory::getLanguage();
$lang->load('com_portfolio');
?>


<?php echo JText::_('COM_PORTFOLIO_FORM_LBL_TEMPLATE_NAME'); ?>:
<?php echo $item->name; ?>

该组件功能正常,并且该模块也可以通过简单的 hello world echo 完成,所以我没有想法。

4

2 回答 2

0

我建议使用模块中组件的模型。类似以下代码的东西可以获取模块中的项目

JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_portfolio/models', 'PortfolioModel');
$model = JModelLegacy::getInstance('Portfolio', 'PortfolioModel');
$model->getState();
// if you want to set a state
$model->setState('filter.category_id', $catid);
$item = $model->getItem();

然后,您可以在模型本身中缓存该项目以防止第二次数据库访问。另一种解决方法。

于 2013-10-01T11:54:37.833 回答
0

我找到了一个简单的解决方法。我只是设置了一个会话

$session =& JFactory::getSession();
$session->set( 'myVar', $this->item->name ); 

并在我的模块中回显...

<?php echo htmlspecialchars($session->get( 'myVar', 'empty' )); ?>

我不知道这是否是一种完全安全或有效的工作方式,但它至少现在可以正常工作。

于 2013-09-30T14:40:38.337 回答