Hot answers tagged

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 ...
user avatar
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'}, {'...
user avatar
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 ...
user avatar
  • 1,907
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 ...
user avatar
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 ...
user avatar
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() %...
user avatar
  • 589
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 ...
user avatar
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 = ...
user avatar
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 ...
user avatar
  • 35.8k
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 ...
user avatar
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 ...
user avatar
  • 66.4k
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 ...
user avatar
  • 6,288
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) ...
user avatar
  • 35.8k
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 ...
user avatar
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 ...
user avatar
  • 7,238
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), ...
user avatar
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() %}
user avatar
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|...
user avatar
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)...
user avatar
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 = [...
user avatar
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 ...
user avatar
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....
user avatar
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} ] %} ...
user avatar
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 }}">{{ ...
user avatar
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 %}...
user avatar
  • 608
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....
user avatar
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 ...
user avatar
  • 6,350
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]) ...
user avatar
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() %}
user avatar
  • 35.8k
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'...
user avatar
  • 8,316

Only top scored, non community-wiki answers of a minimum length are eligible