-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.

closed as off-topic by Clive Aug 29 '17 at 8:59

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on programming, PHP, SQL, etc. that do not relate directly to Drupal are off-topic here, but can be asked on Stack Overflow." – Clive
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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 Aug 29 '17 at 8:59
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.