Questions tagged [variable]

Variable a symbolic name associated with a value that may be changed

Filter by
Sorted by
Tagged with
13
votes
4answers
49k views

How to check variable type?

How do I check what type my variable of (string, integer, boolean, array, dictionary, etc)?
9
votes
1answer
24k views

Use loop index number in a Twig variable

Let's say I have 5 Craft fields named "item1" thru "item5." Instead of writing out each variable... {{ entry.item1 }} {{ entry.item2 }} {{ entry.item3 }} {{ entry.item4 }} {{ entry.item5 }} How can ...
9
votes
2answers
12k views

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

I experienced that there is a difference between real DateTime objects (as now is one) and strings composed to "fake DateTime objects" (ie. {% set myDate = '2014/06/24' %}). Sometimes it makes no ...
8
votes
2answers
18k views

Use a Twig macro to set a variable

Is there any way to use a macro in twig to define a variable outside that macro? For example, say you want to use a macro to set a variable that stores an entry's parent (I know you can do this much ...
7
votes
3answers
310 views

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

With dev mode on, I'm getting a warning there is no entry.title, which I know about already because that's what I'm testing for: {% if entry.title | length == 0 %} {% set entry = craft.entries....
7
votes
3answers
726 views

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

I'm trying to set a cache duration as an environment variable. I set up various environment variables and know they are being referenced correctly in my templates because I can print out the values ...
6
votes
2answers
13k views

How can I incorporate a variable into a string?

How can I incorporate a variable into a string, so that the result is a longer string that includes the contents of a given variable? For example, I have a variable called apple and its value is "...
6
votes
1answer
2k views

id: 'not' with variable

i think the following code shows the problem: {% for entry in craft.entries({ section: 'news', limit: 2, **id: not this_id** }) %} the big one doesn't work in this way. Has anyone an idea? The ...
6
votes
2answers
172 views

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

When to use {{ variable.property }} and when to use {{ variable['property'] }} Is there a difference between these methods of accessing attributes of a variable or advantages of using one of them?
6
votes
2answers
4k views

Calling variables, dynamically

I've got a parent template which contains the following code: {% include '_layout' with {'countryCode': 'au'} %} I have a global set with the handle au_Variables, and within that a field with the ...
5
votes
3answers
45k views

how to concatenate multiple variables and strings

