Exploring MODx From the Inside: How MODx Template Variables Work

How MODx Template Variables Work

Template Variables are one of the most powerful features of MODx dynamic content generation. At their simplest, they can be considered extensions to the existing document variables, such as pagetitle, longtitle and content. Custom fields, as it were. But they can go far beyond that, querying the database or processing PHP code.

TVs are extra tables in the database, with a selection of various types of extra fields added to the document editing form as TVs are created. They also have a choice of output formatting "widgets" to control the appearance of their output in the final web page.

TVs are connected to templates (hence the name Template Variable). Any document that is assigned the same template will have the TV's extra fields in the Template Variables section below the main Document content editing area. Access to a TV can be controlled by the same security measures used to control access to documents, which will be covered in detail in a later chapter.

The order in which a template's TV fields will appear in the document editing form is managed by a drag-and-drop interface in the Templates tab of the Resources -> Manage Resources main menu item. When you select a template to edit, you will see the Assigned Template Variables tab. Opening that tab will show a list of the TVs assigned to that template. If you have more than one TV assigned to the template, you'll see a link "Edit the sort order of the template variables". This will open another page with the TVs listed in boxes. You can drag-and-drop the TVs into the order you want. This is on a template-wide basis; you cannot have the order different for different documents using the same template.

TVs are managed by three .php files in the manager/includes directory. These contain functions that are called by the parser when processing TVs.

TVs are a bit more complicated than chunks or snippets. The TV itself has a default value, which can take a number of forms, or even be left empty. Then each document using the same template can edit its copy of the TV to a unique value. If the document's TV field is not edited, it will use the default TV value. There is also the question of what output widgets were assigned the TV when it was created. Once the value of the TV for a given document is determined, it will be formatted according to the widget selected, if any.

TV output is positioned according to the placement of the TV tags, which take the same form as the default document variable tags; [*TVName*]. TVs can also be used as the value of parameters in snippet calls. They can be placed in templates, chunks and document content. They can be accessed in PHP code with a number of MODx API functions, most commonly $modx->getTemplateVar('TVName'), which will return the value of the TV for the current document.

Creating a TV

Let's start by creating a simple custom-field type TV to hold the keywords for each document.

In the Manager, go to Resources, Manage Resources, and select the Template Variables tab. Click on the New Template Variable link. This will open the TV editing form.

Keywords Template Variable

There are a number of fields to fill in:

That's all, so now save your TV.

Remember, the Input Type is for choosing what kind of field will show on the document editing form, and the Widget is for formatting the TV's output; what will show on the finished web page.

Using a Template Variable

There are two parts to using a TV. To begin with, let's place the TV where we want its output to appear on the web page. This is for having custom meta keywords for each page, so we'll want to have it in the template. If you've been following along with the other articles, you created a simple template while reading the last article. So go to the Resources, Manage Resources page and the Templates tab. Select your template to open it for editing. In the head, add a meta tag, using the TV tags of our new TV in place of its content:

<meta name="keywords" content="[*MetaKeywords*]" />

Now the default keywords will appear there for all of the pages using the same template. The next part is to edit the TV for each page.

Click on the page icon to the left of a document in the Document Tree (or right-click on its title) that is using the same template as your TV. Select Edit document from the pop-up context menu. When the document is opened for editing, scroll down past the main Document Content editing area to the Template Variables area. There you'll see a field for your MetaKeywords TV, already filled in with the default keywords we entered when we created the TV.

Editing a Template Variable

Edit the field to remove or add keywords. Save the document, and now when that page is being viewed on the Web, it will have the keywords you specified for that document rather than the default keywords.

Comment On This Article

Do you find something unclear? Did I miss something or get something wrong? What do you like or not like about this chapter? Please do not ask for help here; use the forums if you need help.

If you have trouble reading the code, click on the code itself to generate a new random code.
Mon May 17, 2010, 03:25:47
Eugh, finally someone made this simple for me!

Susan, your clarity of explanation and generosity in sharing information is such a valuable thing!

One thing I just wanted to check: in the "using a template variable" section you insert the TV , but if I haven't missed something I think you mean .

Thanks again

Mat
Sun January 31, 2010, 05:25:01
Agree with Freek, kick ass article :)
Thaks a lot
Fri January 15, 2010, 07:00:04
This is probably the best explanation I've read so far. I would love to see several more examples... an image, some coding, something unique that people may not have thought about but would be helpful in creating web pages.

Thanks for this.
Wed January 13, 2010, 19:46:22
Susan,

Very good. Many thanks :0)

modxcms handle,
Scotte68
Sun September 20, 2009, 03:10:54
Most helpful step-by-step, thank you..!

Freek, Amsterdam
Mon September 07, 2009, 02:05:35
I don't find the info on what type of output formating should I use when displaying keywords? Delimiter?