Post by opencartplus.com » Tue Aug 02, 2016 9:51 pm

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.


Posts

Joined
Mon Oct 22, 2012 3:35 pm

Post by straightlight » Tue Aug 02, 2016 10:20 pm

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. :)

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by opencartplus.com » Wed Aug 03, 2016 4:17 pm

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


Posts

Joined
Mon Oct 22, 2012 3:35 pm

Post by ThePath » Sat Oct 15, 2016 7:06 am

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

Image

OpenCart Theme Options - See All My Extensions - OpenCart Themes and Mods


User avatar
Active Member

Posts

Joined
Fri Jun 26, 2009 11:53 pm
Location - Scotland

Post by IP_CAM » Sat Oct 15, 2016 8:27 am

.....
Last edited by IP_CAM on Wed Oct 19, 2016 3:41 am, edited 1 time in total.

My Github OC Site: https://github.com/IP-CAM
5'200 + FREE OC Extensions, on the World's largest private Github OC Repository Archive Site.


User avatar
Legendary Member

Posts

Joined
Tue Mar 04, 2014 1:37 am
Location - Switzerland

Post by ThePath » Sun Oct 16, 2016 12:52 am

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.

Image

OpenCart Theme Options - See All My Extensions - OpenCart Themes and Mods


User avatar
Active Member

Posts

Joined
Fri Jun 26, 2009 11:53 pm
Location - Scotland

Post by artcore » Sun Oct 16, 2016 1:27 am

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

Attn: I no longer provide OpenCart extensions, nor future support - this includes forum posts.
Reason: OpenCart version 3+ ;D

Thanks!


User avatar
Active Member

Posts

Joined
Tue Jul 09, 2013 4:13 am
Location - The Netherlands

Post by ThePath » Sun Oct 16, 2016 3:34 am

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.

Image

OpenCart Theme Options - See All My Extensions - OpenCart Themes and Mods


User avatar
Active Member

Posts

Joined
Fri Jun 26, 2009 11:53 pm
Location - Scotland

Post by artcore » Sun Oct 16, 2016 4:48 am

Hey ThePath could you elaborate on your goal, might be a fun brain teaser ;)

Attn: I no longer provide OpenCart extensions, nor future support - this includes forum posts.
Reason: OpenCart version 3+ ;D

Thanks!


User avatar
Active Member

Posts

Joined
Tue Jul 09, 2013 4:13 am
Location - The Netherlands

Post by ThePath » Wed Oct 26, 2016 8:00 pm

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

Image

OpenCart Theme Options - See All My Extensions - OpenCart Themes and Mods


User avatar
Active Member

Posts

Joined
Fri Jun 26, 2009 11:53 pm
Location - Scotland

Post by artcore » Wed Oct 26, 2016 9:19 pm

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

Attn: I no longer provide OpenCart extensions, nor future support - this includes forum posts.
Reason: OpenCart version 3+ ;D

Thanks!


User avatar
Active Member

Posts

Joined
Tue Jul 09, 2013 4:13 am
Location - The Netherlands
Who is online

Users browsing this forum: No registered users and 74 guests