如何将变量添加到header.php控制器并在header.tpl中使用它

时间:2016-10-17 13:17:29

标签: opencart2.3

我正在为OpenCart 2.3创建自定义主题,我需要在页眉(header.tpl)中显示一些其他信息。所以我在catalog / controller / common / header.php中添加了一些变量:

$data['some_var'] = 'some_value';

然后我试图在header.tpl中使用这些数据:

<?php echo $this->data['some_var']; ?>

但我总是收到这个错误:

注意:未定义的索引:第133行的/var/www/store_com/public_html/catalog/view/theme/mystore/template/common/header.tpl中的some_var

如果我尝试使用此代码:

<?php echo $some_var; ?>

然后我又收到了另一个错误:

注意:未定义的变量:第133行的/var/www/store_com/public_html/catalog/view/theme/mystore/template/common/header.tpl中的some_var

即使我在header.tpl中执行 echo print_r($ this-&gt; data),我也不会在$ data数组中看到这个变量。

我做错了什么?请帮忙。

修改
这是我的header.php控制器文件的完整代码。我在文件的最后添加了自定义变量。

class ControllerCommonHeader extends Controller {
public function index() {
    // Analytics
    $this->load->model('extension/extension');

    $data['analytics'] = array();

    $analytics = $this->model_extension_extension->getExtensions('analytics');

    foreach ($analytics as $analytic) {
        if ($this->config->get($analytic['code'] . '_status')) {
            $data['analytics'][] = $this->load->controller('extension/analytics/' . $analytic['code'], $this->config->get($analytic['code'] . '_status'));
        }
    }

    if ($this->request->server['HTTPS']) {
        $server = $this->config->get('config_ssl');
    } else {
        $server = $this->config->get('config_url');
    }

    if (is_file(DIR_IMAGE . $this->config->get('config_icon'))) {
        $this->document->addLink($server . 'image/' . $this->config->get('config_icon'), 'icon');
    }

    $data['title'] = $this->document->getTitle();

    $data['base'] = $server;
    $data['description'] = $this->document->getDescription();
    $data['keywords'] = $this->document->getKeywords();
    $data['links'] = $this->document->getLinks();
    $data['styles'] = $this->document->getStyles();
    $data['scripts'] = $this->document->getScripts();
    $data['lang'] = $this->language->get('code');
    $data['direction'] = $this->language->get('direction');

    $data['name'] = $this->config->get('config_name');

    if (is_file(DIR_IMAGE . $this->config->get('config_logo'))) {
        $data['logo'] = $server . 'image/' . $this->config->get('config_logo');
    } else {
        $data['logo'] = '';
    }

    $this->load->language('common/header');

    $data['text_home'] = $this->language->get('text_home');

    // Wishlist
    if ($this->customer->isLogged()) {
        $this->load->model('account/wishlist');

        $data['text_wishlist'] = sprintf($this->language->get('text_wishlist'), $this->model_account_wishlist->getTotalWishlist());
    } else {
        $data['text_wishlist'] = sprintf($this->language->get('text_wishlist'), (isset($this->session->data['wishlist']) ? count($this->session->data['wishlist']) : 0));
    }

    $data['text_shopping_cart'] = $this->language->get('text_shopping_cart');
    $data['text_logged'] = sprintf($this->language->get('text_logged'), $this->url->link('account/account', '', true), $this->customer->getFirstName(), $this->url->link('account/logout', '', true));

    $data['text_account'] = $this->language->get('text_account');
    $data['text_register'] = $this->language->get('text_register');
    $data['text_login'] = $this->language->get('text_login');
    $data['text_order'] = $this->language->get('text_order');
    $data['text_transaction'] = $this->language->get('text_transaction');
    $data['text_download'] = $this->language->get('text_download');
    $data['text_logout'] = $this->language->get('text_logout');
    $data['text_checkout'] = $this->language->get('text_checkout');
    $data['text_category'] = $this->language->get('text_category');
    $data['text_all'] = $this->language->get('text_all');

    $data['home'] = $this->url->link('common/home');
    $data['wishlist'] = $this->url->link('account/wishlist', '', true);
    $data['logged'] = $this->customer->isLogged();
    $data['account'] = $this->url->link('account/account', '', true);
    $data['register'] = $this->url->link('account/register', '', true);
    $data['login'] = $this->url->link('account/login', '', true);
    $data['order'] = $this->url->link('account/order', '', true);
    $data['transaction'] = $this->url->link('account/transaction', '', true);
    $data['download'] = $this->url->link('account/download', '', true);
    $data['logout'] = $this->url->link('account/logout', '', true);
    $data['shopping_cart'] = $this->url->link('checkout/cart');
    $data['checkout'] = $this->url->link('checkout/checkout', '', true);
    $data['contact'] = $this->url->link('information/contact');
    $data['telephone'] = $this->config->get('config_telephone');

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

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

    $data['categories'] = array();

    $categories = $this->model_catalog_category->getCategories(0);

    foreach ($categories as $category) {
        if ($category['top']) {
            // Level 2
            $children_data = array();

            $children = $this->model_catalog_category->getCategories($category['category_id']);

            foreach ($children as $child) {
                $filter_data = array(
                    'filter_category_id'  => $child['category_id'],
                    'filter_sub_category' => true
                );

                $children_data[] = array(
                    'name'  => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
                    'href'  => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
                );
            }

            // Level 1
            $data['categories'][] = array(
                'name'     => $category['name'],
                'children' => $children_data,
                'column'   => $category['column'] ? $category['column'] : 1,
                'href'     => $this->url->link('product/category', 'path=' . $category['category_id'])
            );
        }
    }

    $data['language'] = $this->load->controller('common/language');
    $data['currency'] = $this->load->controller('common/currency');
    $data['search'] = $this->load->controller('common/search');
    $data['cart'] = $this->load->controller('common/cart');

    // For page specific css
    if (isset($this->request->get['route'])) {
        if (isset($this->request->get['product_id'])) {
            $class = '-' . $this->request->get['product_id'];
        } elseif (isset($this->request->get['path'])) {
            $class = '-' . $this->request->get['path'];
        } elseif (isset($this->request->get['manufacturer_id'])) {
            $class = '-' . $this->request->get['manufacturer_id'];
        } elseif (isset($this->request->get['information_id'])) {
            $class = '-' . $this->request->get['information_id'];
        } else {
            $class = '';
        }

        $data['class'] = str_replace('/', '-', $this->request->get['route']) . $class;
    } else {
        $data['class'] = 'common-home';
    }

    //CUSTOM THEME VARIABLES BEGIN
    $data['some_var'] = 'some_value';
    //CUSTOM THEME VARIABLES END

    return $this->load->view('common/header', $data);       
}
}

2 个答案:

答案 0 :(得分:0)

我需要看到你的控制器才能全面了解,然后我会给你完整的答案,但是看看你的控制器并确保绑定你的数据,如下面的示例:

if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/header.tpl')) {
        return $this->load->view($this->config->get('config_template') . '/template/common/header.tpl', $data);
    } else {
        return $this->load->view('default/template/common/header.tpl', $data);
    }

由于

答案 1 :(得分:0)

我终于找到了我的问题的解决方案,但我不确定这是否是正确的方法。我发现我需要在 system / storage / modification / catalog / controller / common / header .php 中进行更改。看起来像通过Vqmod安装一些扩展后,控制器文件已被复制到此文件夹。如果我在那里添加我的变量,那么我可以毫无问题地访问它们。