【问题标题】:loading a view via function in controller通过控制器中的函数加载视图
【发布时间】:2013-08-05 07:57:10
【问题描述】:

当在 opencart 中选择产品时,我试图在选项卡上获取尺寸图表。为此,我有一个如下所示的模型

class ModelCatalogSizes extends Model {     
    public function getSizes()
    {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "sizes ORDER BY id");
        return $query->rows;
    }
}

我将视图作为模板,它只是一个带有值作为输入单选选项的 html 表。

在我的产品控制器类中,我有如下功能

public function sizes()
{
    $this->language->load('product/product');

    $this->load->model('catalog/sizes');

    $this->data['sizes'] = array();

    $results = $this->model_catalog_sizes->getSizes();

    foreach ($results as $result) {
        $this->data['sizes'][] = array(
            'type'       => ucfirst($result['type']),
            'coat'       => $result['coat'],
            'chest'      => $result['chest'],
            'cverarm'    => $result['overarm'],
            'waist'      => $result['waist'],
            'hip'        => $result['hip'],
            'inseam'     => $result['inseam'],
            'neck'       => $result['neck'],
            'sleeve'     => $result['sleeve'],
            'height'     => $result['height'],
            'weightLb'   => $result['weightLb'],
            'weightKg'   => $result['weightKg'],
        );
    }

    if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/sizes.tpl')) {
        $this->template = $this->config->get('config_template') . '/template/product/sizes.tpl';
    } else {
        $this->template = 'default/template/product/sizes.tpl';
    }

    $this->response->setOutput($this->render());
}

现在我正在尝试通过产品控制器类中的索引函数直接加载此视图,方法是说$this->data['size'] = $this->sizes();

当我在产品视图中回显我的 $size 时,什么也没有出现。我认为应该显示在上面的函数中构建的整个视图。我错了吗(概率 99%)?有人可以帮我通过函数直接发布视图吗?

【问题讨论】:

  • 另一种解决方案,您将sizes.tpl的html粘贴到product.tpl的if条件中,而不是$this->response->setOutput($this->render());只是从index()函数中的sizes()获取数据

标签: php templates opencart


【解决方案1】:

您需要做的是将其路由添加为 index() 方法的孩子的孩子 打开/catalog/controller/product/product.php,在index()方法底部找到这段代码

$this->children = array(
    'common/column_left',
    'common/column_right',
    'common/content_top',
    'common/content_bottom',
    'common/footer',
    'common/header'
);

并添加您的路线,即 'product/product/sizes' 用于您的 sizes() 方法

$this->children = array(
    'common/column_left',
    'common/column_right',
    'common/content_top',
    'common/content_bottom',
    'common/footer',
    'common/header',
    'product/product/sizes'
);

然后在你的模板中,你只需要在任何你想去的地方使用<?php echo $sizes; ?>

【讨论】:

  • 是的,是的,你必须把它称为孩子。
猜你喜欢
  • 2012-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多