29
votes
Accepted
What is the best way to exclude an array of entries?
Craft documentation describes how to exclude entries with a given value. In this case, we want a string that looks like: 'and, not id1, not id2, not id3, not id4'
So we need a string that is all of ...
17
votes
Accepted
Creating dynamic array and set keys
You can do this using the twig attribute function:
{% set data = [
{'column':'1','title':'Quick Facts'},
{'column':'2','title':'Inspiration'},
{'column':'1','title':'Agencies'},
{'...
12
votes
Accepted
Overwrite value in object in array
Solution
This won't work in Twig as presented in the question (see why below). Instead, users would need to be changed to an associative array (or "hash" in Twig terminology) with keys that aren't ...
9
votes
Accepted
Using Twig to merge an array and remove duplicates
You could do it in the twig template
But a template isn't really the best place for something that is this codey, it gets pretty unreadable pretty quick.
Notes:
I start by building arrays of models ...
8
votes
Is there a way to check if a value exists in an array in twig?
Like you said, to test if a value exists in a Twig array you'd do:
{% if myValue in ['apples', 'oranges'] %}
{# do something... #}
{% endif %}
To check whether the value is an array or object ...
8
votes
Accepted
Alphabetical index for a list of entries
First, sort all entries by multiple criteria:
{# get our entries and order by firstname, lastname #}
{% set allEntries = craft.entries.section('entries').orderBy('lastname asc, firstname asc').all() %...
7
votes
Accepted
Adding entries to an array, one at a time
The merge filter only works with 2 arrays (or 2 hashes), not with an array and an object. I think that the event you are trying to add is not an array, it is a single event (Entry Model). You might ...
6
votes
Using Twig to merge an array and remove duplicates
The accepted answer here should hopefully help you out.
Basically:
{% set newArray = [] %}
{% for name in array %}
{% if name not in newArray %}
My name is {{name}}
{% set newArray = ...
6
votes
Accepted
Shouldn't this merge be working?
Try adding find() to your ElementCriteriaModel, I think without that function you're not actually saving your entries as an array in your variable todaysOneOffEvents yet.
Here's what I found in the ...
6
votes
Accepted
Conditional to detect mobile devices with getUserAgent?
Unlike in PHP, it's not actually possible to break a for loop in Twig.
To work around that fact, you can use the for loop to set a flag, and then put the conditional outside the loop:
{% set ...
6
votes
Accepted
Impossible to invoke a method ("limit") on an array... but it's not an array?
I'm not calling .all(), so this is still an object, right?
Nope. When you execute the for loop, it will explicitly run the database query and return the array of results. You're probably getting a ...
5
votes
Accepted
Complicated A-Z problem
I base my example on the assumption that all people are Users (Element type User). If users are entries, you'd have to replace users by entries ;)
I don't think there's a way to do this without ...
5
votes
Accepted
List Categories by 0-9 and A-Z
To group by the first letter you would simply use the slice filter within Craft's group
filter.
For all non letter characters you can now use Craft's replace filter (it does regex since Craft 2.2)
...
5
votes
Accepted
Entries & Twitter Posts
I finally got this working, here is how, I'm sure there is a better way or ways I can improve, but for now it works.
First, we need to create an empty array like so...
{% set posts = [] %}
Then we ...
5
votes
Accepted
How to build a sentence based on tags
I think it'll be easier and more efficient to achieve that if you use the loop functionality instead of querying the actual tags. I think this code does what you want:
{% set topics = '' %}
{% for ...
5
votes
Accepted
Pagination and Entry Model on Homepage
project.images is an array because you are eager-loading the images.
Since you’re only loading one project entry, you’re not actually gaining anything by eager-loading the images (or category), ...
4
votes
Accepted
How do I find live and pending entries when using an array?
You can get all entries regardless of status with .status(null):
{% set allEntries = craft.entries.section('sectionName').relatedTo(category).status(null).find() %}
4
votes
Accepted
Pagination / Custom Routes (Based on Entry Existing)
For generating the nav, this should work.
{% set allEntriesByWeek = allEntries|group("postDate|date('Y-W')") %}
{% for week, entry in allEntriesByWeek %}
<li><a href="events/{{ week|...
4
votes
Accepted
Adding items to an array
You can accomplish this with slice and merge, although it's not very pretty:
{% set myArray = ['a', 'b', 'd' ] %}
{% set myArray = myArray|slice(0,2)|merge(['c'])|merge(myArray|slice(2,myArray|length)...
4
votes
Accepted
Apply 'random' to an array
Take a look at the shuffle function provided by Craft: https://craftcms.com/docs/templating/functions#shuffle
This will shuffle the complete array and output them all in the new order:
{% set img = [...
4
votes
Accepted
Random Number from range
Building off of Brad's answer, you might consider using a for loop to render your markup, instead of setting all of those variables:
{% set range = range(10, 90) %}
{% set totalDivs = 5 %}
<div ...
4
votes
Returning an array to Twig from a plugin
Your plugin should be able to handle PHP arrays just fine.
craft/plugins/myplugin/variables/MyPluginVariable.php
function getArray()
{
return array('hi', 'there');
}
craft/templates/template....
4
votes
Overwrite value in object in array
This could also be accomplished with the map filter, if you’re running Craft 3.1.29 or later:
{% set users = [
{name: 'Bill', age: 25},
{name: 'Tim', age: 30},
{name: 'John', age: 32}
] %}
...
4
votes
Accepted
How can I iterate through an array of field handles for an entry?
I figured it out! I ditched the macro and just included the following in my template:
{% for field in courseFields %}
<section class="col-xl mb-3">
<h2 id="{{ field.handle }}">{{ ...
4
votes
Accepted
Updating Array Variable In Extending Template
You can only access the parent's variable, not modify it. You could however simply add another block specifically for the body class, like so:
Layout template:
<body class="{% block body_class %}...
4
votes
How can I tell if a variable is an array or an entry/element query?
you can use the instance of twig test here.
You want to flip your logic, so test for Query object first else array stuff but this should work I think.
To get the specific class just {% dd props....
4
votes
Checking if a value is in an array of IDS - using a filter?
You could use filter to get only the variant that matches the wheelSpacer variable. This will return an array, so you can use first to get the first matching variant (since presumably product.variants ...
3
votes
Accepted
Filter out duplicate entries in array?
Holy moly! I've solved it already, Batman. Check the array before adding the event to it, like so:
{% if event not in categoryEvents %}
{% set categoryEvents = categoryEvents | merge([event]) ...
3
votes
How do I find live and pending entries when using an array?
Try to set both states in single/double quotes:
{% set allEntries = craft.entries.status('live', 'pending').section('sectionName').relatedTo(category).find() %}
3
votes
What is the best way to exclude an array of entries?
I can think of two ways to accomplish this:
The without filter:
Fetch all entries and all featured entries. Then remove the latter from the first:
{% set all = craft.entries.section('news'...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
array × 96templating × 35
entries × 11
merge × 11
categories × 7
plugin-development × 5
matrix × 5
filter × 5
variable × 5
sort × 5
craft3 × 4
php × 4
elementcriteriamodel × 4
pagination × 4
plugin-craftcommerce × 3
field × 3
datetime × 3
plugin-supertable × 3
sorting × 3
group × 3
plugin-supersort × 3
string × 3
multisort × 3
users × 2
entry × 2