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.

Embedded custom variables with conditionals

June 22, 2012 6:10am

Subscribe [2]
  • #1 / Jun 22, 2012 6:10am

    Dan G

    7 posts

    I can’t seem to get the expected functionality of embedded variables with conditional statements.

    As an example of the problem, I want to embed a custom variable called “tip_points” into another template.

    {embed="template" points="{tip_points}" }

    In my template I have the following code:

    points: {embed:points}
    
    {if embed:points > 1}
    points is more than 1
    {if:else}
    points is less than or equal 1
    {/if}
    
    {if embed:points != ""}
    points is not empty
    {if:else}
    points is empty
    {/if}


    Obviously, the result I’m expecting is:

    points: 2
    
    points is more than 1
    
    points is not empty


    But in fact the output I get is:

    points: 2
    
    points is less than or equal 1
    
    points is empty

    Any ideas what I can do to fix?

    Thanks,
    Dan

  • #2 / Jun 22, 2012 7:13am

    Dan G

    7 posts

    I needed to use the brackets around the embed:variable.

    if I want to check if a string variable is empty or not I need to encase it with quotes like this:

    {if "{embed:variable}" != ""}
    variable is not empty
    {if:else}
    variable is empty
    {/if}

    Because if it’s not encased in quotes (and the variable is empty), when it gets parsed, the engine will see this:

    {if != ""}
    variable is not empty
    {if:else}
    variable is empty
    {/if}

    Which would rightly case an error.

    If the quotes are used (and the variable is empty), this will result in:

    {if "" != ""}
    variable is not empty
    {if:else}
    variable is empty
    {/if}

    Which will be fine.

    I’ve sorted my headache out. hope this can help someone else.

    Dan

  • #3 / Jun 25, 2012 1:57pm

    Man With A Peg

    124 posts

    Glad you got it.

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

ExpressionEngine News!

#eecms, #events, #releases