Conditional test returns "Failed to evaluate expression null:null"

Options

Hello,

I'm writing a display template for a News content type. The News content type has a photo_1 field, which is a drop-down menu that allows the user to pick an item of the Image content type to go with the article. The field photo_1 is optional. Additionally, the Image content type has been modified to include a field called "credit", which is also optional (it's the name of the photographer).



In my display template for News, I can successfully test whether the optional photo_1 has been set by using:



    <t:if test="photo_1.length > 0">photo_1 has been set.</t:if>



However, I'd like to reference the "credit" field of photo_1 in my News template. I figured that, since I can use the dot notation above to reference the length of photo_1, then I'd be able to use the same notation to reference the "credit" field of photo_1 The following tests to see if "credit" was filled in all fail:



     <t:if test="photo_1.credit.length > 0">the credit for photo_1 has been set.</t:if>

     <t:if test="photo_1.credit != null">the credit for photo_1 has been set.</t:if>

     <t:if test="!isNull(photo_1.credit)">the credit for photo_1 has been set.</t:if>

Each of them renders the following message in the browser: "Failed to evaluate expression null:null"

Please note that I'm running the tests above INSIDE the <t:if> block that tests "photo_1.length > 0", so that I know I have a valid photo_1 before I reference it's "credit" property.

Does anyone know how I can correctly check for a valid "credit" for the photo_1 Image item that's part of my News content type definition?

Thanks in advance!

Tagged:

Comments

  • Yep! Whenever you have an object or item of a second content type linked to a primary CT, you need to use the t:list id parameter, like so:

    <t:list id="photo_1"><t:if test="!isNull(credit)">the credit for photo_1 has been set.</t:if></t:list>

    Or:

    <t:list id="photo_1"><t:if  test="credit.length > 0">the credit for photo_1 has been set.</t:if></t:list>

    All variables inside the t:list block refer to the photo_1 item and not the main item. Another way to do this would be to create a display template for your image CT that would handle this, and use a standard CMS list to insert it with the correct DT.

  • Silvio,

    If credit field is just a text fields testing for length will always fail. Length test only applies to category-based fields or fields that link to another item of a specific content type in your site.

    The syntax corrections Jim made are accurate. Here's a code that you might find helpful.This tests if photo_1 field is not null & then prints out the div around the image, sets the width of that div to the width of the image & then prints the credit field if it is not null.

    <t:if test="photo_1.length > 0">

      <t:list id="photo_1"><div class="my-photo-box" style="${imagewidth}"><img src="${url}" alt="${alt_txt}" />

           <t:if test="!isNull(credit)">${credit}</t:if>

    </div></t:list>

    </t:if>

  • Elizabeth M:

    Silvio,

    If credit field is just a text fields testing for length will always fail. Length test only applies to category-based fields or fields that link to another item of a specific content type in your site.

    The syntax corrections Jim made are accurate. Here's a code that you might find helpful.This tests if photo_1 field is not null & then prints out the div around the image, sets the width of that div to the width of the image & then prints the credit field if it is not null.

    <t:if test="photo_1.length > 0">

      <t:list id="photo_1"><div class="my-photo-box" style="${imagewidth}"><img src="${url}" alt="${alt_txt}" />

           <t:if test="!isNull(credit)">${credit}</t:if>

    </div></t:list>

    </t:if>

    Thank you everyone for your help. I followed your recommendations and now everything is working fine!

  • Elizabeth M:

    Silvio,

    If credit field is just a text fields testing for length will always fail. Length test only applies to category-based fields or fields that link to another item of a specific content type in your site.

    The syntax corrections Jim made are accurate. Here's a code that you might find helpful.This tests if photo_1 field is not null & then prints out the div around the image, sets the width of that div to the width of the image & then prints the credit field if it is not null.

    <t:if test="photo_1.length > 0">

      <t:list id="photo_1"><div class="my-photo-box" style="${imagewidth}"><img src="${url}" alt="${alt_txt}" />

           <t:if test="!isNull(credit)">${credit}</t:if>

    </div></t:list>

    </t:if>

    How is that imagewidth variable nowhere in the documentation?? *runs off to update half a dozen display templates that have been broken for a year*

  • Jim Drey:

    How is that imagewidth variable nowhere in the documentation?? *runs off to update half a dozen display templates that have been broken for a year*

    Jim,

    The imagewidth & imageheight variables are recent additions to the product, you can find the updated documentation of templating language here.

Categories