Questions tagged [variable]

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

Filter by
Sorted by
Tagged with
1
vote
1answer
28 views

global variable {{ siteUrl }} in multilingual sites

In multilingual sites the global variable {{ siteUrl }} should not point to the URL with "/de/" if it is included in the templates head section for pointing to a global js-file or css-file (...
0
votes
0answers
16 views

Variable scope in Element API

I am using the Craft Element API and getting a bit muddled over PHP variable scopes. I was hoping if someone could please explain why this does not work: $isProd = App::env('ENVIRONMENT') === '...
0
votes
0answers
14 views

Error when trying to access 'variables' inside the PDF template in Craft Commerce

I'm trying to include the custom image field inside the PDF file of Craft Commerce. I added the following code to the module: use craft\commerce\events\PdfEvent; use craft\commerce\services\Pdfs; ...
1
vote
1answer
43 views

How to directly access translated variables by language

I have a localized, two language site (en and fr) that I have a translated global variable set for. My question is: is there a way on a Craft 3 template to directly access a translation of a variable ...
0
votes
1answer
43 views

currentUser gets cached

I'm having a problem with navigation that seems to get cached. If the page is loaded by someone who is not logged in, it shows the login button in the navigation (and it should). Once the user is ...
2
votes
1answer
240 views

Error using environment variable for email setting

I'm trying to create an environment variable for email settings, but it doesn't work. Here is my email setting at config/general.php 'dev' => [ '@host' => 'host', '@port' => '587', ...
1
vote
1answer
456 views

Incorrectly setting module template variable

Getting error: Calling unknown method: craft\web\twig\variables\CraftVariable::markAsReadModule() Trying to use a template variable that I created in my Craft 3 module, I use this module for ...
1
vote
1answer
249 views

Pass Craft Field String to Javascript Function

I tried different solutions given here, here and here. But I get nothing to work, probably missing something small and simple. I have this variable in a .js File: var dziFilesUrl = 'http://...
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....
13
votes
4answers
50k views

How to check variable type?

How do I check what type my variable of (string, integer, boolean, array, dictionary, etc)?
1
vote
1answer
48 views

Field removed from entry layout is still displaying

So I'm setting my meta author tag this way: <meta name="author" content="{% if seoAuthor is defined %}{{seoAuthor}}{% else %}{{globalSeo.seoAuthor}}{% endif %}"> seoAuthor is a field that ...
1
vote
1answer
349 views

Redirect to template from controller and pass data

I am building a custom login process which follow these steps: Request user email (submits to custom controller) Check if user has an account If they have an account, show a form requesting their ...
0
votes
1answer
317 views

Passing variables into twig components

Based on this interesting article: DRY templating with Twig and Craft CMS. I found myself trying to update the approach with include instead of macro following reading I had about twig 2 and craftcms ...
1
vote
1answer
374 views

How do I show errors for a new front end entry form?

I'm using a front end form so logged-in users can create a new entry ("post") or edit an existing entry. Here's the controller for figuring out whether it's a new or existing entry: {# Get the entry ...
1
vote
1answer
81 views

Need help trying to pass a variable of a related entry

On my page, I have a form which is using Freeform (Solspace). I'm attempting to setup a custom redirect URL for the form. The form will apply to numerous entries in a channel and some will require ...
1
vote
1answer
170 views

Passing optional variable values into an include template

Craft noob here. I have an include file which I'm calling a "component" so I can reuse it across the site. The component is set up like this. <h2>{{ heading }}</h2> {{ bodyContent }} {% ...
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 "...
1
vote
1answer
93 views

Craft 2 - Returning Value in Variable from Plugin to Template

I am working with a plugin and wish to return a value from it to the template that calls it. In my plugin, there is an XML feed call, and I would like to pass a value(variable) from the plugin to the ...
1
vote
1answer
83 views

Pass last segment to solspace freeform field

Using freeform on another CMS, I was able to pass the last segment of the previous page url (when navigating to the form page) to a form field in the freeform composer, is this feature possible in the ...
2
votes
2answers
299 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:...
1
vote
1answer
112 views

SEOmatic and Global Variables

We have a Craft site with nearly 800 entries. For SEO purposes we need to change the page title and page (SEO) description for each entry, a laborious task to say the least. Is there a way to have, ...
2
votes
1answer
339 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('...
1
vote
1answer
1k views

Twig default filter or Null coalescing operator?

When testing for a variable in a template these two appear to do the same thing. I've read CruftlessCraft's article on How to check for undefined variables but can't see an explanation of why I should ...
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.
1
vote
1answer
409 views

Passing Querystring Variable to PHP Code in Plugin

I'm pretty new to Craft and PHP, and I am needing to design a page in Craft (powered by a plugin) which will pass a variable in a querystring (or equivalent in Craft) to the PHP code in the plugin. In ...
1
vote
2answers
51 views

Using variable references to fields in a template

I have a situation where Matrix within Matrix would be ideal, but instead we've ended up with a Matrix row that has pairs of fields up to a maximum of 6: quote1 attribution1 … quoteN attributionN … ...
1
vote
1answer
50 views

stashing variables?

I have an entry id in that I am storing in a variable that I would like to access outside of this for statement, is there a way I can "stash" it somewhere? I want to do another query based on tags but ...
1
vote
1answer
4k views

Twig variables wont work in an include js file?

Im trying to put my code in a included theme.js file. It wont recognize my variables anymore. Working in .rain file within the html code: <script> jQuery(document).ready(function(){ var ...
2
votes
2answers
964 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
1answer
264 views

Specify a field dynamically using section handle

I'm using a single template to display entries from several structures. Each structure has a cooresponding global field that is an entries field. I use this to display related entries as a sidebar. ...
0
votes
2answers
2k views

Include with -- variable does not exist error

New guy here. Can someone explain what the proper way to pass a variable into an include? I'm using this: {% include "singlepage" with { currentPage:'leaders' } %} .. and I'm getting a Variable "...
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 ...
1
vote
1answer
542 views

How can I access a template variable from an include that is loaded via ajax?

I am making an ajax request to an entry. It is returning the entry's html - and all is working great. I have a section called "portfolio" and I loop through each: portfolio/index.twig {% set ...
1
vote
1answer
569 views

Can't seem to get variables set in controllers

I think I might be confused by https://craftcms.com/docs/plugins/controllers#posting-to-controller-actions I have a controller that I want to return a model when validation fails: $post = craft()-&...
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 ...
1
vote
1answer
1k views

How to get Current Entry ID in Plugin Variable?

I need to get the current entry ID in plugin's variable file without submitting any form. I tried this but it gave an error Undefined property: Craft\PluginNameVariable::$element namespace Craft; ...
1
vote
1answer
1k views

Variable "entry" does not exist

This is very basic as I am new to Craft CMS, but I created a Plain Text field in the Homepage section labeled "Tagline". When I enter the Twig code {{ entry.tagline }} into the index.html template, I ...
1
vote
1answer
95 views

Include Global Set Variable in Print Delimiters

I have a site which has another group site - both use same templates but I want to include different phone number in the header of each. I was wanting to use Global Set Variables to pull this ...
1
vote
1answer
5k views

Defining Variables

I am getting myself confused with how to define variables in craft. I am thinking in expressionengine mode where I can define a custom global variable to be used later in my templates. I see craft ...
2
votes
1answer
148 views

$craftPath multiple enviroments

I am new to Craft and am trying to figure out what the best way to set the $craftPath PHP variable to something like the other configurations for multi-environments. Locally I have it set up the ...
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
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
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': ...
1
vote
1answer
439 views

Variable for User Field

How do I go about using a dynamic value to access a user field value? I have created a form builder for users and dynamically populate the user field based on the question's associated field. <...
1
vote
1answer
445 views

Reuse chosen dropdown field value on frontend

I would like to choose entry values from a frontend dropdown and reuse the chosen value directly. {% set entries = craft.entries.section('mountainbikes') %} <select name="year"> {% for entry ...
1
vote
0answers
42 views

Trying to display post based on two variables

I am setting three variables like so ... {% set featureEntryIds = craft.entries({ section: 'blog', featurePost: 1, limit: 1 }).ids() %} {% set articleClass = entry.id in featureEntryIds ? 'featured' ...
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....
1
vote
1answer
226 views

craft()->urlManager->setRouteVariables() loads the correct template but does not pass in the variables

everybody! setRouteVariables() doesn't appear to be working for me. If I boil my controller function down to just this: public function actionSubmitForm() { craft()->urlManager->...
1
vote
1answer
153 views

Why two equal variables give false statement?

I have two equal variables comparing each other which give me false. One variable is set out of ancestor entry in structure and another one out of ancestor entry as well. Both variables are set a bit ...