Questions tagged [templating]
Front-end Twig templates that can Craftily render markup, scripts, and styles.
1,697
questions
1
vote
2
answers
25
views
Range filter doesn't accept variables
How do I pass variables into the range filter from a static supertable field?
{% for item in variants.b2bProductQty %}
{% set start = item.selectRange.cellStartInteger %}
{% set end = item....
1
vote
1
answer
35
views
twig: loop and switch within set -> dirty code or pragmatic solution
First of all: My solution works ... I can not see any problems.
But I'm very unsure whether my code ist a dirty hack or not.
Please don't care about details in my code ... I'm uncertain because:
There ...
0
votes
0
answers
49
views
Craft 2 - Referencing Twig Variables in Javascript - Variables Do Not Exist
I'm working with a template in Craft 2, and in referencing Twig variables in the Javascript, they are not recognized when the page is previewed. I get a Template Error - Variable x does not exist
...
1
vote
1
answer
64
views
How to extend or override CP template?
I need to add some custom condition to the top siteMenu selector.
Template file with top menu:
vendor/craftcms/cms/src/templates/_elements/sitemenu.twig
Block with this menu:
vendor/craftcms/cms/...
1
vote
1
answer
66
views
Can you create a pop-up in Craft?
Is there a way to create a pop-up for when someone visits the site on the backend of Craft?
1
vote
2
answers
74
views
Element In Template Not Hiding On Page Load
I'm working on a template in Craft 2, and I have a number of divs which are part of a Javascript "read more" script, which automatically shortens the visible text at a set number of ...
1
vote
1
answer
32
views
Passing a variable to an include to set an image transform throws Setting Unknown Property error
I'm using an entry field to set an image transform handle:
{% set format = block.imageCrop %}
Then I'm trying to pass that to an include:
{% include 'partials/_img' with { format: format } %}
This ...
1
vote
2
answers
110
views
Make external links open in new tab, while internal not?
I've been going around in circles wondering which way to tackle this, so thought I'd ask while trying things out in case there's a really obvious way staring me in the face!
I have tens of thousands ...
1
vote
1
answer
62
views
Twig Runtime Error on Craft 4 Getting Started Tutorial
I'm working through the 'getting started' tutorial and I've run into an error I can't get passed. I've created the Category Group: 'Topics' and then created the Field 'PostCategories' and connected it ...
1
vote
1
answer
65
views
Create deep copy of twig variable
I'm building a filter page with Craft and Sprig.
The goal is to show the amount of products for each filter.
So for example if we want to filter on color we show 2 checkboxes "Red" and "...
1
vote
2
answers
59
views
Twig > for looping on objects -> only one result
Sorry per advance for my english :/
I am trying to construct an twig array in a .twig file like this :
{% set allergenesArray = [] %}
{% for allergne in recipe.allergenes %}
{% set ...
2
votes
4
answers
48
views
Find all entries containing a certain twig component
How can I get all the pages (entry urls) which are containing a certain .twig component?
Ex:
$entries = Entry::find()->search('button.twig')->all();
$urls = [];
foreach ($entries as $entry) {
...
0
votes
0
answers
37
views
How to list categories only related to enabled entries or disabled child entries
Edit: solved in comments
My entries are set up as a parent > child structure. A single entry can either have children or not have any. If parent has children, the children entries are put in a ...
1
vote
1
answer
53
views
I'm trying to remove transforms if my image is an svg? Any tips or advice would be great
{% for icon in icons %}
{% set image = icon.icon.one() ?? null %}
{% if image %}
<div class="c c-md-6 c-lg-6">
...
1
vote
2
answers
59
views
Applying custom CSS to a page based on custom ID or class
I want to apply custom CSS to 1 page and am wondering if there is an easy way to do either of the following:
-add some code to the Craft Template file that has the opening tag that can add an ID ...
2
votes
2
answers
89
views
How to prevent Twig Syntax Error when a plugin is disabled?
I am using the Sprig plugin, and for some reason I'm disabling it in some environments.
In config/general.php I have added: 'disabledPlugins' => ['sprig']
And in my twig template, I have a ...
1
vote
1
answer
34
views
Events with sub-events in a matrix field - display as a calendar (combined with other events)
I'm building a site where the client has courses that run once a week. We'll have several courses and each course will have a matrix with multiple blocks detailing that weeks class.
What I want to do ...
1
vote
1
answer
20
views
Matrix Field - Call block that matches block field value
I've got an event entry with each day of the event in a matrix field block. The block has a date field eventDate and I've got it set up so each block has a unique page that display with the date in ...
1
vote
1
answer
39
views
Creating a new SINGLES page presents a PAGE CAN'T BE FOUND or TEMPLATE CAN'T BE FOUND
I'm hoping you can help resolve my issue or point me in the right direction. Sincere apologies if this question has been asked a million times, however I can't seem to find anything specific to this ...
2
votes
1
answer
50
views
Variable entry does not exist on blog post
We just installed the CMS and like it very much. On the other hand while following the basic tutorial we seem to get the error "Variable "entry" does not exist." on the most simple ...
1
vote
1
answer
126
views
How to show entries randomly
I want to show entries (hundreds) for signed-in users randomly, I use this:
.order('RAND()')
and in the same time make sure to loop over all entries before start showing the entries that have been ...
0
votes
0
answers
113
views
CP templates are slow to render
My CP pages are taking ~15 sec to load. It seems the bottleneck is in rendering the Twig templates. This slowness also seems to have manifested quickly and it's affecting multiple separate Craft CMS ...
1
vote
1
answer
61
views
Tertiary Nav Items breaking Navigation when lightswitch set to zero
I have a navbar built out based on a pages section (structure) that has a field hideFromNav that when set to on hides the item from the nav bar - this is on line 8 of the code below.
This works fine ...
1
vote
1
answer
51
views
Update from Craft 3 + Reasons to Craft 4: how to get fields' conditions from template?
I've just updated from Craft 3 to Craft 4. I was previously using the Reasons plugin for CP but also front-end purpose. On front-end, I was printing field layouts and show/hide the fields based on the ...
3
votes
1
answer
281
views
What does the triple operator ??? do in this case?
I know what the null coalescing operator (??) does. I have an example here with 3 operators and I am unsure what it does?
Here is an example that I came across in a script I am trying to debug:
{% set ...
1
vote
1
answer
146
views
Changes to twig template take over 20s to propagate
If I make changes to one of my twig templates, it takes over twenty seconds for Craft to reflect the updates on the local site. I'm running Craft locally in DDEV.
I tried the debug toolbar but am ...
0
votes
2
answers
54
views
How do I only display a list of users that have entries and hide the rest
I want to list users that have created entries in a specific channel.
I tried it the Ben Parisak way, but that didn't work out.
here's the code that doesn't work (example 1):
{% set entryIds = craft....
1
vote
1
answer
67
views
Redactor (or Twig?) Issue stripping <R> and <TM> strings
This is a Craft 3.7.x install with Redactor.
My client is writing content referencing the proper usage of their trademarks. They'd like their content to state that it's permissible to use "...
2
votes
1
answer
51
views
Create automatic subpage (kind of) for each entry
I really searched this forum for question like this but i was not able to find the answer.
Im creating news portal website, where I have blog entries with url like this
/blog/SLUG
I want for every ...
0
votes
1
answer
172
views
Eager-loading in a page builder built on NEO field
Upon upgrading to Craft 4, I've realized that I should implement eager-loading in quite a few places as suggested by the Blitz caching plugin. Most of these are within our page builder, which is built ...
0
votes
0
answers
40
views
Trouble with channel URLs and CSS files not pulling through - Craft CMS 4 Multisite
I'm building a multisite on Craft CMS 4 and I'm having trouble with channel URLs. At the moment I'm working locally and then pushing it to the hosting via github.
I've created a folder for each site ...
1
vote
1
answer
31
views
How do you get Craft to generate multi-paragraph text?
In every other CMS I've used there's an option to compose multi-paragraph text in the back end which is then parsed out into P tags in templates. Neither the native text field nor the popular Redactor ...
1
vote
1
answer
50
views
Group/sort by multiple criteria
I have some users:
{% set people = craft.users({
groupId: '1',
order: 'lastname ASC'
})
And two lightswitch fields associated: isLeadership and isActive.
This code groups users by ...
2
votes
1
answer
124
views
extending the Craft control panel templates are unstyled
First time building a custom control panel dashboard for a module.
Template loads ok but is unstyled and not loading JS that the rest of the control panel screens do.
{% extends "_layouts/cp"...
0
votes
0
answers
28
views
How to append the category field data nested inside a section to an input tag?
while clicking the input tag it should display its related entries
Based on the category field the data selected in the backend value should get appended to the input tag.
When looping through the ...
1
vote
2
answers
440
views
Craft 4: How do I get field by handle?
In Craft 2, I used to be able to do this:
https://craftcms.com/docs/2.x/templating/craft.fields.html#getfieldbyhandle-handle
{% set body = craft.fields.getFieldByHandle('body') %}
{{ body.instructions ...
1
vote
1
answer
18
views
Field data as part of external URL call (jsonclient plugin)
It's possible it's the various search terms I'm using but I can't seem to find anything close to what I'm looking for.
I am trying to call an external API URL, with keywords from a field, but while I ...
0
votes
2
answers
292
views
How to reuse "functional code" in multiple twig files
I usually load and prepare data before calling a certain include. That data may be loaded in a certain way for different cases, so I cannot add that data preparation code into the include itself. I ...
0
votes
1
answer
88
views
Custom Element Index stopped working after Update
So we had an "old" Craft 3.1.* instance that i needed to update. Which is now running on Craft 3.7.20.
This instance does have a Module which introduces a new ElementType with its own custom ...
0
votes
2
answers
68
views
How to trim each string in an array?
I am looking for how to trim each string in an array, in Craft 3.
I would like to check if a string is in an array of strings. The array is from a form submission, a checkbox group.
For example, there ...
2
votes
1
answer
973
views
Loop last error getting entries
Craft 4
Key "last" for array with keys "parent, index0, index, first" does not exist.
When using
{% if loop.last %}
Data is there and printing out a menu but cannot seem to get ...
0
votes
1
answer
80
views
Accessing eager loaded Collections data
So I have a query to return series. At the same time I eager load the series client.
{% set series = craft.entries()
.section('series')
.inArchive(false)
.with('client')
.collect()
%}
...
0
votes
1
answer
105
views
How to add Field to Template hook cp.entries.edit.settings and save value to entry?
I'm trying to show a custom lightswitch field inside the right panel on the edit page of an entry and saving its state to the entry when the user clicks on save.
My form template looks like this:
<...
0
votes
1
answer
35
views
Behaviour around {% exit %} is different on local VS production
I'm trying to get my site to display a message and exit on the basis of a querystring in the URL. I have this code at the top of my main template:
{% set queryString = craft.app.request.queryString %}
...
1
vote
1
answer
740
views
Updating/replacing query string parameters with twig
I have code similar to below that basically just outputs links with query parameters:
{% for country in craft.categories.group("country").all() %}
<a href="{{ url('wine') }}?...
0
votes
1
answer
94
views
How to use twig macro inside controller?
I want to use the macro I defined in twig inside a controller or a module.
Is that possible?
0
votes
3
answers
75
views
Get section handle of an entry inside of an EntryField
Maybe Craft 4 is playing games with me but im stuck on this problem:
linkedEntry is an entry field which is limited to 1 entry.
This outputs the section handle correctly
{% set linkedEntry = ...
1
vote
1
answer
118
views
Do I have to check if a field has a value if I want to use it in an include that already checks the value?
Let's say I have a channel called testimonial which has a field called customerImage.
I want to use a twig template to display information about the customer, which may or may not have an image set.
...
0
votes
1
answer
117
views
Debugging slow loading entries with matrix field in Control Panel
From a recently migrated Craft CMS 2 site, there are various entries in the Control Panel which take a very long time to load (sometimes over 30 seconds). This is the same in our production ...
1
vote
1
answer
312
views
How to implement Laravel's collection `filter` method inside twig?
How to implement Laravel's collection filter method inside twig?
Laravel:
$collection = collect([1, 2, 3, 4]);
$filtered = $collection->filter(function ($value, $key) {
return $value > 2;
}...