Page 1 of 1

How to setting global variables in opencart 2.3 template?

Posted: Tue Aug 02, 2016 9:51 pm
by opencartplus.com
Hi everybody,

In opencart 2.2 or older, when I want to set a global variable in template (.tpl files), I just use "global", for example:
global $config;
$config_theme = $config->get('config_theme');
but it did not work opencart 2.3.

Because predefined global $config in controller rather inconvenient (we have use config value in many template file).
Hopefully someone with more experience will have an answer to this problem.

Thanks in advance,
Regards

Sorry for my limited English.

Re: How to setting global variables in opencart 2.3 template

Posted: Tue Aug 02, 2016 10:20 pm
by straightlight
opencartplus.com wrote:Hi everybody,

In opencart 2.2 or older, when I want to set a global variable in template (.tpl files), I just use "global", for example:
global $config;
$config_theme = $config->get('config_theme');
but it did not work opencart 2.3.

Because predefined global $config in controller rather inconvenient (we have use config value in many template file).
Hopefully someone with more experience will have an answer to this problem.

Thanks in advance,
Regards

Sorry for my limited English.
This approach would not be a suggested one but rather to implement the:

Code: Select all

$data['config_theme'] = $this->config->get('config_theme');
then to use the: $config_theme variable in the related controller file which the $config object is already registered through the Registry engine throughout the platform. Using global methods is not safe. However, another method you could imply by passing variables would be to take an example from the catalog/controller/checkout/cart.php file where the total modules are being initiated. Each assembled variables are being passed into an array which can then be reliable within each loaded order total files.

For this request, rather than using order totals, you could definitely use it for your own purposes in your own extensions / modules. :)

Re: How to setting global variables in opencart 2.3 template

Posted: Wed Aug 03, 2016 4:17 pm
by opencartplus.com
Thanks straightlight,

Ignoring the issues about safe or not safe. I just want an answer how to setting global variables in opencart 2.3 template?

Tks

Re: How to setting global variables in opencart 2.3 template

Posted: Sat Oct 15, 2016 7:06 am
by ThePath
Hi guys,

Did you find a solution for this.

I think the problem here is say I have a load of variables for a module, this module has to be inserted into the footer.tpl however and not its own template file. So I use vqmod to achieve it but now I cant use the global variable I cant pull the required variables into where I need them.

So are you telling me the only thing I can do is to add all these variables I require for my extension into the footer controller?

Thanks

Re: How to setting global variables in opencart 2.3 template

Posted: Sat Oct 15, 2016 8:27 am
by IP_CAM
.....

Re: How to setting global variables in opencart 2.3 template

Posted: Sun Oct 16, 2016 12:52 am
by ThePath
Really you are sorry for the harsh words are you. I doubt you are otherwise you probably would have kept them to yourself.

I think you will probably find that the success of OpenCart is in part because of us "hobby type devs". I can also tell you that I used the global variable from the advice of a very experienced OC coder who is very well respected around here.

I suspect you mean unsafe and not "unsave". Since you are an expert I will point you to one of my sites that uses this technique and watch you hack it?

Since I've been doing a bit of reading on the subject I have seen other people say its not secure, I still don't know why because nobody has fully explained it...including you.

Perhaps instead of being critical for the sake of it you should offer a solution to help "hobby devs" improve. I didn't ask for a way to perpetuate the unsafe scenario, I asked for a solution. I'm guessing your dev skills don't stretch to this either, perhaps just a jumped up hobby dev.

I'd suggest backing up your critique with more information and actually help (that's what the forum is for) trying to answer the question raised and not just use it as a chance to put others down and bolster your own ego.

Re: How to setting global variables in opencart 2.3 template

Posted: Sun Oct 16, 2016 1:27 am
by artcore
I'll try to chip in from my experience as a professional amateur ;D

If you want a variable defined in the footer controller to be available in other controllers, you can use the session array.

A global var before runtime available everywhere in the templates from a single controller is not feasable in OC2+ unless by ignoring the framework mvc. You can't do $this in a template anymore but $_SESSION should work (please don't use that unless you know how to do it safely. - Session hi-jacking eg. Single search on google:http://stackoverflow.com/questions/8318 ... -variables).

You should also ask yourself if a global php var is smart as it can be overwritten by someone else's code!!!

If you want a variable to be accesible from the footer controller in all template files on the front end you can use javascript too - ajaxed in from the footer or just json_encoded in the $data array. But that would be after php is done processing so not usable for theme settings and the likes.

I'm not aware of safety issues using (php)global but in general anything global is frowned upon.

Hope this helps, please ammend if needed :D

Re: How to setting global variables in opencart 2.3 template

Posted: Sun Oct 16, 2016 3:34 am
by ThePath
Thank you artcore a much more useful and informative answer!

Cool, I will have a look into sessions.

Well the extension has a lot of settings, set in its own module but its the footer template that HAS to pull them in and use them. I could do it another way but it would limit certain bits of the functionality.

Can't help but think it would be useful if a set of classes etc existed to help.

Re: How to setting global variables in opencart 2.3 template

Posted: Sun Oct 16, 2016 4:48 am
by artcore
Hey ThePath could you elaborate on your goal, might be a fun brain teaser ;)

Re: How to setting global variables in opencart 2.3 template

Posted: Wed Oct 26, 2016 8:00 pm
by ThePath
Hi, thanks artcore

Yeh of course. Well I built a custom footer extension a while back: https://www.opencart.com/index.php?rout ... n_id=25293

Essentially it has loads of options to create your own footer but it doesn't use it's own template file, it hooks into your existing footer.tpl

The reason I did this was I wanted to allow the ability for users to keep there existing OpenCart footer also. So there is an option to overwrite the default footer or keep it.

So not being able to access the variables because they're not part of the footer controller is a bit of a pain.

I guess I could rework everything and use vqmod to edit the footer controller rather than have the extension use it's own controller but that seems like it may cause more issues.

I just edited someone's product.tpl file and I noticed the use of:

Code: Select all

$theme_options = $this->registry->get('theme_options');
$config = $this->registry->get('config'); 
I've never seen this before, need to investigate.

@IP_CAM thanks for proving my point, not even the strength of conviction to stick by your "harsh words".

Cheers

Re: How to setting global variables in opencart 2.3 template

Posted: Wed Oct 26, 2016 9:19 pm
by artcore
Very cool extension! A lot of work for sure :D
So if I understand correctly you add to the existing footer tpl using settings from the settings table?

I would definitly create my own controller and matching tpl and as for the choice of keeping the original, you can just add it as this->load>controller as of version 2+ and getChild() in OC1+

So create your own controller say customFooter extends Controller...etc
and add $data['original_footer'] = $this->load->controller('common/footer'); which can be echoed in your own custom_footer.tpl if your customers choose to. So echo $original_footer would display the footer as we know it but from your own controller.

The only ocmod would be to hide the original footer: replace: <?php echo $footer; ?> with <?php echo $custom_footer; ?>

In OC2+ you cannot use any version of $this. But for OC1+ I would do the same. Now you have all vars from your own controller to pass easily.

Hope it helps, let me know if you need more info.

Cheers