I am trying to form a url parameter (for Google Maps API) from entry field data. Basically, this is what I would like to do: {% set street = entry.address %} {% set city = entry.city %} {% set ...
5
votes
1answer
2k views

Getting environmental variable via plugin

A similar question was answered before, but I was wonder how I might be able to return the same query from within a plugin (php)? Convert this: {{ craft.config.environmentVariables.baseUrl }} To ...
4
votes
3answers
5k views

How can I make a template output its own filename?

Is it possible to output the template name being used? Using a global variable or something similar? I am wanting to check which template is being used in a conditional.
4
votes
1answer
6k views

Pass variable into include

I have a snippet within a template which I want to pass a variable into: page.twig {% set lightswitchTest = entry.aktivesProjekt('1').find() %} {% include '_snippets/thingy' %} thingy.twig <...
4
votes
1answer
4k views

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

I have this: {% include "partials/includes/page-header" with { 'title' : '{{ entry.heading }}' } %} But it seems that nothing I do will actually pass {{ entry.heading }} as 'title' in the include.
4
votes
1answer
2k views

How to create new variable from For Loop results

I need to get a list of all the "sizes" from my Table type field column. The list of sizes needs to be separated by a "|"... eg: small|medium|large My Table type field name is "productAttributes" and ...
4
votes
1answer
530 views

Does Twigs 'addGlobal' exist in Craft?

I know Craft gives us access to Twigs getTwig template service; so when I attempt to use the addGlobal function like so: craft()->templates->getTwig()->addGlobal('var', $var); ... nothing ...
4
votes
3answers
4k views

How can I globally set variables?

Originally at Include a template that sets variables (global template variables). I have a number of templates and layouts but every layout includes a global header in which the meta data is ...
4
votes
1answer
600 views

Best practice on how to hand over variables with links?

You sometime want to pass specific info from one page to another. How do you do that with twig and are there explicit Craft methods to do this? Can you do this without exposing the variable in the ...
4
votes
1answer
309 views

Setting arbitrary PHP constants in index.php

I was looking for a way to set arbitrary PHP constants in the index.php that could then be accessed within templates. This is particularly useful if you have more than one index/domain pulling ...
4
votes
1answer
218 views

Reusing logic to set string variables

I would like to set some variables for every entry within a section ('courses'), and then use those variables in loops that are filtering by entry types and categories. Some simplified markup: <...
3
votes
2answers
3k views

Dynamically load craft entries based on twig variable

I am looking for something quite simple, but I still can't find a way to do it in twig syntax. So basically here is my code: {% set upperlimit = 3 %} {% for entry in entry.getChildren() %} ...
3
votes
1answer
190 views

Can setRouteVariables be in Services?

It seems that something like craft()->urlManager->setRouteVariables(array('calupdateResponse' => $out)); works in a Controller so that {{ calupdateResponse }} has a value, but it doesn't ...
3
votes
1answer
9k views

Variable variables in Twig

I'd like to create a variable from another variable. What I got right now is something like this: {% set language = fr %} {% if language == "en" %} {% set fooAddress = en_foobar.globalAddress %} ...
3
votes
1answer
1k views

Can't access plugin variable in a twig template?

So i've got this little plugin named AnnuaireAdherents with a variable that i try to access from within a template : annuaireadherents/ ├── AnnuaireAdherentsPlugin.php ├── services │   ├── ...
2
votes
3answers
31k views

Can I set a JS variable to a Twig output tag?

This came out of a previous question I need to set a variable of a tag group that I can reference in jquery for use with a plugin. I'm declaring this variable in a tag so my included js files can ...
2
votes
2answers
1k views

Remove white space and parenthsis from global from entry variable

I am using a phone number in a global variable called {{ global.phone }} with the format "(555) 555-5555". I'd like to use that same variable/string and remove spaces, dash, and parenthesis to use for ...
2
votes
2answers
147 views

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

When providing plugin users with variable containing arguments like {{ craft.pluginname.variable(arg1, arg2) }} what's the best practice for returning something that doesn't throw an Internal Server ...
2
votes
2answers
963 views

How can I iterate through an array of field handles for an entry?

Background: One of my templates has up to 6 fields that should each go into the same markup. Here's an example with a courseDesign field: {% if entry.courseDesign|length %} <section class="col-...
2
votes
2answers
574 views

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

I want to show entries according to an array. This is my code: {% set navsubids = craft.amNav.getNavRaw("hoofdnavigatie", {startFromId:25}) %} {% set navsubidarray = [] %} ...
2
votes
1answer
130 views

SET versus FIND

As I understand, I have (at least) the following two possibilities to list entries of a channel: Possibility 1: {% for entry in craft.entries.section('example').find() %} Possibility 2: {% set ...
2
votes
2answers
1k views

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

I'm working on a site with a structure containing products that are nested within "category"(not actually using craft's categories, just an entry type) pages. On each category or sub-category page I ...
2
votes
1answer
2k views

Can you pass a variable with includeJsFile?

Can you do something similar to using "with" in twig's include tag (http://twig.sensiolabs.org/doc/tags/include.html), but instead with includeJsFile? I am using a plugin to filter my entries by ...
2
votes
2answers
298 views

Updating Array Variable In Extending Template

I have a layout template: {% set bodyClass = [] %} <body class="{% for class in bodyClass %}{{ class }} {% endfor %}"> {{ block('pageBody') }} </body> And a template that extends it:...
2
votes
1answer
2k views

Why are variables persisting inside of my for loop?

Is anyone able to tell me why variables which I've defined inside of a for loop are persisting and retaining their values on into consecutive iterations? In order to overcome this, I've had to set ...
2
votes
2answers
8k views

Can I call methods on an object represented by a string variable?

I tried the attribute twig function for this but that did not work. Essentially, what I'd like to achieve is the following: {# This string variable is a name of the object #} {% set myForm = 'form' %...
2
votes
1answer
338 views

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

In my Craft 3 plugin I would like to pass a variable defined in my Controller to my PluginVariable.php file. Is this possible?
2
votes
2answers
2k views

Filter entries by Field value using a variable

How do I filter a list of entries by the value of a field using the value of a variable? e.g. {% set location = block.locationVacancies %} {% set entries = craft.entries.section('careers').search('...
2
votes
2answers
300 views

Set Craft variables in the markup

I am trying to use Twig methods/loops while working on a design/template. To avoid touching the CP or the DB i did set variables directly in the template, like : {% set service = { 'titel': ...
2
votes
1answer
504 views

Macros, Variables and Includes

Trying to get my head around this and make my code a little DRYer. I have a _helpers macro file. Like so: {% macro date_formatted(givenDate) %} {{ givenDate|date("l, jS F Y") }} {% endmacro %} {...
2
votes
1answer
91 views

Adding a random entry to an array

I have a navi on which one entry consists of a random channel entry from one section among standard singles: Single 1 Single 2 Random Channel entry Single 3 I thought about something like this: {% ...
2
votes
1answer
1k views

Trying to set a dynamic twig variable using craft.request and a craft global

I'd like to understand how to combine these two elements into a twig set vars. -- Context: I'm building a dynamic template that will list entries from which ever section the URL segment loads. For ...
2
votes
1answer
255 views

Influence Twig parse order?

I'm working on a plugin where I set a variable from my template. The plugin also provides a Twig filter that makes use of that variable. {% do craft.myPlugin.setVar('example') %} {# Filter appends '...
2
votes
1answer
428 views

Setting Variable inside of Matrix

I'm not sure why this isn't working as I set the variable outside the scope of the matrix block. The variable is working inside the matrix block, but not accessible outside of it. What am I missing? ...
2
votes
1answer
148 views

Pass aspect ratio as variable into imager

I use imager to transform images and I want to pass the image ratio as a variable: {% set customRatio = 1/1 %} {% set image = craft.assets().one() %} {% set transformedImage = craft.imager....
2
votes
1answer
307 views

How can I dynamically set checkboxes to be named variables?

If I have a field of checkboxes, I'd like to be able to access them by the name of each checkbox as the variable name, and the 0 or 1 checked state as the value. Said variables need to be accessible ...
2
votes
1answer
203 views

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

How can I use the shortener plugin to get and shorten the current URL? I've tried both twig and plugin methods shown in the docs, but the twig method outputs the FULL url, and the plugin method doesn'...
2
votes
1answer
119 views

Spitting out the URL of the entry

Bear with me. Shortly changing a site to run over https vs http - the issue is though that we need to maintain the Facebook open graph share data. Facebook makes distinction between https and http. ...
2
votes
2answers
250 views

Is it possible to use a loop variable to target a specific field entry?

I'm looping through the rows of a table field and hoping to target a specific field entry based on the loop.index. Here's a simplified example of what that might look like: {% for row in entry.table %...
2
votes
1answer
1k views

How do I reference a variable/expression inside an expression in twig templating?

This is the desired code <img src="{{ asset.getUrl('newsImageHalfWidth') }}"> Which works. However I want the transform name to be replaced by the contents of entry.newsImageSize So in a ...