【问题标题】:I can't access data from controller to view(tpl = template) OpenCart我无法从控制器访问数据以查看(tpl = 模板)OpenCart
【发布时间】:2019-02-01 12:50:11
【问题描述】:

无法从控制器访问变量数据到视图(模板 tpl 文件) 我有非常基本的功能

public function index() {
$message = "hello";
return $this->load->view('common/hello.tpl', $message );
} 


In view template i try to get $message variable but not defined

<?php echo $message; ?>

public function index() {
$message = "hello";
return $this->load->view('common/hello.tpl', $message );
} 

在视图模板 hello.tpl 我尝试获取 $message 变量但未定义

这只是一个例子。我有必须加载的模型,但现在我只需要从控制器访问视图..帮助..

【问题讨论】:

    标签: php opencart


    【解决方案1】:

    在您给定的代码中,模板没有机会知道有关名为$message 的变量的任何信息,因为您只传输该变量的。根据another SO thread,您应该将该变量添加到像$data 这样的数组中,如下所示:

    $data['message'] = $message;
    

    这使得$message 的内容可以在您的模板中以相同的名称使用。如果您更改密钥(例如更改为$data['otherKey']),它将在$otherKey 下可用

    然后,将该值数组传输到view 方法:

    return $this->load->view('common/hello.tpl', $data );
    

    【讨论】:

    • Okej 我在控制器 $this->data['message'] = $message; return $this->load->view('common/reifenmontage.tpl', $data); } 这在视图中 tpl 不工作我尝试设置也返回 $this->load->view('common/reifenmontage.tpl', $message);还没有工作的原因?始终未定义变量
    • @AlexAl 在查看了当前的约定后,我再次编辑了我的答案。那个帖子有点老了……
    • 我试试这个但已经不行了 $data['message'] = $message;返回 $this->load->view('common/reifenmontage.tpl', $data );在视图中 再次不是未定义的变量
    • 好吧,这个 reifenmontage.tpl 是我的真实文件名,我写 hello.tpl 只是为了解释你。我对 MCV 有很好的了解,我在 Laravel 工作,但是这个 opencart 对我来说很困惑。 hello.tpl 是为了便于理解.... 有什么问题?
    • 如果我知道这里出了什么问题,我很乐意帮助你。但目前,我没有想法 - 抱歉
    【解决方案2】:

    首先您需要发布正在使用的 OC 版本...因此,如果您在控制器文件中使用 OC 版本 1.x,您应该像这样定义您的数据: $this-&gt;data['message'] = 'hello'; 并渲染 tpl:

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

    如果您使用版本 2 或更高版本,您应该定义: $data['message'] = 'hello'; 并在 tpl 中渲染:

    return $this->load->view('extension/payment/hello', $data);
    

    在 tpl 中检索数据: &lt;?php echo $message: ?&gt; 如果 OC 版本 3.x.x 在哪里使用树枝模板... 检索数据{{ message }}

    这很简单,如果你看一下它在 OC 对应版本中是如何工作的。

    所以对于您定义的 OC2.3,应该是: 控制器 hello.php 文件:

    <?php
    class ControllerCommonHello extends Controller {
        public function index() {
    
        $data['hello'] = 'Hello!!!';
    
            $data['column_left'] = $this->load->controller('common/column_left');
            $data['column_right'] = $this->load->controller('common/column_right');
            $data['content_top'] = $this->load->controller('common/content_top');
            $data['content_bottom'] = $this->load->controller('common/content_bottom');
            $data['footer'] = $this->load->controller('common/footer');
            $data['header'] = $this->load->controller('common/header');
    
            $this->response->setOutput($this->load->view('common/hello', $data));
        }
    }
    

    hello.tpl 文件如下:

    ?php echo $header; ?>
    <div class="container">
    
     <?php echo $hello; ?>
    
    
    </div>
    <?php echo $footer; ?>
    

    【讨论】:

    • 好吧,我试试这个,但没有成功。看我的代码 Controller class ControllerCommonMotorTypeCustom extends Controller { public function index() { $data['message'] = 'TEST';返回 $this->load->view('common/motor_type_custom', $data); } } 和我的视图 tpl 文件

      DSA

      underfined 变量 除了变量 $message 之外的所有工作......???错在哪里?错误不在文件名中...我不知道在哪里?
    • 也许我必须在任何地方注册这条路线?
    • 哪个版本的 OC?
    • // 版本定义('VERSION', '2.3.0.2');
    • 什么问题?也许 url 不读取我的功能?我真的不知道原因。我是打开购物车的新手
    猜你喜欢
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多