87630

Twig check multiple values

I have about 5 or so tables that are booleans. I want to test all of them and if one or more return true then to do something.

So far I have tried something like

{% if product.is_red == true %} <h1>Has colors</h1> {% elseif product.is_yellow == true %} <h1>Has colors</h1> {% elseif product.is_green == true %} <h1>Has colors</h1> {% elseif product.is_purple == true %} <h1>Has colors</h1> {% elseif product.is_black == true %} {% endif %}

But if anyone of them returns true then it will say

Has Colors

whatever the amount of times it returns true. Is there any way to check all of them and if one more returns true then returns "Has colors"?

Answer1:

You have to work with a flag in twig to keep track if one or more colors are specified. A shorter example of the code would be (should also work with an object product):

{% set product = { 'is_red' : false, 'is_yellow' : false, 'is_blue' : true, 'is_green' : false, } %} {% set has_color = false %} {% for color in ['red', 'yellow', 'blue', 'green', 'purple', ] %} {% if product['is_'~color] is defined and product['is_'~color] %}{% set has_color = true %}{% endif %} {% endfor %} {% if has_color %} <h1>Has color</h1> {% endif %}

fiddle

Answer2:

Digging deeper in Twig, I would do like this...

{% set has_color = false %} {% for color in product if color is true %} {% set has_color = true %} {% endfor %} {% if has_color %} <h1>Has color</h1> {% endif %}

Recommend

  • Message part MyClass was not recognized. (Does it exist in service WSDL?)
  • Create a unique primary key (hash) from database columns
  • What is the difference between a CPU and a GPU oriented code?
  • time_t conversion format question
  • dynamically set table titles with jquery's Datatables plugin - get titles from data object
  • Read Values from .csv file and convert them to float arrays
  • JavaFX icons randomly disappear from TreeTableView when scrolling, performance slow as well
  • Build entire solution for .NET 4 and .NET 4.5 and copy files to specific folders
  • jquery limit lines in a paragraph and apply three periods to the end
  • Linq to SQL left outer join using Lambda syntax and joining on 2 columns (composite join key)
  • DirectX game with no prerequisite software to run
  • NHibernate Lazy Loading Behaviour
  • Comma within fields in CSV file -import to DB using SSIS
  • Net-ssh session timeout
  • ASP.NET - Efficiently delay code execution for single request?
  • CHAR vs VARCHAR for password security
  • How can I sum two different columns at once where one contains Decimal objects in pandas?
  • How to develop MS CRM kind of application
  • Flot returns incorrect x value (mm/dd/yy - date) after zoom for stack bar chart
  • 'Enemy' was not declared in this scope?
  • How do I create an image and save it for later to draw as texture in XNA?
  • Paypal 'Buy Now' button not letting me dynamically set the price
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • Time out Error in send mail
  • PayPal API Listener Website Payments Standard URI
  • redirect_to root_url and return unless @user.activated
  • ApplePay PKPaymentAuthorizationViewController always shows processing
  • Atlas images wrong size on iPad iOS 9
  • How to avoid particles glitching together in an elastic particle collision simulator?
  • Recording logins for password protected directories
  • Splitting given String into two variables - php
  • NetLogo BehaviorSpace - Measure runs using reporters
  • angularjs unit test when to use $rootScope.$new()
  • Check if a string to interpolate provides expected placeholders
  • Fill an image in a square container while keeping aspect ratio
  • ActionScript 2 vs ActionScript 3 performance
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Reading document lines to the user (python)
  • java string with new operator and a literal