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.

Running code before any template parsing takes place

January 12, 2011 8:34am

Subscribe [2]
  • #1 / Jan 12, 2011 8:34am

    Russ Back

    142 posts

    Hi

    I have a plugin that sets a session var (see http://ellislab.com/forums/viewthread/177971/ for details) and I then depend on that session var to compile my templates. However I am finding that the variable is not set the first time a page is compiled, presumably because my plugin is parsed too late.

    What’s the best way to run code before anything takes place? I am hoping I don’t need to add code to the /index.php file or anything like that - I’d much rather be extending in the correct way.

    Here’s an example of my page template:

    {embed="global-embeds/header"
        body-id="index"
        robots="all"
        has-aside="n"
    }
    
        <h1>Home Page</h1>
    
        {embed="test_inc/.{exp:deetective:get_template_type}_body"}
    
    {embed="global-embeds/footer"}

    Inside my global-embeds/header template, I am calling a plugin at the very top to set my session var:

    {exp:deetective:set_template_type}
    
    <!doctype html>
    <html lang=en>
    <head>
    
    etc…

    Then back in the parent template, I am retrieving the value of that session variable to call the relevant embed file (so evals to something like ‘test_inc/.default_body’.

    Thanks

  • #2 / Jan 12, 2011 5:16pm

    Lisa Wess

    20502 posts

    The code in the template will run when it is called in the template.  If you need pre-processing, you will need to come up with a custom solution for it.  Perhaps an add-on might help out.

    I’ll move this down to the CodeShare corner for more community discussion.

    Thanks!

  • #3 / Jan 12, 2011 5:45pm

    Russ Back

    142 posts

    So presumably in the code above, the top-level template gets parsed before the embedded templates? The session setting is done in the first embedded file so if embeds are parsed first, I would expect this session setting to be available in the parent template further down in the example.

    I’ve tested that by moving the setting code to the parent template and it now seems to work.

    Top-level template:

    {exp:deetective:set_template_type}
    
    {embed="global-embeds/header"
        body-id="index"
        robots="all"
        has-aside="n"
    }
    
        <h1>Home Page</h1>
    
        {embed="test_inc/.{exp:deetective:get_template_type}_body"}
    
    {embed="global-embeds/footer"} 

    Any thoughts on a better approach?

  • #4 / Nov 24, 2011 3:50pm

    amityweb

    162 posts

    Hi Russ

    Did you figure this out?

    I too am stuck… I an setting a $_SESSION variable depending on a form submission in a template that I need to access, but this $_SESSION assigment, being in an embedded template, is being run after the template I need to access the Session variables in. So they appear only after a second page load.

    I need to find a way to process session variabled before any template parsing.

    I’ve looked at plugins but they seem to only run when you call that plugin in a template.

    I wonder if there is anything else to add in to EE that can run before template parsing??

  • #5 / Nov 24, 2011 4:10pm

    amityweb

    162 posts

    The only way I can get around this is have my form to change session variables on its own separate page, so when a user makes a change and then leaves this page to go to another page, the new session variables are then available on these subsequent pages.

    Its not the best solution, I really wanted the form to change session variables within the template pages themselves.

    For info, its a form to change date. So there are choices of say 2010/2011, 2011/2012, 2012/2013. Then my system is showing various results of data depending on the year the user has chosen. I wanted them to change the date on the page the results are shown (the form is in an embed template as it needs to be used on many pages), but I cant figure out how to run assign session variables before the template is parsed, due to the embed running after the template.

     

  • #6 / Nov 24, 2011 5:17pm

    Russ Back

    142 posts

    Hey

    I didn’t get any further than the above to be honest - I had to set the session var in the top-level template as I couldn’t figure out a way to preprocess embedded templates.

    Sorry!

  • #7 / Nov 25, 2011 3:13am

    amityweb

    162 posts

    No worries. I can’t do that because I want the session on every page so it’s in my header template which is an embed so parsed later :(

  • #8 / Nov 28, 2011 3:14am

    Russ Back

    142 posts

    If you use a one-liner like I have above, adding it to the top of each template isn’t a huge pain, especially as its a call to a plugin so if you edit the plugin code, all templates will pick up that change.

  • #9 / Nov 28, 2011 5:22am

    amityweb

    162 posts

    I’ve got this thing about repeating the same code on pages, I hate it with a passion! Would be just my luck after I did it I need to edit it again.

    I have decided to abandon EE for this project now anyway. Yesterday I started to use Codeigniter with Grocery CRUD add-on and I have almost replicated the entire DB system in less than a day! I wish I did this from the start. I will keep EE for websites and v. simple databases if need be, but realise it does not cut it for a proper DB system.

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

ExpressionEngine News!

#eecms, #events, #releases