The MODx Content Management System

What is MODx?

MODx is a content management system that helps you create superlative web sites. There are two key benefits to MODx. First, it makes web sites easy for you and others you designate as site admins to maintain. Second, once you get used to it, it allows you to create very complex web sites in a very short time. Things that would take many lines of code for you to do yourself can often be accomplished with a line or two in MODx. Of course there are many other benefits as well, but we'll get to those later.

There are other content management systems that allow you to create a web site rapidly and maintain it easliy, but they tend to restrict you in many ways. Working with such a CMS can make you feel like you're bound up in duct tape. MODx, on the other hand, is flexible and open. You can extend it easily in almost any direction and create exactly the web site you want with no compromises. For this reason, MODx is often described as a "content management framework" rather than a "content management system." Because of the power and flexibility of MODx, it can be a little hard to get started but, once you get the hang of it, it's amazingly easy to use.

You may be anxious to install MODx, but first, you might want to know how MODx works and what features would make you want to install it in the first place. If you like to jump right into the action, you can go here to install MODx and start playing.

How does MODx work?

As with many other content management systems, MODx stores the data for your web pages in a database. When a user visits one of your pages, MODx goes to the database, gets the information, assembles it, and sends it off to the user's browser for display. This all happens behind the scenes. Many MODx users create sites without ever dealing with the database or the process of saving and retrieving the information. To really make MODx sing, however, you'll eventually want to learn about how some of the behind-the-scenes processes work.

Extending the MODx framework are, among other things, Templates, Snippets, Chunks, and Plugins. They're much simpler than they may sound and we'll get to those in a minute, but first a little bit about the MODx Manager.

Getting Started

The MODx Manager is where you work on your web site. One of the great things about MODx is that you can work on your site from anywhere you have a web connection. The Manager is a tool stored at your site and, once you log in, you can add content, edit existing content, and do almost any task that you could do at home. Everything you need is in the Manager so the only thing you need on the computer you connect from is a web browser.

In the Manager, you can edit your pages in a rich text html editor or, if you prefer, you can edit the raw html of each page. You can upload images, control who gets to see and/or edit each page, add new pages, re-organize your menu, and much, much more. Go here to learn more about Getting Started with MODx and using the MODx Manager. If you're new to MODx, however, it's a good idea to finish reading this page before going to the Getting Started link.

Documents

A web page, in MODx, is the finished product that MODx sends off to the user's browser when that page is requested. A MODx document is the heart of that web page and contains the content that appear only on that page (like the paragraph you are reading now).

Unlike the documents on a regular web site, MODx knows a lot about each document. Rather than storing the page as a file on the web site, MODx stores the document's content (and information about it) in the MODx database. When a user visits your site and the browser requests a page, MODx retrieves the appropriate document from the database, gets the template assigned to that document, assembles everything, and sends the finished product to the browser for display. For more details about the nuts and bolts of this process, see Susan Ottwell's excellent explanation here.

Because MODx is pre-processing requests for pages, it can do a lot of tricks with the page before it is sent to the browser like inserting a menu, breadcrumbs, recent blog posts, or news items; showing a random photo; filling in a standard header, logo, or footer; and adding boilerplate content at various places on the page.

When you create a document in the MODx Manager, you can specify whether you want it to appear on the web site or remain hidden, how it will be referred to in the menu, where it will be placed in the menu, who can see it, who can edit it, the dates you want it to appear and disappear from the site, and other details about the page. Many of these page attributes are optional so, if you just want to create a page and let everyone see it, you can ignore most of these settings and just type in the content of the page. Go here to learn more about creating and editing MODx documents.

Templates

A template in MODx is just a place to put the stuff that you want on every page or a subset of pages. Typically, a template will contain the banner that goes at the top of each page, the footer that goes at the bottom, a menu for all or part of your site, and any other content that should appear on every page that uses the template. You can have as many templates as you like, but many sites just use a single one so that all the pages have the same look, layout, and style. Sometimes, people create special templates for things like upload pages, blog pages, photo gallery pages, etc. You might also have a special template for your home page although often, it's not necessary.

You might think of a template like the letterhead used by a business. The letterhead will almost always have a standard header and might also have a footer and sometimes a sidebar. When the business sends a letter, the content of the letter appears inside the standard elements of the letterhead. When you create a typical MODx document, you type in the content and save it. In the template for that document, you put the MODx content tag, [*content*], where you want that content to appear. When a user visits the page, MODx gets the template, plugs in the content where it belongs, and sends the page off to the user's browser for display.

You can also put some of the other information from the document into the template if you like, such as the title, longtitle, publication date, author, etc. These are template variables that you type in when you create the document. They're stored with the document in the database.You can even create custom Template Variables to hold extra information related to the document and place those in the template as well.

On this page, for example, the template contains the page title (what you see at the very top of your browser window). It's generated from the site name and the title of this document: ("The MODx CMS"). It also contains the header and footer, and the snippet calls for the breadcrumbs you see just below the banner and the menu at the right side. It also contains the main heading you see at the top of the page, which is generated automatically from the document's longtitle. The document's content is just the subheads and paragraphs of text on the page.

When I create a new page for this site (by creating a new document). I just tell MODx I want a new document, type in the page content, and fill in the template variable fields. The page appears automatically in the breadcrumbs and menu and the title and main heading are generated automatically by MODx.

There will eventually be link here to a page on templates and template variables. Until then, see the Getting Started page for more information about templates and template variables or see Susan Ottwell's great explanation of how MODx processes templates here.

Chunks

