11
votes
Accepted
Select entries starting with same letter from alphabet list
First, you should create a dynamic route (something like "games/glossary/<slug>") and point it to a template. A dynamic route means you won't have to create a template for every letter. Note the ...
9
votes
Accepted
How to get IP address without a plugin?
To make use of your visitors IP address, use {{ craft.request.getIpAddress() }} in Craft 2.5. In Craft 3.x, there is {{ craft.app.request.userIP }}.
8
votes
Accepted
When to use "if" or "without"?
For your example, you should exclude the fictional towns in the initial query itself rather than filtering them out afterwards:
{% set towns = craft.entries.section('towns').townFictional('not 1')....
6
votes
Accepted
If page conditions
Both titles can not appear in your condition(on the homepage). Always only one true statement is executed.
To your problem: there is one global variable that solves your problem.
{% if craft.request....
6
votes
Accepted
If page is enabled, show div
You can check the status of an entry with .status. https://craftcms.com/docs/templating/craft.entries#status
This code should work!
{% set jobsEntry = craft.entries.id(jobsId) %}
{% if jobsEntry....
5
votes
Accepted
If YouTube in Redactor Then Wrap in Div
Check out the Retcon plugin!
Retcon is a small Craft CMS plugin offering a series of easy-to-use Twig filters for manipulating HTML content.
Specifically, it sounds like you need the wrap method:
...
5
votes
Accepted
How to check if matrix block type is empty or not
You can fetch the number of entries for the first type, check if you receive an empty array if so fetch matrix blocks of type B
Craft 2
{% set blocksTypeOne = entry.matrixFieldHandle.type('...
4
votes
Accepted
Is the length filter required when checking if a field has content or not?
You can probably get away with removing |length for most conditionals that check for the existence of content in a custom field and just check on entry.fieldHandle instead.
One exception to this ...
4
votes
Accepted
Parent based Selectable Entries Condition
Rendering Twig in the "Selectable Entries Condition" field setting isn't actually possible, but it will be in the upcoming Craft 4.4 release, which as of writing this (march 1st 2023) is due ...
3
votes
Conditional check if Matrix block field is empty (no content)
I'm assuming your matrix field is not called hours? Because in your current code you are handeling it like it is.
I think this should work
{% for block in entry.matrixfieldHandle %}
{% if ...
3
votes
Accepted
Checking if a field contains information
If you want a Dropdown field with the option to not select a value (or to clear out the current selection), you can just add an option (typically the first) with an empty value column. If the empty ...
3
votes
Accepted
Check if current entry is in loop and add class
Firstly, from the Craft docs on routing:
If the URI matches an entry’s or category’s URI, the
section’s/category group’s template will get loaded, and the matched
element will be made available ...
3
votes
Accepted
How can I filter selected/related entries by their categories?
Start by looping thru each category. Say, all categories in a category group called "eventsCategories." Then find entries related to each category (if any).
{# loop through each of the categories #}
{...
3
votes
conditional does not run first in a file that is included using the extends feature for twig
The issue here is that extends isn't the same thing, neither conceptually or technically, as an include. When you extend a template with a layout template, the template is "decorated" by the layout. ...
3
votes
Avoid notice of variable's not being set within dev mode
Since Craft 2.6 you can do this cleanly with the ?? null-coalescing operator:
{% if not (entry.title ?? false) %}
...
3
votes
Accepted
Does Craft parse requests inside a conditional if it is false?
Yes, Twig will parse it all down to PHP before running it. It stores the rendered PHP in a /storage/runtime/compiled_templates/ file.
No, Craft will not run anything inside the conditional if it's ...
3
votes
Accepted
If multiple entry types?
You were so close! Instead of asking whether entry.type is equal to the array (which it isn't), you need to find out whether the value of entry.type matches one of the array items.
Here is an updated ...
3
votes
Accepted
How to determine if there are events in a calendar?
Looking at the docs you can search for events for a certain calander by including the id or the handle in your query.
{% set calendars = craft.calendar.calendars({
limit: 100,
order: "name",
...
3
votes
Accepted
Add conditional in a for loop
{% for type in entry.buildType %}
{{ type }}
{% if not loop.last %}/{% endif %}
{% endfor %}
See: https://twig.symfony.com/doc/2.x/tags/for.html#the-loop-variable
3
votes
How do multiple date queries differ between Craft 2 and 3
My Craft 3 version is:
{% set result = craft.entries()
.section('events')
.eventDate( ['and', '<' ~ now|atom, '>' ~ now|date_modify('-6 days')|atom ] )
.results(':notempty:')
....
3
votes
Twig default filter or Null coalescing operator?
So things like the null coalescing operator make it easier to chain things, so if you have a number things to fall back on, it's much neater to write:
{% set woof = bark ?? howl ?? growl ?? null %}
....
3
votes
Accepted
Check for number less than x in array
Twig isn't very well equipped to deal with more than basic logic and comparisons, but you can accomplish some of these tests with a bit of extra juggling:
{% set constraintMet = false %}
{% for ...
3
votes
Is there a way to make conditional input fields in Craft CMS?
For what it's worth, it's coming in Craft 4. The author of the Reasons plugin has specifically said he won't port it to Craft 3, as this will be part of core in the next release.
3
votes
Accepted
Tertiary Nav Items breaking Navigation when lightswitch set to zero
The issue is that the way you check whether the current entry has children is inconsistent. You're using the {% ifchildren %} tag, which will only check if the entry has children in the pages array, ...
2
votes
How can I add a dynamic "active" CSS class to the navigation on any given page?
I know this post is old, but I'd like to share another technique I've been using for the past year. If you need a "hard-coded" navigation with no editing capabilities in the admin but still want ...
2
votes
Can't get checkboxfield values to show
If you want a comma-separated list of selected options, try this.
{% for option in offer.offerMinimumPrice.options|filter(v => v.selected) %}
{{ option.label }}{% if loop.last==false %}, {% ...
2
votes
Accepted
Conditionals and outputting tables
Assuming your example code, something along these lines will help...
{% if entry.yourTable | length %}
<table>
{% for row in entry.yourTable %}
<tr>
<td>{{ row....
2
votes
How can I filter selected/related entries by their categories?
Craft’s |group filter can be used here.
I’m going to assume that the Categories custom field on your Exhibitor entries is called myCategoriesField.
{% set groupedListings = entry.myEntriesField.find(...
2
votes
How to add two variables into one conditional
It looks like according to your code this (and Aran's) solution should work except for one small difference... if you are testing for this:
entry.title ~ category.title
The value parameter (what is ...
2
votes
If page conditions
I like the solution above. I'd add craft.app.language if you're working on a localized site.
{% if craft.app.request.absoluteUrl == siteUrl or craft.app.request.absoluteUrl == siteUrl + craft.app....
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
conditional × 139templating × 37
matrix × 15
forloop × 9
field × 8
entries × 7
categories × 7
url × 7
entry × 6
navigation × 6
entries-field × 6
logic × 6
variable × 5
datetime × 4
craft4 × 4
assets × 3
plugin-craftcommerce × 3
form × 3
structure × 3
sections × 3
category × 3
entry-type × 3
limit × 3
craft3 × 2
plugin-development × 2