-1

Original Code:

<div class="our-story search-query">
  <div class="container">
  <h1>Search Results</h1>
  {% if keysearch %} 
  <p>{{ view.total_rows }} results found for '<span class="font-500">{{ keysearch }}</span>'</p>  
  {% endif %}
  </div>
  </div>

I have this code in a twig file. I need to put in a condition:
1. If search results are ZERO, then it should say "No results found for 'YYY' "
2. If results are found i.e above Zero, then it should say " XX results found for 'YYY' "

Code with If condition:

{% if keysearch %} 
  {% set variable = {{ view.total_rows }} %}
  {% if variable > 0 %}  
  <p>{{ view.total_rows }} results found for '<span class="font-500">{{ keysearch }}</span>'</p>
else {
 <p>No results found for '<span class="font-500">{{ keysearch }}</span>'</p>
}  
  {% endif %}

When I put in a condition under `{% if keysearch %} it throws an error:

Twig_Error_Syntax: A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value "{" in "themes/custom/iifl/templates/views/views-view--search-page.html.twig" at line 59. in Twig_ExpressionParser->parseHashExpression() (line 281 of vendor\twig\twig\lib\Twig\ExpressionParser.php).

Can someone help me out.

1
  • This is a Twig syntax issue rather than anything to do with Drupal; {% set variable = {{ view.total_rows }} %} needs to be {% set variable = view.total_rows %}
    – Clive
    Commented Aug 29, 2017 at 8:59

1 Answer 1

0
    {% if keysearch %} 
  {% set variable = {{ view.total_rows }} %}
  {% if variable > 0 %}  
  <p>{{ view.total_rows }} results found for '<span class="font-500">{{ keysearch }}</span>'</p>
{% else %}
 <p>No results found for '<span class="font-500">{{ keysearch }}</span>'</p>
  {% endif %}

Do not know if this is the case , but your else condition is not twig like.

Not the answer you're looking for? Browse other questions tagged or ask your own question.