What if you have some content that you want on many pages but not all of them, and you want it in different places on different pages? That's where chunks come in. A chunk is just a piece of text or html code that you can place anywhere on any page. It can contain text, images, links, snippets, JavaScript, or anything else that can go on a web page. You create the chunk in the MODx Manager and simply put a reference to it on any page in the position where you want it to appear.

Say you want to put a paragraph of text on a some of your pages. You just create a chunk. Let's call it MyChunk and put the following text in it:

<p>This will appear wherever I want it to</p>

Then, whenever you want that text to appear in a page, you simply put {{MyChunk}} at the spot where you want it. Chunks can be placed in headers, footers, templates, or in the content of a document. Go here to learn more about MODx chunks. Chunk names are case-sensitive so be sure to use the same case in creating and using any chunk. Chunks can contain snippets, but they don't have to.

Snippets

What if you want to have some dynamic content on a page? Maybe you'd like to read a list of files from a particular directory, sort them alphabetically, and put them on the page as links. Files are always being added to the directory so you can't just create a file list and put it on the page (well, you could, but then you'd have to edit the page every time someone uploaded a new file) You'd much rather generate the file list dynamically from the actual contents of the directory. That way it would always be up-to-date. To do this, you'd write a bit of PHP code to read the directory and print out the HTML code for the list, put that code in a snippet, and just put a reference to the snippet on the page where you want the file list to appear with a line like the following:[[MyFileListSnippet]]. If you need to tell your snippet something about how to operate, you can have parameters in the snippet call like this:

[[MyFileListSnippet? &sortOrder=`ascending` &fileDirectory=`assets/mydir`]]

The ampersands indicate MODx parameters. Parameters are just pieces of information that can be sent to a snippet. Parameters can be optional or required. These two tell the snippet that you want an ascending sort order and that the files to be displayed are in the "assets/mydir" directory. In the snippet's PHP code, these can then be accessed as regular PHP variables ($sortOrder and $fileDirectory). Many snippets also allow tpl parameters which are mini templates that allow you to format and style the snippet's output. Often, a tpl parameter is actually a chunk that holds formatting information.

Go here to learn more about snippet parameters.

If you don't know PHP, don't despair. MODx already has a number of built-in snippets that can do a lot of what you want. New snippets are being created by the MODx developer's community all the time. The Wayfinder snippet creates menus like the one at the top of this page. The Breadcrumb snippet creates breadcrumb-style navigation lines. The Ditto snippet is a powerful tool that lets you show collections of things like blog posts, document summaries, and much , much more on any page. The MaxiGallery snippet lets you create and manage photo galleries. The SPForm snippet creates a simple contact form like the one on this site's Contact Us page. The eForm snippet creates more complex forms that can be used to register users, collect data, and store the data in the MODx database. In each case, the job is accomplished with a single snippet call. Tasks that would take many lines of code on a regular web site can very often be done with a single snippet call in MODx.

Click here to learn more about snippets. Snippet names are case-sensitive so be sure to use the same case when creating and using snippets.

Chunk or Snippet - Which do I use?

Chunks and snippets are both used to insert content in a web page. For new users it can be confusing to figure out which one to use when. There's a pretty simple rule of thumb, however, for making the decision. If you want to insert a piece of content on a number of web pages, the content contains no PHP code, and you don't want to put that content in the template, you want a chunk. For example, you might want a disclaimer telling users that you won't be responsible for damage caused by following the advice on a page. You don't want this in the template because it won't appear on every page and it will appear in different places on different pages. A chunk is perfect for this. If you change your mind about how to word the disclaimer, you can just edit the chunk and the text will change on every page that contains that chunk.

Chunks can't contain PHP code, only HTML and the usual items you'd put on a MODx web page. Chunks can contain flash graphics, JavaScript, images, tables, paragraphs of text, snippets, and anything else you'd put on a standard MODx web page. When you create a chunk you can place the chunk call like this {{MyChunk}} on any page. When MODx sees the chunk call, it simply gets the contents of the chunk and plugs it in where it found the chunk call. Chunks usually load faster than snippets because MODx doesn't have work as hard to interpert them.

The header and footer of this page are actually chunks that I inserted into the template. I could have put their content directly into the template without using chunks, but having them in separate chunks means I can edit them without worrying about messing up the rest of the template.

Go here to learn more about creating and using chunks.

A snippet, on the other hand, is for when you want dynamic rather than static content. It might contain PHP code or Javascript code (or both) and it creates content "on the fly" to be inserted into the page. The drop-down menu at the top of this page is created by the built-in MODx Wayfinder snippet. The output looks a little different on every page. The Wayfinder snippet uses PHP code to figure out where you are in the site and generates the appropriate string to be output. Go here to learn more about creating and using snippets.

Modules

Modules actually extend the MODx ManagerModules are an advanced topic and most MODx users will never create a module although they use some of the existing modules like the MODx Doc Manager and the editor used to create document content.

If you need to create a module, one method is to look at the code of the existing modules. It will take you some time, but eventually, you can understand how it's done and create your own. There is also Susan Ottwell's excellent guide to creating modules here.

Plugins

Plugins are another advanced topic and, again, most MODx users will never create one. Plugins are used to interrupt the processing of the MODx engine and add custom functions. There are a number of hooks into the MODx engine that let you perform just about any kind of operation at key points in the processing. You can, for example, have access to a document's content just before it's rendered as a web page. You could translate it, emphasize key words, turn some items into hyperlinks, etc, etc. You can do anything you can write code for. You can also process documents before they're saved, process a user's information during the process of logging in or do any number of other transformations. The beauty of plugins is that they allow you to put custom processing in place without hacking the MODx core so your work will be unaffected by upgrading to newer versions of MODx.

 

Thank you for visiting BobsGuides.com

  —  Bob Ray