Error in simple if-conditional

Hi, my site’s search fails silently when I modify a searchbox’s liquid templating to the following. Could someone help me figure out what is wrong in the way I’ve written this conditional?

{% if post.description %}
  {{post.description}}
{% elsif post.content.size < 180 %}
  {{post.content | strip_html}}
{% else %}
  {{post.content | strip_html | truncate: 180}}
{% endif %}

For reference, this is the original code which worked fine (unless the post length was less than 180 chars which is what I’m trying to fix above

{% if post.description %}
  {{post.description}}
{% else %}
  {{post.content | strip_html | truncate: 180}}
{% endif %}

Any help/pointers appreciated! Thank you :slight_smile:

I don’t see anything wrong with the original conditional (or the new one, come to that). What output are you getting?

What search utility are you using … and how do you know it is “failing silently?” Maybe try setting your Liquid error mode in your config file to strict?

1 Like

What’s the issue with using the original code? As it looks like text less than 180 chars would still get truncated but that have no effect and so look unchanged.

1 Like

Have you considered a bad flow in the new code where you check if it less than 180 chars using untouched value, but apply truncation on text with html stripped out?

Consider alternative below where check and apply are done on the same stripped value

{% if post.description %}
  {{ post.description }}
{% else %}
  {% assign p_content = post.content | strip_html %}
  {% if p_content.size < 180 %}
    {{ p_content }}
  {% else %}
    {{ p_content | truncate: 180 }}
  {% endif %}
{% endif %}
1 Like