Jump to main content Jump to doc navigation

What is a Template Variable?

A Template Variable (TV) is a custom field, or more specifically, it is custom field for a MODX Resource. TVs are used to extend the default attributes available for a Resource (e.g. for a Page or WebLink). A normal MODX Resource has a certain number of default fields: pagetitle, content, description, etc. If you need to add some custom fields to your pages, e.g. a second content area or a dropdown list of month names, or any other bit of custom data, you do this by adding a Template Variable to your template. MODX allows you to have a virtually unlimited number of TVs.

Why is it called a Template Variable Other content management systems call these simply "Custom Fields", so why does MODX call these "Template Variables"? Well, any time you have customized content, you will also have customized templates. Your MODX templates already have placeholders for content or longtitle because those are the built-in fields for a standard MODX "page"; the information and the template used to display that information go hand in hand. If you have a manager form with a field for entering a special date, then it stands to reason that your HTML template that you use to display that page would also have a bit dedicated to displaying that date. Likewise, you wouldn't build HTML templates with divs and tables for formatting bits of data unless the manager offered some way to edit that data. So the content is inexorably tied to the template, thus the name Template Variable.

When a Resource is displayed on the web, TV tags are replaced with the actual value entered by the user. TVs are Template-specific, meaning they can only be used in Templates that they are assigned to.

Template Variable Output Filters make it easier for users to add special visual effects to their web sites. With just a few clicks you can add an Image, URL or custom render to your website.

Usage

Let's say we have a TV named 'bio', that is a textarea TV that we've created. We've assigned it to our 'Biography Pages' Template, and want to show it on our page. To do so, we'd simply place this tag into our templates:

[[*bio]]

To add a TV to a page, you have to think back to its template (these are Template variables, remember?). Make sure you've defined the TV and attached it to the template that you're using. See the page on Creating a Template Variable.

Advanced Usage

TVs can also have Properties. Say you had a TV called 'intromsg' with the value:

Hello [[+name]], you have [[+messageCount]] messages.

You could fill the data with the call:

[[*intromsg?name=`George` &messageCount=`123`]]

Which would output:

Hello George, you have 123 messages.

Output Filters are also great tools to be applied to TVs. Say you wanted to limit a TV's output to 100 chars. You'd simply use the 'limit' output filter:

[[*bioMessage:limit=`100`]]

See Also

  1. Creating a Template Variable
  2. Bindings
    1. CHUNK Binding
    2. DIRECTORY Binding
    3. EVAL Binding
    4. FILE Binding
    5. INHERIT Binding
    6. RESOURCE Binding
    7. SELECT Binding
  3. Template Variable Input Types
  4. Template Variable Output Types
    1. Date TV Output Type
    2. Delimiter TV Output Type
    3. HTML Tag TV Output Type
    4. Image TV Output Type
    5. URL TV Output Type
  5. Adding a Custom TV Type - MODX 2.2
  6. Creating a multi-select box for related pages in your template
  7. Accessing Template Variable Values via the API

Support the team building MODX with a monthly donation.

The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.

Backers

  • modmore
  • STERC
  • Digital Penguin
  • Jens Wittmann – Gestaltung & Entwicklung
  • Fabian Christen
  • Dannevang Digital
  • Sepia River Studios
  • CrewMark
  • Chris Fickling
  • deJaya
  • eydolan
  • Following Sea
  • Lefthandmedia
  • Murray Wood
  • Anton Tarasov
  • Stéphane Jäggi
  • Raffy
  • Snow Creative
  • A. Moreno
  • Nick Clark
  • JT Skaggs
  • Helen
  • YJ
  • krisznet
  • Richard
  • Yanni

Budget

$311 per month—let's make that $500!

Learn more