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.

Embed Variable Behavior

September 12, 2007 7:20pm

Subscribe [3]
  • #1 / Sep 12, 2007 7:20pm

    IMDadmin

    35 posts

    A couple questions:

    First, when you pass in variables through an embed such as

    {embed="test/page" title="Foo"}

    normally on test/page you can access the title by using {embed:title}. If you don’t say title=“Foo” in the embed call, and don’t give title any value at all, what is the value of {embed:title} on the test/page page? Is it empty, just a “” value, or does EE just render it out as the actual string {embed:title}? Right now, it seems like it returns its actual string of {embed:title}.

    Second and related question. I’m trying to set up an embed so that when users don’t pass in values for some variables, defaults are automatically set. For example, if they don’t say var1=“me” in the embed call, I can give var1 a default value instead. I’ve been trying to do this first with EE if statements, and then with a PHP-EE hybrid solution I found at http://ellislab.com/forums/viewthread/54024/.

    Passed in: {embed:sc_server}
    
    <?php
    if ( '{embed:sc_server}' == "{embed:sc_server}")
    { ?>
       yes {assign_variable:mg_server="EXPRESSION"}
    <?php } else { ?>
       no {assign_variable:mg_server2="{embed:sc_server}"}
    <?php } ?>
    
    {mg_server}
    {mg_server2}

    Basically, trying to get PHP to handle the flow control because EE couldn’t seem to line it up right. If {embed:sc_server} returns its own variable name, assume the user didn’t pass anything in through the embed and give it its default value. Otherwise, give the var what it passed through the embed.

    Now here’s the odd part. At first I had both assign_variable statements set to use the {mg_server} variable since only 1 should’ve been executed. And I’d get output indicating that only 1 if statement had been found to be true (via seeing the ‘no’ or ‘yes’). Yet mg_server always equalled the string {embed:sc_server}. I changed it to the above and found that even though I always still see only ‘no’, mg_server always equals EXPRESSION and mg_server2 always equals {embed:sc_server}. It’s like both assignments are still getting run somehow. The exact output that appears in the page source is:

    Passed in: {embed:sc_server}
    
       yes 
    
    EXPRESSION
    {embed:sc_server}

    Is this expected behavior or am I doing something wrong? Is there an easier way to do this?

  • #2 / Sep 12, 2007 9:11pm

    Sue Crocker

    26054 posts

    I requested some help on this from the rest of the team. I’ll let you know what happens.

  • #3 / Sep 13, 2007 11:00am

    Robin Sowell

    13255 posts

    I think you’re getting jammed up due to parse order.  If it’s parsed on input, the first conditional will always be true.  If it’s parsed on output, it may be the case that the variable assignment happens before the php conditional kicks in.

    Could you pass a ‘’ as default and use that as your variable?  And avoid the assign variable entirely?

  • #4 / Sep 13, 2007 11:34am

    IMDadmin

    35 posts

    Basically what I’ve got is a list of about 6 variables that need to be set. 90% of the time though, 4 of the 6 can use default values. The user only has to worry about 2 of them. From a user-friendliness standpoint, I’m trying to make it so that they don’t always have to pass in all 6 variables through the embed, even with blank values. Optimally they’d only have to think about those 4 rarely-set ones when they actually want to set them. I.e,

    {embed="test/page" server1="foo" server2="foo2"}

    as oppposed to

    {embed="test/page" server1="foo" server2="foo2" server3="" server4="" server5="" server6=""}

    Does EE have a solid way to take a list of embed variables, check for ones which don’t exist/aren’t set and set a default value for those, and preserve the values of those which do exist/have a value without having to define the variables in every embed call?

  • #5 / Sep 13, 2007 11:44am

    Robin Sowell

    13255 posts

    No, though we could move to ‘How to’ and poke the code a bit.  I’d imagine it would be doable.  I’m just not sure it would work.  Try this- set up a two templates, both of wich embed the same template- x.  On x, try:

    Passed in: {embed:sc_server}
    
    <?php
    if ( '{embed:sc_server}' == "yes")
    { ?>
       yes {assign_variable:mg_server="EXPRESSION"}
    <?php } else { ?>
       no {assign_variable:mg_server2="{embed:sc_server}"}
    <?php } ?>
    
    {mg_server}
    {mg_server2}

    In one template, pass sc_server as ‘yes’, in the other, pass it as ‘no’.  Is it working if the variables are defined?

  • #6 / Sep 13, 2007 1:45pm

    IMDadmin

    35 posts

    It works like expected if I pass in a value, even if it’s blank. But still, oddness. Here’s what happens if I pass in “” for sc_server, using the following code to evaluate it:

    Passed in: {embed:sc_server}
    
    <?php
    if ( '{embed:sc_server}' == "")
    { ?>
       yes {assign_variable:mg_server="EXPRESSION"}
    <?php } else { ?>
       no {assign_variable:mg_server2="{embed:sc_server}"}
    <?php } ?>
    
    {mg_server}
    {mg_server2}

    If I pass in “”, here’s the output:

    Passed in: 
    
       yes 
    
    EXPRESSION

    That’s pretty much correct. If I change it so that both assignments are done to mg_server and drop the mg_server2 variable, it also works, which is the final result I want (mgserver2 is just a test variable I made once I realized both assignments were getting run no matter what).

    However, if I pass in an actual value, such as IPS3, here’s the output:

    Passed in: IPS3
    
       no 
    
    EXPRESSION
    IPS3

    So basically, if I pass in an empty string it evaluates as it should. If I pass in a value, it is somehow still setting mg_server even though according to the output that assignment value shouldn’t be evaluated.

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

ExpressionEngine News!

#eecms, #events, #releases