Gravatar for nicolas.a.slavik@accenture.com

Question by yonico, Jan 26, 2015 12:08 PM

check that images exist in sublayout

We are trying to check if an image already exist. {{=image(raw['<%=ToCoveoFieldName("productimageurl", false)%>'], {alt : ""}) }} {{=image(raw['<%=ToCoveoFieldName("thumbnailimageurl", false)%>'], {alt : ""}) }}

if those fields doesnt exists, we should apply another logic with if statements. Can you provide an example?

1 Reply
Gravatar for pastjean@coveo.com

Answer by pastjean, Jan 26, 2015 12:58 PM

Since i don't know what your backend is here is the basic way to do it:

the variable imagefield needs to represent the content of the field

<% if(imagefield.length) !== 0 {%>
  ... first case template
<% }else{ %>
  ... no image template
<% } %>

as @lbergeron stated, in the context of asp.net

{{ if(imagefield.length) !== 0 { }}
  ... first case template
{{ }else{ }}
  ... no image template
{{ } }}
Gravatar for lbergeron@coveo.com

Comment by Luc Bergeron, Jan 26, 2015 1:19 PM

In addition to @pastjean answer, I would strongly recommend to use the curly braces syntax with underscore templates to avoid conflicts with ASP.NET syntax.

{{ if(imagefield.length) !== 0 { }}
  ... first case template
{{ }else{ }}
  ... no image template
{{ } }}
Ask a question