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.

unexpected PHP errors with conditionals and empty safecracker field - parse order issue?

April 22, 2011 4:59am

Subscribe [3]
  • #1 / Apr 22, 2011 4:59am

    David Lawrence

    41 posts

    I’m getting a couple strange errors with what should be a pretty simple conditional statement that tests the contents of a matrix field to trap an error when one of the fields is empty.

    Here’s my template code:

    {feature_images}
        {if "{image_orient:label}" == "hidden"}
            {if:else}
            {feature_image}<!--Debug print the feature image URL-->
            {image_orient:label}<!--Debug print image_orient:label value-->
            {if feature_image != ""}
            <ul class="{if "{image_orient:label}" == "horizontal"}horizontal{if:else}vertical{/if}">
              <li><a href="http://{feature_image}" rel="lightbox" title="{exp:low_replace find=">" replace=""|<|>" multiple="yes"}{image_description}{/exp:low_replace}">{exp:imgsizer:size src="{feature_image}" width="{image_orient}" alt="{title}" title="{title}"}</a></li>
              <li>{image_description}</li></ul>
                {if:else}
                <ul> 
                <li>{image_description}</li>
                </ul>
            {/if}
        {/if}
    {/feature_images}

    If {image_orient:label} is set to “hidden”, I want to bypass everything

    If If {image_orient:label} is not “hidden”, I want to check if the {feature_image} field is empty and if so, bypass the image processing statements.


    For some reason, I get imgsizer PHP errors, even when the test for “hidden” is true. Shouldn’t the section with the imgsizer tags be bypassed if the first condition is met?

    Is this because the plugins are getting parsed before the conditional test?

     

    Also, when I set the {image_orient:label} to a value that’s not “hidden”, in the debug output I see a period being output for {feature_image}.

    Shouldn’t an empty Safecracker file field output nothing?

    This should be simple but I’m perplexed. Any help much appreciated!

    David

  • #2 / Apr 22, 2011 1:25pm

    Brandon Jones

    5500 posts

    Hi David,

    Yes, advanced conditionals (if:else) are parsed later, and that’s likely the issue here. You could try using only simple conditionals which are parsed earlier, or sticking the imgsizer tags in an embedded template instead (you may need to experiment a bit). Does that help?

  • #3 / Apr 22, 2011 1:43pm

    David Lawrence

    41 posts

    Hi Brandon,

    thanks, yes I think so.

    So if I understand correctly, I should try re-writing this section to eliminate {if:else} statements, or create a small template that gets embedded for the imgsizer part of the conditional. Is that correct? A special template for one line of code seems a bit like overkill but I’ll do whatever works!

  • #4 / Apr 22, 2011 5:25pm

    David Lawrence

    41 posts

    Using an embed took care of the PHP error inside the conditional statement, thanks!

    I just have one more issue:

    The embedded template is not outputting field content, it’s just printing the literal field names


    Here’s my simplified conditional statement:

    {feature_images}
        {if "{image_orient:label}" == "hidden"}
            {if:else}
            {feature_image}<!--Debug print the feature image URL-->
            {image_orient:label}<!--Debug print image_orient:label value-->
            {if feature_image != ""}
            {embed="{my_embedded_template_group}/features_display_image"}
            {if:else}
                <ul> 
                <li>{image_description}</li>
                </ul>
            {/if}
        {/if}
    {/feature_images}

     

    Here’s the embedded template called features_display_image:

    <ul class="{if "{image_orient:label}" == "horizontal"}horizontal{if:else}vertical{/if}">
              <li><a href="http://{feature_image}" rel="lightbox" title="{exp:low_replace find=">" replace=""|<|>" multiple="yes"}{image_description}{/exp:low_replace}">{exp:imgsizer:size src="{feature_image}" width="{image_orient}" alt="{title}" title="{title}"}</a></li>
              <li>{image_description}</li></ul>


    Here’s the generated source:

    <a href="http://www.stretcher.org.php5-18.dfw1-1.websitetestlink.com/images/uploads/features/nb_km2.jpg&lt--Debug">http://www.stretcher.org.php5-18.dfw1-1.websitetestlink.com/images/uploads/features/nb_km2.jpg<!--Debug</a> print the feature image URL-->
            horizontal<!--Debug print image_orient:label value-->
                    <ul class="vertical">
              <li><a href="http://{feature_image}" rel="lightbox" title="{image_description}"></a></li>
              <li>{image_description}</li></ul>

     

    So it looks like none of the field data is being pulled into the embed.  How do I tell the embed to use the field data instead of printing the field names? Am I using quotes wrong? Is there a different syntax or tag I need to include in the embed template?

    This is my first time using embeds and I figure I’m forgetting something simple.

    Thanks!

  • #5 / Apr 22, 2011 9:31pm

    David Lawrence

    41 posts

    just forgot to wrap in the channel and matrix field tags, that helped, still need a few more tweaks.

  • #6 / Apr 23, 2011 3:47pm

    Greg Salt

    3988 posts

    Hi David,

    Where are you with this now? Do you still need assistance?

    Cheers

    Greg

  • #7 / Apr 23, 2011 4:23pm

    David Lawrence

    41 posts

    Hi Greg,

    Yes, I have it nailed. Took quite a bit of experimenting because the embed uses the the same matrix field as the conditional statement. Turned out I had to pass a row variable and tweak the limit values to keep the loops under control. Here’s the final code:

    main template

    {feature_images search:image_orient="not 400"}<!--Skip rows set to "hidden"-->
                {if feature_image != ""}
            {embed="{my_embedded_template_group}/features_display_image" row="{row_id}"}
                {/if}
                {if feature_image == ""}
                <ul> 
                <li>{image_description}</li>
                </ul>
                {/if} 
    {/feature_images

    embedded template

    {exp:channel:entries channel="features"}
    {feature_images row_id="{embed:row}"}
    <ul class="{if "{image_orient:label}" == "horizontal"}horizontal{/if}{if "{image_orient:label}" == "verticle"}verticle{/if}">
              <li><a href="http://{feature_image}" rel="lightbox" title="{exp:low_replace find=">" replace=""|<|>"  multiple="yes"}{exp:tagstripper:tagsToStrip tags='p'}{image_description}{/exp:tagstripper:tagsToStrip}{/exp:low_replace}">{exp:imgsizer:size src="{feature_image}" width="{image_orient}" alt="{title}" title="{title}"}</a></li>
              <li>{image_description}</li></ul>
    {/feature_images}          
    {/exp:channel:entries}


    Thanks for your help!

    best,
    David

  • #8 / Apr 23, 2011 4:32pm

    Greg Salt

    3988 posts

    Hi David,

    Excellent. Please post again if you have further questions or problems.

    Cheers

    Greg

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

ExpressionEngine News!

#eecms, #events, #releases