18 votes
Accepted

How can I incorporate a variable into a string?

If you‘ve got a multi-lingual site (or even if you don’t), you can pass variables into strings with the |t filter: {{ "Welcome back, {name}"|t( {'name': currentUser.name} ) }} (See http://...
user avatar
17 votes

How can I incorporate a variable into a string?

I know three ways: Using format() (look for type specifiers) {% set string = "There is an %s."|format("apple") %} Using string interpolation (assuming that apple="apple") {% set string = "There is ...
user avatar
  • 2,332
14 votes
Accepted

How can I make a template output its own filename?

You can get the current template’s name like so: {{ _self.getTemplateName() }}
user avatar
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 ...
user avatar
  • 4,361
12 votes

How to check variable type?

To my knowledge this is not possible natively in Twig. Depending on your situation / template needs here are some useful tests {% if foo is iterable %} Array or object {% if foo is same as(true) %} ...
user avatar
12 votes
Accepted

Avoid notice of variable's not being set within dev mode

I've had luck using "is defined" before using other logic operators. This should do it: {% if entry.title is defined and entry.title | length == 0 %} {% set entry = craft.entries.section('...
user avatar
  • 394
12 votes
Accepted

id: 'not' with variable

It should be a string: {% for entry in craft.entries({ section: 'news', limit: 2, id: 'not 42' }) %} so concat your variable like so: {% for entry in craft.entries({ section: 'news', limit: 2, id: '...
user avatar
  • 35.8k
12 votes
Accepted

Use a Twig macro to set a variable

I think the issue with trying to set a variable in a macro is that the variable is limited to the scope of the macro you are triggering, so {% set parent = thisEntry %} is available within the scope ...
user avatar
  • 4,509
11 votes
Accepted

Getting environmental variable via plugin

Solution: return craft()->config->get('environmentVariables')['baseUrl'];
user avatar
  • 2,265
10 votes
Accepted

How to create new variable from For Loop results

If I understood your question correctly then you should be able to do it as follows: {% set sizes = [] %} {% for productAttribute in entry.productAttributes %} {% set sizes = sizes|merge([...
user avatar
  • 6,146
10 votes
Accepted

How would I pass and entry's field value to an include?

You'll never need to put Twig tags within other Twig tags... Try it like this: {% include "partials/includes/page-header" with { 'title' : entry.heading } %}
user avatar
  • 23.6k
10 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(...
user avatar
9 votes

how to concatenate multiple variables and strings

Another option is to use the twig join filter: {% set urlParam = [street, city, 'UT']|join(', ') %} join concatenates the items in the sequence with the separator of your choice in between.
user avatar
8 votes
Accepted

How to check variable type?

I needed this feature today, so I've put together a simple plugin, which does the job: Craft-TwigTypeTest Example: {% set test_var = craft.entries %} {% if test_var is of_type('object') %} true {%...
user avatar
  • 8,316
8 votes

how to concatenate multiple variables and strings

Figured it out! Proper syntax is: {% set urlParam = street ~", " ~city ~", UT" %}
user avatar
  • 1,311
8 votes
Accepted

Use loop index number in a Twig variable

I found the answer in the Twig documentation: http://twig.sensiolabs.org/doc/templates.html#variables You can use a dot (.) to access attributes of a variable (methods or properties of a PHP object,...
user avatar
  • 2,563
7 votes

How to check variable type?

I recently needed to check if a variable is a number. My first idea was to use the divisibleby test: {# DOESN'T WORK AS EXPECTED! #} {% if var is divisible by(1) %} {% endif %} Unfurtunatelly twig ...
user avatar
  • 35.8k
7 votes
Accepted

Difference between using subscript syntax and "dot" syntax when accessing attributes of a variable or advantages of usage one of them

They're both the same for the most part. The only time I'd use the second way you listed is when the property is a variable. For instance: {% set foo = 'name' %} {{ variable[foo] }} This will give ...
user avatar
7 votes

How can I make a template output its own filename?

In Craft 3, {{ _self.getTemplateName() }} throws an error, but {{ _self }} does the trick.
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()->...
user avatar
6 votes
Accepted

Is it possible to use environment variables as the cache duration?

The error you're encountering is part of how Pixel & Tonic wrote the cache tag. In short, variables will not work for what you're trying to accomplish. For the longer explanation... Tags in Twig ...
user avatar
6 votes
Accepted

Best practice on how to hand over variables with links?

You're options are: Adding it as a parameter in the querystring (e.g. domain.com?key1=value1) Submitting it though a form and grabbing the value from POST. Setting the value in a cookie and ...
user avatar
  • 66.4k
6 votes
Accepted

What's the best way to degrade gracefully when using a Plugin Variable?

I think the best way to do this would be in your PluginHandleVariable file's method, just have it accept one, optional parameter and expect it to be an array. public function variableMethod($options =...
user avatar
  • 66.4k
6 votes

Working with Variables

Within your variables class file you will want to define methods that 'return' values to your template, like so: class HelloVariable { public function message() { return "Hello, world!...
user avatar
6 votes
Accepted

SET versus FIND

You're exactly right: The advantage of using the variable is that you can re-use it throughout your template, which helps keep things DRY. When you use craft.entries in your template, Craft serves up ...
user avatar
  • 3,390
5 votes

Avoid notice of variable's not being set within dev mode

If you want to set entry to a new value if entry.title is undefined or 0 length, try this: {% if !(entry is defined and entry.title is defined and entry.title|length) %} {% set entry = craft....
user avatar
5 votes

Calling variables, dynamically

Not sure if you saw this, but you may also be able to use the Twig attribute function: {{ attribute(array, item) }} Pretty straightforward, here's the official docs page...
user avatar
  • 23.6k
5 votes

How can I make a template output its own filename?

I don't believe this is currently possible outside of the context of a Craft plugin. Inside of a plugin, you could probably do it through a Twig extension so that you have access to the template ...
user avatar
  • 66.4k
5 votes
Accepted

Is it possible to compose a DateTime object from a string?

In Twig, the date_modify filter returns a DateTime object. Also, date is not just a filter, but also a function. The date function accepts any a DateTime object or a string in a supported format, and ...
user avatar
5 votes
Accepted

Best way to deal with Impossible to access an attribute ("isEditable") on a NULL variable ("")

You could combine the first three: {% if entry is defined and entry and entry.isEditable %} If the first or second part of this logic fails, it will not check the third part so no error will be ...
user avatar
  • 6,288

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