ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Global variables or embeded templates? Peformance difference?

August 18, 2007 12:52pm

Subscribe [6]
  • #1 / Aug 18, 2007 12:52pm

    JT Thompson

    745 posts

    Which would you say is better to use?

    I’m asking this question because I’m helping someone with their site and I noticed that the developer (who did a wonderful job) used global variables for the site framework.

    Personally I use embeded templates but I’m curious as to which is better? Is there a performance difference calling templates? Which is more efficient?

    Additionally, what’s your thoughts (whoever replies) as to ease and simplicity? What’s the line of thought? My thoughts are it’s easier to use templates because…. well because that’s just the way I did it. But I also know that a lot of site engines that are designed this way are done almost exclusively using those global vars.

    I’m juuuuuuuuuust about finished with a new, much better looking layout for my site. But this is the time to change things. I didn’t even think about it until I saw it done using vars. If that’s the best way to do this I’d rather get in there now and restructure it before i go live. This time I tried to think in semantics and template as much as possible. eg: i made a template group for javascript and one for styles. etc etc.

  • #2 / Aug 18, 2007 2:46pm

    BlackHelix

    226 posts

    Well, global variables are almost always far better for performance reasons.  There’s an order of magnitude different in speed and efficiency if you use global variables. 

    But global variables are really global constants—they cannot change dynamically, nor have any tags, etc in them.  So they are great for things like footer text that is static: “Designed by El Bubba’s internet hot spot design” or something…. something you can easily change.  But if you have a need for a tag or other things that change, you have to use embeds. 

    For me, I use lots of both—I have my javascript, favicons, lots of other HTML header info in global vars.  I have my footer text in global vars.  I have my menu in an embedded template, because I want to use some dynamic stuff depending on the page the visitor is on. 

    I’d take a good, long look at Behind the Curtain, Part II by Derek Jones on this subject.  He’s a huge fan of global vars… in their place.  And that entry really helped my in my site design, too. 

    I’ll quote his line on this:

    The choice of whether to use an embedded template or a Template Global Variable is rather easy: if you need to use ExpressionEngine tags or PHP, then you need an embedded template.  But if not, Template Globals are incredibly efficient, and still let you affect site-wide changes by modifying things in one place.

    Hope that helps some!

  • #3 / Aug 18, 2007 2:59pm

    JT Thompson

    745 posts

    Thanks for the response.

    That pretty much completely answers my question. It’s funny I didn’t even think of doing it like that. But it really does make sense. I use globals for lots of things, i’m just surprised it didn’t dawn on me that this would be the best way. Guess it shows I’m still REALLY new.

    Thanks for that answer! back to work for me 😊

  • #4 / Aug 18, 2007 3:07pm

    Lisa Wess

    20502 posts

    JT, head to the developer’s blog and read the Behind the Curtain series - in that, Derek explains much of how this site is built. You might find it enlightening =)

  • #5 / Aug 18, 2007 3:20pm

    JT Thompson

    745 posts

    Hi Lisa,
    After reading the one linked above my post I’m going to read all of that series. I wish I’d had time to do so in the beginning. but thankfully since I’m rebuilding now I am free to do so completely. The only thing I’m pulling from the old site is the stories themselves. everything else is done completely independant.

    So the reality is, a site that I thought was at probably 95% now is really down to about 50% with all the rebuild stuff I need to do 😊 yuck.

    On the flip side though, since I templated everthing modular it will be simple to take those templates and put them as gobals. the structure is already broken up, so the hard part is already done

  • #6 / Aug 18, 2007 3:26pm

    Lisa Wess

    20502 posts

    Just as a note, JT, if thsoe templates have no PHP or EE code in them, you could just as easily swap them to template type: static, which has the same benefits as global variables.  I personally like the “look” of global variables in the templates better - I Find it easier to read, but hey. =)

  • #7 / Aug 18, 2007 3:31pm

    JT Thompson

    745 posts

    That does make sense. But I agree with you that the globals look better. I looked through the site I was talking about and was really impressed with how clean the developer had done it. I want mine to look that way too so I’m going to do it.

    I did find one thing though, you can sort of use things with tags depending on what they are in the globals. eg: i have a new dhtml menu I wrote for the site. At the end I put a search form which uses tags. The form still worked fine. So what’s the real limit? is it limited because if you have things like comments in the global they won’t work because the actual tag for entries isn’t opened in it? Where does the line actually go?

  • #8 / Aug 19, 2007 3:51am

    BlackHelix

    226 posts

    I’d be careful about opening or closing tags in global vars that aren’t balanced…such as a closing div or something.  Unless you KNOW it’s going to be called. 

    If you are talking about using a EE tag, or the EE comments functions—they cannot go inside a global var.  It’s just a chunk of text or html or whatever that is inserted anywhere the global var is put.  No processing at all is done, so in some senses they are limited.  But, if you look at the link I gave, in the example code on that page, Derek has a bunch of globals like {html_wrapper_open} and stuff.  Those open tags without closing, etc. 

    So the limit is—if EE needs to process anything INSIDE the global var, they break.  Otherwise, use them to your hearts content.  You can easily use them to test for stuff inside your templates, or {if: } statements, or what have you.  You can have a global var that is a regular constant, like a one or two word var…. or you can have a long batch of javascript, say, or your lovely footer text, or even your menu, if you don’t need your menu to have any processing done by EE. If the menu just does CSS type processing that the browser does, global vars work. 

    Does this answer your question?

  • #9 / Aug 19, 2007 4:15am

    JT Thompson

    745 posts

    Yeah it’s pretty much what I thought.

    I wish I’d have realized this years ago, or even a month ago when I began redesigning my (now ugly) site.

    Like I said above though, it’s good to see this now because I’ve only got a handful of new templates for the site, so i can easily convert this.

    Thanks!

  • #10 / Aug 19, 2007 5:01am

    Magnus Wester

    38 posts

    In most cases there is no difference between a global variable and a custom weblog field. So if you consider

    {exp:allow_eecode}
    {body}
    {/exp:allow_eecode}

    an acceptable design practice (Paul Burdick apparently does so, since he wrote the allow_eecode plug-in), I believe that in all fairness you also should accept

    {exp:allow_eecode}
    {global-variable}
    {/exp:allow_eecode}

    I’m not saying I would design a site this way, I just don’t think it’s correct to say that global vars “cannot contain EE code” or “cannot contain PHP code”.

  • #11 / Aug 19, 2007 5:09am

    JT Thompson

    745 posts

    Your point is good. I am currently using the EE search tags in a global variable. I think what I’m seeing here is that you can’t do things like make a var for comments, then call it from within something like the {exp:weblog:entries} because it won’t know what the outer tag is doing. there’s no relationship there.

    Unless I’m mistaken. But yes you’re right that I can use some tags in vars for sure.

  • #12 / Aug 19, 2007 10:10am

    Derek Jones

    7561 posts

    I’m not saying I would design a site this way, I just don’t think it’s correct to say that global vars “cannot contain EE code” or “cannot contain PHP code”.

    Actually, Magnus, that’s a fair statement: they can’t.  Global Variables are parsed at the very end of the template engine, long after ExpressionEngine tags and PHP are evaluated.  And as a side note, just because Paul made a plugin, does not mean that he would approve of every use of it.  That is perhaps one of the most abused and misused plugins that exists, and I’m fairly confident that Paul shares my general distaste for putting ExpressionEngine tags inside entries without reason.  At any rate, the reason EE tags in Global Variables are not parsed is not because they are protected in some fashion as they are by default in entries, but that there’s simply no more template parsing that occurs after Global Variables.  Turn on the Template parser log in your Output and Debugging preferences for a page that has a few embedded templates and have a look for the line ‘Parse Global Variables’ (and the line immediately preceding it).

    Just as a note, JT, if thsoe templates have no PHP or EE code in them, you could just as easily swap them to template type: static, which has the same benefits as global variables.

    Lisa is mostly right here, but with a caveat.  All of your global variables are fetched and parsed once for a page load.  Static embedded templates are still parsed and fetched individually, so it would definitely be possible to construct a page that was much more expensive using static embedded templates instead of global variables.  Again, a look a the parser log will reveal the difference.

  • #13 / Aug 19, 2007 11:32am

    Stephen Slater

    366 posts

    JT,

    I’m going to add my recent experience to this discussion because it adds another dynamic to your decision…

    Recently, I set up a site that took advantage of Global Variables and it worked out beautifully… or so I thought.  Weeks after the project was complete, the client wanted to duplicate the site, change a few things and host the second site at a unique URL.  Perfect, I thought, I’ll use the new Multi-Site Manager.  Upon installing MSM, sharing weblog entries, templates, prefs, etc… I learned the Global Variables were not shareable.  eek!  I had to restructure all of my global variables back to static templates in order to share them.

    This ended up not being too big of deal, but had I known prior to setting up the first site, the second one would have been a breeze.

    My point:  If there is any reason you would be using the Multi-Site Manager in the future, keep in mind that Global Variables cannot be shared between sites.

  • #14 / Aug 19, 2007 1:20pm

    JT Thompson

    745 posts

    Well I should correct something here. I said I had code for the search form in my gloabal but that’s not the case. I had forgotten that I tried to use the tags and they did not work. So i replaced
    {exp:search:simple_form search_in="everywhere" result_page="sg_distro/search_results"} with a long form.

    So now it makes more sense.

    Stephenslater thanks for the heads up. I don’t think msn is something I’ll need since this is just a personal site but that’s nice to know.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases