BoltCMS fetch content filters by taxonomies - php

trying to achieve needed behaviour with Bolt CMS content types, but can't, so asking for help
Have Content Type and 2 taxonomies, industries_t and services_t
{% set condition = condition|merge({'industries_t' : industriesFilter|join(' || ')}) %}
{% endif %}
{% if topicsFilter is not empty %}
{% set condition = condition|merge({'services_t' : topicsFilter|join(' || ')}) %}
{% endif %}
{% if app.request.query.get('from') is not empty or app.request.query.get('to') is not empty %}
{% set fromFilter = app.request.query.get('from') is not empty ? app.request.query.get('from') : '2014-01-01' %}
{% set toFilter = app.request.query.get('to') is not empty ? app.request.query.get('to') : 'now'|date('Y-m-d') %}
{% set condition = condition|merge({'publishedAt' : '\>' \~ fromFilter \~ ' && \<' \~ toFilter }) %}
{% endif %}
{% if app.request.query.get('search') is not empty %}
{% set condition = condition|merge({'title' : '%' \~ app.request.query.get('search') \~ '%' }) %}
{% endif %}
{% setcontent casestudies = 'case-studies' where condition orderby "-publishedAt" limit 6 %}
I need to fetch records, that has ´"industries_t" = "Test" OR services_t = "Another"´
right now, it works like "SELECT records where industries_t = "Test" and services_t = "Another"
is there any way to make it "SELECT records where industries_t = "Test" OR services_t = "Another"?

Related

How would you do ternary conditionals inside {%%} in Twig

I want to render parameters if they exist but can't find a way of correctly displaying it and keep getting
An opened parenthesis is not properly closed. Unexpected token "punctuation" of value ":" ("punctuation" expected with value ")")
where
{% setcontent records = 'properties' where
{filter:search_term,
((classification) ? ('classification':classification):(''))
} printquery %}
To use it inside Bolt CMS, first define your options and then pass that to Bolt CMS
{% set options = { filter: search_term , } %}
{% if classification is defined and classification|trim != '' %}
{% set options = options|merge({classification:classification,}) %}
{% endif %}
{% setcontent records = 'properties' where options printquery %}
After rereading your question you are probably looking for something like this,
{% set records %}
'properties' where {
filter : '{{ search_term }}',
classification: '{{ classification is defined ? classification : '' }}',
} printquery %}
{% endset %}
{{ records }}
However using the filter default here is more suited than using the ternary operator,
{% set records %}
'properties' where {
filter : '{{ search_term }}',
classification: '{{ classification|default('') }}',
} printquery %}
{% endset %}
{{ records }}
demo
To omit properties you would use the following,
{% set records %}
'properties' where {
filter : '{{ search_term }}',
{% if classification is defined and classification|trim != '' %}classification: '{{ classification }}',{% endif %}
} printquery %}
{% endset %}

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"?
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

Merge key and value into an array in Twig file

I want to add key and value into array in twig file. But I am facing following issue "Twig_Error_Syntax: A hash key must be a quoted string or a number"
{% set phoneCount = 0 %}
{% set phoneNumbers = {} %}
{% for currPhone in currBroker.phones %}
{% if (currPhone.type == 'Work' or currPhone.type == 'Mobile') and phoneCount <= 2 and currPhone.number !='' %}
{% set phoneCount = phoneCount + 1 %}
{% set phoneNumbers = phoneNumbers|merge({ currPhone.type:currPhone.type }) %}
{% endif %}
{% endfor %}
{{ phoneNumbers|print_r }}
I just need the syntax of merging key and value into array.
I tried by giving static inputs and its works
{% set phoneNumbers = phoneNumbers|merge({ 'work':'(011)112-1233' }) %}
But its not working for dynmic input. Please help!!
You have to wrap your key in braces :
{% set phoneNumbers = phoneNumbers|merge({ (currPhone.type) : currPhone.type }) %}
Tested and working example :
{% set currPhone = {type: 'test'} %}
{% set phoneNumbers = {} %}
{% set phoneNumbers = phoneNumbers|merge({ (currPhone.type) : currPhone.type }) %}
{% dump(phoneNumbers) %}
I get :
array:1 [▼
"test" => "test"
]

Multidimensional Array on TWIG loop

I'm trying to save some values into a multiarray on Twig, but for some reason, it's not working.
I want to save in an array a list of users and some values of them. I am using merge function to create the multi array.
{% for result in results %}
{% set users = users|merge({ ('name'~loop.index):result.name,('age'~loop.index):result.age,('credits'~loop.index):result.credits}) %}
{% endfor %}
Try this out :
{% set users = [] %}
{% for result in results %}
{% set users = users|merge([{ 'name' : result.name, 'age' : result.age, 'credits' : result.credits }]) %}
{% endfor %}

Loop till the value of variable is not zero

I have been stuck at this for a while now. I am new to twig and i am trying to iterate a code untill my variable becomes zero. I have tried this:
{% set total = 5%}
{% set i=1 %}
{% for total %}
{{i}}
{%set i=i+1%}
{% set total = total -1%}
{% endfor %}
and this
{% set i=1 %}
{% for total > 1%}
{{i}}
{%set i=i+1%}
{% set total = total - 1%}
{% endfor %}
but none are working.. What am i doing wrong?
Twig fors are more akin to PHP's foreachs (they are for iterating over a traversable). To achieve what you are describing you would do:
{% set nums = range(1, 5) %}
{% for num in nums|reverse %}
{{ num }}
{% endfor %}
In practice, you could set nums from your controller logic. Also note from the Twig manual:
Unlike in PHP, it's not possible to break or continue in a loop.
You can, however, skip elements with if. Manual example:
{% for user in users if user.active %}

Categories