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.

Useful custom variables please!

August 10, 2011 11:26am

Subscribe [3]
  • #1 / Aug 10, 2011 11:26am

    RedLeader

    33 posts

    I’m having trouble with the code below.  I’ve simplified it to illustrate the point.

    Basically, I want to check the URL in case it’s carry variables I need to act upon.  For example:

    1. website.com/template_group/template/client/client_name

    Here I detect that there is a name/value pair: client = client_name.  So now I know what the client’s name is I can bring back that client’s data.

    2. website.com/template_group/template/order/title/client/client_name

    Here there is another variable pair in the URL which I also want to detect and act upon.  But notice the client name variable pair is in different segments of the URL.


    So, I want an IF statement that checks the different segments and sets a known variable.  See a basic example here:

    {if segment_3=="client"}
         {preload_replace:client_url_title="{segment_4}"}
    {/if}
                
    {if segment_5=="client"}
         {preload_replace:client_url_title="{segment_6}"}
    {/if}            
                
    {embed="includes/client_work_list" client_url_title="{client_url_title}"}

    Notice I am trying to store the client name in to some kind of container (here I use the preload_replace variable “client_url_title”) in order to reuse that container in the embed code at the end.  But this doesn’t work!  Because the second preload_replace call OVERWRITES the first one regardless of whether the second IF statement fired.

    This seems to be the case for MX_Jumper plugin too (which offers a way to reuse “variables” in a template).

    Is there any way to do this?  It seems like a pretty obvious thing to do so I think I’m missing a trick here!

  • #2 / Aug 10, 2011 12:00pm

    RedLeader

    33 posts

    I made a slight error above.  I should have said:

    preload_replace uses the FIRST instance.  Once it’s set, it doesn’t matter if I try and set it again, it ignores it and only uses the first one REGARDLESS of the IF statement.

    I tried this with MX_Jumper but MX_Jumper is the one that sees both and sets them both (again regardless of the IF).  So you only ever get the value set in the last variable.

    Both ways ignore the IF statements.  It’s like they don’t exist.  Why can’t I set a variable with an IF to be used further down int he template?  Should I just use PHP??

  • #3 / Aug 10, 2011 12:35pm

    glenndavisgroup

    436 posts

    RedLeader,

    According to the EE docs here:

    http://ellislab.com/expressionengine/user-guide/templates/globals/preload_replacement.html

    “Assignment and replacement occurs instantly when the template is loaded, before any tags are parsed, and therefore may not be affected by the result of another tag’s output.”

    This explains why you are having this issue. It’s best you do this with PHP code. I hope that helps.

    Mike

  • #4 / Aug 10, 2011 12:36pm

    RedLeader

    33 posts

    Yeah, I read that and that explains the issue but I just wanted to illustrate what I was trying to do and the lack of options that EE provides me.  I was surprised because it seems like an obvious thing for EE to have.

    I guess I’ll use PHP then.

  • #5 / Aug 10, 2011 6:55pm

    Sue Crocker

    26054 posts

    Thanks for the assist, Mike.
    RedLeader, looks like you have a solution of sorts then. If anything else comes up, please do let us know in a new thread..

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

ExpressionEngine News!

#eecms, #events, #releases