Skip to main content
14 votes
Accepted

Pass variable into include

You can leave the with lightswitchTest part off altogether, and all the variables from the parent template will be available inside the include too. with expects an array, where the keys correspond ...
Mike Pepper's user avatar
  • 4,391
10 votes

How can I make a template output its own filename?

In Craft 3, {{ _self.getTemplateName() }} throws an error, but {{ _self }} does the trick.
hashchange's user avatar
9 votes

Use a Twig macro to set a variable

A little bit late but maybe someone else has the same issue and want's to accomplish this. You can use this Plugin Twig Perversion to create macros that return values {% macro getParentEntry(...
Robin Schambach's user avatar
7 votes
Accepted

Does Twigs 'addGlobal' exist in Craft?

Edit: I needed to do a little digging. Internally, Craft can actually have multiple instances of Twig_Environment running (a Twig_Environment instance is what you get back by calling craft()->...
Mats Mikkel Rummelhoff's user avatar
5 votes

How can I make a template output its own filename?

Post marked as answer doesn't work for me nowadays. For me works: {{ craft.app.requestedParams.template }}
Дмитрий Боровков's user avatar
4 votes
Accepted

Reusing logic to set string variables

I would use macros. A Twig macro is basically a function, that can take arguments and render HTML. Macros can be defined inline (i.e. in the same template where it's used) or in a separate file (...
Mats Mikkel Rummelhoff's user avatar
4 votes

Remove white space and parenthsis from global from entry variable

For what it's worth, you can strip out all non-numerics with this something like this: {{ globalPhoneNumber.phoneNumber|replace('/[^0-9]/', '') }} Or, as part of a tel: anchor: <a href="tel:{{ ...
Nick Benson's user avatar
4 votes

How to check variable type?

I hit a snag for this where I needed to check if a value was a boolean type. This isn't the greatest approach, but it does work and is easy to interpret: {% if value is same as(false) or value is ...
Kevinleary.net's 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 }}">{{ ...
Katie Fritz's user avatar
4 votes

Twig variables wont work in an include js file?

There is no "syntax" for it since you don't parse these files with PHP. You'll have to declare your variables inside a twig file First Approach Twig <script> var myVariable = {{ theme|...
Robin Schambach's 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 %}...
Udo's user avatar
  • 607
3 votes

Test a variable in a set entries tag

You're dealing with scope issues – both related to for loops and in regards to extending templates (blocks). First, any variable set inside a for loop will not be available outside that loop. The ...
Mats Mikkel Rummelhoff's user avatar
3 votes
Accepted

Test a variable in a set entries tag

A quick fix would be to set homeFeature outside the for loop and than the one inside the loop would change the one outside, you could than access it from other parts of your template. {% set ...
Gui Rams's user avatar
  • 106
3 votes
Accepted

Macros, Variables and Includes

You aren't able to pass an import into a macro. So helpers needs to be imported into each macro file that uses it. You can however pass the cart to your macros. As a side note, it's definitely best ...
Luke Pearce's user avatar
  • 3,863
3 votes
Accepted

Why two equal variables give false statement?

It seems like you're trying to compare two EntryModel objects. Could you just compare their ids? {% if rowParent.id == parent.id %} // Go {% endif %} Also, try using Twig's dump() function to ...
Alex Roper's user avatar
  • 2,642
3 votes
Accepted

Set a Variable inside a conditional

You're trying to your float variable using another variable at the moment that isn't defined. I expect you mean to set float to the string value "yes" like so: {% set float = "yes" %} You could also ...
Luke Pearce's user avatar
  • 3,863
3 votes
Accepted

How do I make my variable available inside an assets loop?

Your problem is a scoping issue. You've defined theUrl inside the scope of a for loop, so it only ever lives inside of that loop. You can do this to have it exist outside of the loop, too. {% set ...
Brad Bell's user avatar
  • 67.5k
3 votes
Accepted

Adding a random entry to an array

You can use slice and merge filters to “insert” that random entry into your array of entries. In the following example I’m using the id param to query for the selected entries and I’m also setting ...
carlcs's user avatar
  • 36.3k
3 votes

Using a variable with array of ElementIds to use in craft.entries.id

Luke's answer above is correct, you might as well just use the array directly. But technically you can use a joined string as well, it's just your syntax is wrong - you just call the variable ...
Jeremy Daalder's user avatar
3 votes
Accepted

Using a variable with array of ElementIds to use in craft.entries.id

I'm not sure where you're getting the id's from but you will need to pass an array into craft.entries.id so try: {% set navsubids = craft.amNav.getNavRaw("hoofdnavigatie", {startFromId:25}) %} {% ...
Luke Pearce's user avatar
  • 3,863
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) %} ...
Marion Newlevant's user avatar
3 votes
Accepted

Call a Craft variable inside array

To output variables inside an array like that you'd simply omit the twig braces.. at the moment the ' are telling whatever is inside them to output as a string. UPDATE: try adding parentheses around ...
Luke Pearce's user avatar
  • 3,863
3 votes
Accepted

What is the proper way to set an asset's upload location using the slugs/ structure path of the entry it is selected from?

To include the handle of the slug, and the path within the structure based on the entry uploaded from in the upload location field I've found the following to work: {section.handle}/{parent.uri}/{...
Bonk's user avatar
  • 159
3 votes
Accepted

stashing variables?

Twig doesn't let you stash variables for access elsewhere; rather every variable has a well defined scope. So what you'd have to do is store that id in a variable that has a global context, so other ...
andrew.welch's user avatar
  • 11.6k
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 %} ....
andrew.welch's user avatar
  • 11.6k
3 votes

How can I pass a variable defined in a Controller to my variables?

The shortest answer is "that's not possible," and the slightly longer answer is "that's not possible because Controllers and Variables have no reason to ever interact." If your Controller relies on a ...
Lindsey D's user avatar
  • 24k
2 votes
Accepted

How to shorten 'craft.request.getUrl()' with the shortener plugin?

The issue was that the custom short name I was using (in this case the development domain) was not associated with the bitly account. Total oversight! I've removed that and it works perfectly.
ByteMyPixel's user avatar
2 votes
Accepted

Reseting variable

You have three for loops, and are setting currentMonthEarning to 0 outside of all of them, and then adding to it (and never resetting it to 0). Probably you should move the {% set currentMonthEarning ...
Marion Newlevant's user avatar
2 votes

how to concatenate multiple variables and strings

Just wanted to add another example to this since this post help point me in the right direction. In my example, I am getting back a list of events from a 3rd party. All that is returned to me is the ...
Damon's user avatar
  • 4,706
2 votes

How can I globally set variables?

What you're trying to do is how I used to do it; for your specific case, I'd suggest trying out SEOmatic, which will keep all of this out of your templates, but still give you the control you want. ...
andrew.welch's user avatar
  • 11.6k

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