Template Variables
From MODx Wiki
Contents |
Overview
Template Variables, commonly known as TVs, allow you to add extra data fields to your documents beyond the default fields for "Title", "Long title", and "Document content". The official documentation didn't leave me with a good understanding of what exactly what these were, so I wanted to elaborate on how you might use them. In the MODx manager, when you edit a document, you get to alter the basic page attributes: Title, Long title, Description, alias, etc. and most of these tie back to a specific place-holder that you can use in your templates. For example, the text you have in a page's "Title" field can be used in a template by utilizing the [*pagetitle*] placeholder. That placeholder will be parsed and replaced with the value from that page. It is a built-in template variable.
Examples
What if you aren't satisfied with the built-in options? My first discovery of a MODx "limitation" was realizing how the CMS handles the META keywords... instead of letting me type a comma separated list of keywords, I had to navigate through the Resources-->Manage META tags and Keywords. Then I realized that MODx was replacing the opening <head> tag in my HTML document by doing something like this:
This: <head> Becomes: <head><meta name="keywords" content="my, comma, separated, list, of, values">
I found that to be really weird given that the rest of MODx is straight-forward find/replace templating. What I really wanted was a simple text field where I could type in my keywords and be done with it. *I* wanted something like this in my template:
I wanted this: <meta name="keywords" content="[*my_meta_keywords*]"/> To become this: <meta name="keywords" content="my, comma, separated, list, of, values">
Well, aha... MODx DOES allow for this sort of thing. You can extend any of the built-in template variables by creating your own. In this case, I can head over to Resources-->Manage resources-->Template Variables and I can create a New Template Variable. I can choose the name of the placeholder, e.g. [*my_meta_keywords*] and I can choose the input type (e.g. text). The "Caption" is for display purposes... just like the page title has a caption that reads "Title", your new template variable will have a caption so that anyone entering data in this new field will know what it's for.
Be sure to check at least one template under the "Template Access" area -- this is what associates this variable with the templates. Once you have saved your template variable, edit a page that uses the template that you associated with. E.g. if you associated the template variable with the "MODxCSS Wide" template, edit a page that is using that template (or change the template on a given page to use that template). Then once you edit a page that's using that template, you'll see that you have a new field available to you under the document content.
FAQs
I have created a TV, but it does not show when I edit my document
- Make sure that you have enabled the TV for the template your document is using. Go to the Resources-->Manage resources-->Template Variables and make sure you have checked the box for the template your page is using.
- Make sure you spelled the place-holder correctly.
Links
- From the MODx Documentation: What is a Template Variable
- From the MODx Documentation: Creating a Template Variable -- this is good. It shows how you can create your own drop-down lists, etc.
