如何将模块与多个tpl文件链接


How to link module with multiple tpl file?

是否有任何方法可以添加链接到具有多个模板(.tpl)文件的任何模块?

我不确定,但喜欢这样:OpenCart欢迎控制器文件。

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

但这个例子不起作用。

对于new.tpl,您必须创建一个名为new的模块

方法如下:

  1. 创建目录/controller/custom/new.php

    <?php  
     class ControllerCustomNew extends Controller {
     public function index() {
       if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/custom/new.tpl')) {
          $this->template = $this->config->get('config_template') . '/template/common/new.tpl';
          } else {
            $this->template = 'default/template/common/new.tpl';
          }
          $this->response->setOutput($this->render());
      }
    }
    ?>
    
  2. 在catalog/view/theme/default/template/custom/new.tpl 下创建一个新的.tpl文件

    <p>This is new module content</p>
    
  3. 在createcatalog/controller/custom/welcome.php 下创建一个自定义欢迎控制器

     <?php  
      class ControllerCustomWelcome extends Controller {
       public function index() {
          if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/custom/welcome.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/custom/welcome.tpl';
          } else {
           $this->template = 'default/template/custom/welcome.tpl';
          }
         $this->children = array(
           'common/column_left',
           'common/column_right',
           'common/content_top',
           'common/content_bottom',
           'common/footer',
           'common/header',
           'custom/new'
          );
        $this->response->setOutput($this->render());
       }
      }
     ?>
    
  4. 在catalog/view/theme/default/template/custom/welcome.tpl 下创建一个welcome.tpl文件

     <?php echo $header;?>
     <p>This is Welcome module content</p>
     <?php echo $new; ?> <!-- new module content -->
     <?php echo $footer; ?>
    
  5. 在前端加载欢迎模块,即http://yourdomain.com/index.php?route=custom/welcome