Exploring MODx From the Inside: Using Chunks

Creating and Using a Chunk

As has been described before, a chunk is simply a block of HTML code, in the same manner as a template. Chunks are stored in the site_htmlsnippets table in the database. They can contain any elements that a plain HTML document can have as well as MODx tags, including other chunks. When the document is being processed, the chunk's contents will be inserted into the document at the point the chunk's tags were placed. Any MODx tags in the chunk's content will be processed during the next pass through the parser.

Let's go make the footer chunk for our template. Log in to your Manager, and from the main menu go to Resources -> Manage Resources. Open the "Chunks" tab, and click on the New Chunk link. As in creating a template, a form will be displayed with a few text fields and a textarea field.

Create Chunk

Give the chunk the name of Footer (it must match the name used in the {{Footer}} tag exactly), and a brief description; perhaps "container for site footer content". The Category is not required, although it does make things easier when you have a long list of chunks and want to edit your footer. If the category "Template" doesn't already exist in the Existing Category drop-down, add it by putting Template in the New Category field.

For editing the chunk, you can choose to use any editors, such as the default TinyMCE Rich Text Editor, that is available in your installation. There is a select drop-down under the chunk code field to select the editor to use.

Select Chunk Editor

In the Chunk code field, put something like this:

<p>&copy; [!CopyYears?startYear=`2008`!] My Site | Content managed by <a href="http://modxcms.com" target="_blank">MODx</a> | <a href="[~42~]">Contact Me</a></p>

As you can see, a chunk can have HTML code and MODx tags. It could also have any javascript tags that your page uses. Many sites have a site utilities menu in the footer; Wayfinder snippet tags would be added in that case. Here we use a link tag, where 42 is the ID of your contact form document. We also used the CopyYears snippet, which will automatically keep your copyright date current with an output such as 2008-2009.

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.