问题
I'd like to include a script to detect $_GET
values on certain pages of the site. If I use the script in header.tpl
, it will add the script on all pages. If I use it as a template file and display it as Layout
in the Extensions > Modules, it gets added to all the pages within that Layout. Is there a way to include this only on certain selected pages? I'm sure this will have to done programatically. Just wanted to find out if there is an elegant way to do this.
回答1:
You can access the $_GET
files using the correct method of $this->request->get
in any of the controller, model or view files. As for an elegant solution, that really depends on which pages you want it to show on and also what region of the page. For example, if you only want to use code on the product pages, but want to have the code placed in the header, you would need to check for the route to be product/product
in your header.php
controller in the index()
method, then pass the data to the template using $this->data['your-variable'] = $some_data;
. Similarly, this would be almost the same on a product page using the product.php
controller and skipping the route check since that controller will only be called on product pages
来源:https://stackoverflow.com/questions/16974558/opencart-how-to-include-a-script-on-certain-pages