1

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 has what it calls "Globals" but i don't see where I can add a custom function for each global variable. Am I looking at this all wrong?

Or is everything done directly on the template? I am developing using jade, which is then compiled into the final html template. So running into errors adding the twig templating language into my jade templates.

  • You can't use Craft without using Twig syntax in your templates. Were you able to use jade in your "expression engine mode"? – carlcs Jan 21 '15 at 16:19
  • I feel like there is about a dozen questions in there, but I'm not sure what any of them are asking. Can you try and clarify what you're attempting to do? – Brad Bell Jan 21 '15 at 18:35
3

I'm not familiar with Jade so I can't speak to that, however I would suspect that even if Jade did allow Twig syntax it would be mostly redundant.

Regarding variables. There are a number of ways to create variables in craft.

  • Twig Variables - defined using {% set variable = 'value' %}, which can be printed using {{ variable }} or otherwise referenced by name. You can also assign a block of code to a variable using {% set variable %}...{% endset %}.
  • Globals - global fields defined within the CP which can be accessed in your templates using {{ globalSetHandle.fieldHandle }}.
  • Environment Variables can also be defined in your config file (although these are generally reserved for variables that are unique to a particular environment, i.e. 'BaseUrl' ).
  • Plugin Template Variables - defined in php in your plugin's variables files

You might also want to look at Twigs extends, include, and embed methods.

I'm not exactly sure what you mean by "custom function for each global variable". Twig has some methods and functions that allow you to manipulate data (see twig functions and craft functions), as well as macro functionality for creating reusable code. Although if you are doing anything overly complicated it might be best to create a custom plugin.

  • Thanks Douglas. Can you elaborate how to set up globals? I assumed it worked like low variables, but can't seem to understand how to do the same. After creating the global ( name & handle ) I am not exactly sure what to do next. Do i create a text field where I define the variable? – juju Jan 21 '15 at 22:40
  • You first want to set up fields to hold your data (i.e. 'myTextField', 'myOtherTextField', etc.). Then create a new 'global set' under 'settings->globals->create new global set'. Then add the field(s) you created to the field layout tab of the global set. Then save. With at least one global set defined, you will get a new tab at the top of the control panel called 'globals'. Select that tab, select the new global set, and enter data into your field(s). – Douglas McDonald Jan 21 '15 at 23:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.