Questions tagged [templating]
Front-end Twig templates that can Craftily render markup, scripts, and styles.
1,654
questions
1
vote
1
answer
28
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;
}...
1
vote
1
answer
23
views
Get all entries by structureId
It is possible to get all entries by structureId ?
If yes how ?
For now i use something like this:
{% if entry.parent %}
{% set relatedEntries = entry.parent.children()|filter(v => v.type == '...
1
vote
2
answers
20
views
Translate only the default twig value (Nesting twig filters)
Is it possible to write this as a one-liner?
{% if craft.entries.section('mypage').one() != null %}
{{ craft.entries.section('mypage').one().title }} {# If title is provided - use, but don't ...
1
vote
1
answer
25
views
How can I output only half the entries in a loop?
I'm building a Craft website on Bootstrap 5.
I want to split the output of a loop evenly across 2 seperate columns.
There is an image column in the middle so the loop can't be continuos.
The amount of ...
3
votes
1
answer
45
views
.all() — Looping through element queries directly has been deprecated
This is very nearly a duplicate question but I can't figure out where to insert .all() to execute my element query. I'm still asking because I may be messing up php, function or filter syntax ...
2
votes
1
answer
39
views
Craft Commerce: Multiple redirect submit buttons from cart form (checkout/update cart) without ajax
I'm building a basic cart page in Craft 3.7.37 with Commerce 3.4.12. The /store/cart page has a <form> with the line items in the cart next to an <input /> to change quantity and one to ...
2
votes
1
answer
31
views
Disable template cache globally based on condition
I'm looking for a way to disable the template cache ({% cache %} tags) for the entire request based on a specific condition (not just based on the current environment). In this case, the condition is ...
1
vote
2
answers
27
views
Construct entries object from an array of IDs
I have 2 arrays of entry ID's that I need to merge and generate an entries object that I can iterate through and paginate. The reason for doing it this way at all, is that I need to display entries ...
2
votes
1
answer
44
views
Can I change a user field with twig without using a form?
I basically just want to set the value of a users field to a different number if something happens, so that the user field is actually updated in the CMS. Can I do that without creating a form? So ...
1
vote
1
answer
95
views
Twig in array not working as expected
I am trying to get an entry type check on an array
{% set landerArray = ['fundingLander','healthLander', 'projectLander', 'publicationLander'] %}
{% set pageType = entry.type %}
{% if ...
1
vote
1
answer
44
views
Uppercase certain words in string
We have some content that contains words that should be presented in uppercase - the content comes via an API and is in all uppercase, we change it to title case to make it presentable.
I had started ...
1
vote
1
answer
24
views
Hack into .getUrl() method of an entry
Due to some SEO and infrastructure issues I would like to be able to intercept the getUrl() method of entries and modify its return value.
e.g.
<a href="{{ entry.getUrl() }}">{{ entry....
0
votes
0
answers
39
views
'Endless Loop' Kiosk Crashing Randomly - Craft 2
I am needing an 'endless loop' through my entries for a kiosk application, and was wondering if this approach would work for that? I had been using just Twig on a template, cycling through my selected ...
0
votes
0
answers
31
views
"Missing template" when omitting file extensions (.html.twig) or underscore
I'd like to be able to only have to refer to a file by it's name, just like it works in JS (e.g. import './js/scripts') and SCSS (e.g. @import './variables'), without the need for file extensions or ...
2
votes
1
answer
119
views
Debugging with dump()/dd() within Twig templates
This feels like it falls under silly/obvious questions, but I can't seem to find a decent answer anywhere, so I'm asking here:
How do I find out which methods for example, is available on an object? ...
0
votes
1
answer
21
views
Get the localized handle and slug for "current" section for constructing breadcrumbs
I'm trying to contruct breadcrumbs for entries under a specific section. This feels like such a "how-hard-could-it-possibly-be" type of question, but I've been at this for a lot more time ...
1
vote
1
answer
78
views
Checking for a valid date in a query string parameter using Twig
I have a form that lets you search articles by date.
The 'Results' template uses dates from the query string in an Entries Query like this:
{% set afterParam = craft.app.request.getQueryParam('after') ...
2
votes
2
answers
106
views
PHP array missing attributes when converted to JSON
I am converting certain php data to json for use in my front end javascript.
I have an array of product variants, an array of images from a media field that is defined on my variants, and an array of ...
1
vote
1
answer
29
views
How can I use the final URI segment as an argument without losing the entry variable?
I have a Structure - Products - that contains multiple products, and each product contains different sub pages (details, pricing, etc).
One section of the Structure is Details which is under each ...
0
votes
0
answers
23
views
Craft 2 Manually Rendered Template - Logic
I'm currently working on a plugin in Craft 2 that will load entries into Ajax and then output to a template, one entry at a time, controlled perhaps by javascript window.setTimeout to advance to the ...
1
vote
1
answer
25
views
Table Field Row ID?
I need a unique id for each table row. Something like this which does not work:
{% for row in block.buttons %}{{ row.id }}{% endfor %}
Is this possible?
Thanks!
1
vote
1
answer
101
views
How to create custom utility/helper functions for use in Twig templates?
So when using Drupal or Wordpress -- obviously you can write as much PHP in a template as you need, or in Wordpress for example, you can abstract that functionality away to the functions.php file. ...
1
vote
2
answers
83
views
Showing URL in 'clean' way, omitting 'https://', 'www' and '/'
Is there a way to dispay URLs as a 'clean URL' within Twig? I want to omit 'https' (or 'http'), 'www.' and the '/' at the end for URLS I want to display in the front-end.
Input:
https://www....
1
vote
1
answer
37
views
Get parent entry from child entry where relationship is in parent matrix field
I have a parent entry with a matrix field and one of the blocks has a relationship to a child entry.
How do I access the parent entry from the child entry page?
tried this, where eddChapter is the ...
1
vote
1
answer
70
views
Is it possible to access a Craft service directly from a Twig template?
Before attempting to perform an asset transform to an AVIF or WEBP format, I'd like to use Craft's Images service to determine if the server actually supports either of those formats like so:
<...
1
vote
0
answers
24
views
Craft 2 - Date Comparison In Criteria - Impossible to access an attribute ("today") on a string variable (">=")
I am working with a template in Craft 2, and I am setting up the criteria for entries to be displayed.
Here is my code (below).
{% set today = now|date('Y-m-d') %}
{% set criteria = {
section: '...
0
votes
0
answers
30
views
ImagerX Connection Refused on Craft Nitro
I've currently got a website setup with Craft Nitro, though when trying to query an ImagerX image I get the following error:
cURL error “7” encountered while attempting to download
“http://xxxxx....
1
vote
1
answer
32
views
Asset Custom Field Returning Null in Array
I am relatively new to Craft CMS and I have encountered an issue that I have attempted to debug for quite some time.
When using the {% dd %} function to dump my array, a load of my custom fields ...
1
vote
1
answer
29
views
Bad Gateway when using cached category query
I'm trying to find all tiers that have associated entries for a selection dropdown.
This does not work, and throws a 502 error
{% set allTeirs = craft.categories.group('catPartnerTeirs') %}
{% set ...
1
vote
1
answer
66
views
How to find next sibling from entry order of parent entries field
I have an index of projects that uses a standard entries field to select multiple entries to display and also provides a custom order (different to their Structure order). This is my "Projects&...
1
vote
1
answer
33
views
Replace HTML string in body tag?
I have thousands of imported entries which have HTML image embeds to files which no longer exist. Is there a way to use the replace function in the tag to say replace <img src="[variable ...
-1
votes
1
answer
76
views
how to create a custom plugin if the user enables plugin settings then it will replace the website home page to my custom design? [duplicate]
I am trying to create a plugin if the user enables plugin settings then it will replace the homepage of the website with my custom designed template
1
vote
1
answer
48
views
For loop to dynamically output field names
I have a set of fields:
fieldName1
fieldName2
fieldName3
fieldName4
fieldName5
and I want to be able to create a for loop something like this:
{% for i in 1..5 %}
{% if entry.fieldName ~ i|length ...
3
votes
1
answer
84
views
Is there a way to return all available (custom) variables in some kind of list?
As I'm still a total n00b in working with CraftCMS, so I hope to get some help from more experienced users :-)
I followed every available tutorial (which are very clear and well written. Great job!) ...
1
vote
1
answer
51
views
How to Create an Author Page?
I'm trying to understand the best or most common approach to creating unique author/user pages that list that user's blog entries.
This question was helpful for how to process the results.
However, I'...
0
votes
1
answer
95
views
Want to use assets field my template settings in custom plugin craftcms
I am creating a plugin that is based on images the admin will upload some sort of images in plugin settings not sure how to creat image field setting in plugin
1
vote
1
answer
51
views
Twig filter country_name in craft CMS
Craft CMS doesn't have TWIG filter country_name. Even by default country name filter package is not available in twig. We need to install the corresponding package. https://twig.symfony.com/doc/3.x/...
1
vote
1
answer
92
views
Upgrading to 3.7.26 causes tempnam() error from twig's FilesystemCache.php
I am upgrading a Craft project that I have been neglecting for a little bit too long. I am upgrading to 3.7.26 from 3.4.25; on top of this, I am upgrading from PHP 7.1 to PHP 7.4.
I'm also updating a ...
1
vote
1
answer
100
views
Using UrlHelper from template or remove query string params when paginating?
I have an implementation of a pagination which uses fetch() to get a subset of the page (i.e the page listing and pagination links), so when switching pages it gets the new list, as well as the ...
0
votes
0
answers
17
views
Custom GraphQL Resolver that utilizes the same craft function used in twig
I'm pretty new to Craft CMS or PHP for that matter. I have custom code that unfortunately can't be done with just GraphQL so I need to utilize the same functions used in a twig template, in a custom ...
1
vote
1
answer
26
views
Craft 3 paginate not working when fetched with a controller
I'm loading a paginate via Craft::$app->getView()->renderTemplate(), this is being executed within a controller. But my pageInfo.nextUrl is now the url of my controller (/actions/controllername/...
0
votes
0
answers
17
views
Entry Looping Template With Javascript and 500 Internal Server Error
I'm working with a looping mechanism in my Twig code that loops through entries and displays them for a kiosk type of application.
The loop itself works for the most part, although after a few hrs, it ...
1
vote
1
answer
26
views
Find attributes in an object
I have a craft project which I've inherited and I'm trying to make some minor changes. I don't know PHP, Craft, or Twig very well so forgive me if this is an XY problem. This website is a company ...
1
vote
1
answer
51
views
Customizing Field Layout / UI element in Craft Control Panel
Maybe I don't understand well the usage of UI Element in Field Layout, but I would like to display something more 'jazzy' for the admin on his CP and I thought inserting a twig template in the page ...
1
vote
1
answer
104
views
Remove red error lines in VS code for TWIG
I've come across this a few times in my code / and others.
Basically - there is no error, this is valid syntax and works, but is there a way I can highlight this and tell VS code - this is ok, dont ...
1
vote
1
answer
23
views
how to write db-commands on custom table (PHP syntax) inside twig file?
if i have a custom table (ct_table) and i have to use MySQLi commands on it using the PHP syntax,
it won't work if i put the PHP snippets inside a twig file.
So are there any tools or official methods ...
1
vote
2
answers
94
views
Replace asterisk (*) with my own HTML in twig-templates
I'm working on a Craft CMS website and I need help with formatting the H1 tag.
I would like to be able to easily outline some parts of the title, like in the example below.
Ideally by adding an ...
2
votes
1
answer
158
views
Get entry associated with current uri
If I am visiting /recipes/spaghetti which corresponds to an entry in a recipes entry type and the recipes/entry template, is there a way to directly get the current entry's data from within the ...
0
votes
0
answers
76
views
Passing variables, and reading them, to Module template
I'm trying to pass an array to a module's twig template (on the front end). I have tried many solutions I've seen here, but I'm not able to read the passed variables inside the twig template.
In my ...
1
vote
2
answers
230
views
Unable to find the template
I know this question has been asked multiple times. I have read all the post on this topic, and craft's docs. But still, I haven't figure out what I'm doing wrong. I would really appreciate a hand ...