3

As I'm still a total n00b in working with CraftCMS, so I hope to get some help from more experienced users :-)

I followed every available tutorial (which are very clear and well written. Great job!) but of course there's still enough to explore and discover. The hardest part for me is to find the correct variables and the format to use in templates. That's what this topic is about ..

I've worked with Lightspeed HQ for some years, which is also a twig / symfony based platform. What made templating very easy in Lightspeed HQ is it's developer toolbar that shows all available variables in a wink. Here's an example of the toolbar, active on a product-page.

enter image description here

When I need to show prices for example, I simply can use this syntax:

{{ product.price.price }}

// or when I need price incl. and excl. taxes
<p>Price incl. TAX: {{ product.price.price_incl }}</p>
<p>Price excl. TAX: {{ product.price.price_excl }}</p>

[ADDED] I'd like to see (if possible) a dump of all variables available in the backend, and more important, the way they need to get called eventually. Maybe some example would explain it better:

Let's say that I'm starting with a clean / empty template and want to know which variables are available to use in the actual template. I have a slider, which should be dynamic. In that slider I need to call / show specific related product properties. In this case it should be the featured product image. I'm totally stuck in finding the right variable to call: i.e. {{ variableimageentry.getUrl }} for the source.

In the above example I constructed it this way: enter image description here

I was hoping that there was a way to create an overview of all fields' variables, ready to use in templates.

I came by the KINT plugin, but can't find a usable way to get something similar as my example. Maybe I'm overlooking something, so I hope someone could help me out here.

Thank you very much in advance for your help and thinking with me!

2
  • Thank you very much @MoritzLost for your explanation! Maybe I'm totally wrong here, but in this case all 'objects' are returned when they are already declared and working in the active template, right? That's actually not what I'm after. I'd like to see a dump of all variables available in the backend, and more important, the way they need to get called eventually. Maybe some example would explain it better: Let's say that I'm starting with a clean / empty template and want to know which variables are available to use in the actual template. I have a slider, which should be dynamic. In tha
    – Hielke
    Jan 1 at 12:05
  • I've updated my answer with some tips for your situation.
    – MoritzLost
    Jan 5 at 16:29
1

You can use the dd tag to dump the contents of a variable and exit the current request. This will allow you to see the type and contents of a variable:

{% dd entry %}

If the variable you're inspecting is an object, this will also show you its type. Once you know the type of the variable, you can look it up in the API reference to see what properties and methods are available on it. For example, the entry variable in entry templates is a craft\elements\Entry object. Of course, the API documentation doesn't know about the custom fields you have on your entries.

If you want to list all variables available in the current context of a template, you can use the special _context variable. This array contains all variables for the current context, the array keys being the variable names. You can dump them like this:

{% dd _context %}

This will result in a long list. A useful trick is to only dump the variable names using the keys filter:

{% dd _context | keys %}

This will dump a list of variable names. This list includes the global variables provided by Craft, some variables depending on the current route (like the entry variable on entry routes), as well as any variables provided by plugins or modules or defined by you in the template itself.

As you mentioned, you can also use the Kint plugin to use the dump methods provided by it, which is a bit nicer and easier to navigation than the dd output.


Update regarding question clarifications

If you're looking for a place where you can find every available property, method, field, attribute, etc for every component – that's just the API reference. Custom fields won't show up in the API reference since they're specific to a site.

Let's say that I'm starting with a clean / empty template and want to know which variables are available to use in the actual template.

Based on the current, a variable with the current element (for example, the current entry will be available in your template). That is the gateway to most properties and fields you're gonna need. Pre-defined properties (like the URL, slug, title, ID) can be found in the API documentation for the Entry model. Besides that, you can access any custom field available on the entry. You can find the handles for those in a number of ways:

  • Through Settings -> Sections, look up the section and then the entry type your entry belongs to.
  • In your user profile, check the checkbox Show field handles in edit forms to display the machine names of all fields in the edit form for the entry.
  • A handy one-liner to display a list of custom fields on any element:
{% dd entry.getFieldLayout().getFields()|column('handle') %}

Some fields will contain queries to get related assets. For example, any asset field will return an AssetQuery that you can use to fetch the related asset. Make sure to read the documentation on Element Queries to find out more.

Once you have the related asset (in this case, the product image), you can use dd to find the class name to look up in the API reference. In this case, you're looking for craft\elements\Asset where the getUrl method is documented.

0

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.