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.

Conditional Custom Field?

February 05, 2011 6:27pm

Subscribe [3]
  • #1 / Feb 05, 2011 6:27pm

    heavymark

    66 posts

    I read the document on conditional global variables which is great. But I’d like to do the same with a custom field.

    Would this be the proper format?

    {if {link_rel} != ""}rel="{link_rel}"{/if}

    In which I’m saying if the custom field “link_rel” contains no value, do not show anything between that if statement, but if “link_rel” does have a value, show the contents between the if tags. Or is their a different more efficiently way of doing this in EE2. Thanks!

  • #2 / Feb 05, 2011 6:41pm

    Focus Lab Dev Team

    1129 posts

    In which I’m saying if the custom field “link_rel” contains no value, do not show anything between that if statement, but if “link_rel” does have a value, show the contents between the if tags. Or is their a different more efficiently way of doing this in EE2. Thanks!

    In my experience it’s most reliable to use quotes around the custom field in the conditional. However, I imagine there may be some type of conflict if the custom field value has the same quote (single or double) in it.

    So, something like this is what we use:

    {if "{link_rel}" != ""}rel="{link_rel}"{/if}

    I’m interested if there is an advised method though.

  • #3 / Feb 05, 2011 6:44pm

    heavymark

    66 posts

    Great, thanks, I’ll add the quotes. Now my syntax highlighting looks more proper.

    Luckily the value of this field would not ever contain quotes, but I will keep that in mind for future uses.

  • #4 / Feb 05, 2011 8:22pm

    Lisa Wess

    20502 posts

    Quoting and bracing conditionals can create security issues, especially when the data can come from user input, such as segments.  See point 5 in troubleshooting conditionals

    A variable having to be quoted can be a sign that the code generating the variable is not being run through prep_conditionals, or that the conditional isn’t written correctly as in the original post. =)  See conditional global variables for proper syntax.

    heavymark - the correct and safe way to write your conditional is:

    {if link_rel != ""}rel="{link_rel}"{/if}

    Hope that helps!

  • #5 / Feb 06, 2011 10:13am

    heavymark

    66 posts

    Thanks Lisa!

  • #6 / Feb 07, 2011 8:27am

    Sue Crocker

    26054 posts

    Glad Lisa was able to help. Don’t hesitate to post again as needed.

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

ExpressionEngine News!

#eecms, #events, #releases