February 2011 Archives

MODx File Elements Mirror: Working with snippets, chunks, templates and version control

Most of you out there working in teams are most probably using a version control system of some sort to manage your files in projects. Once you get used to the workflow and peace of mind it brings - that every change is marked, commented and reversible, it's pretty hard to not like it. This brings us to the long discussed topic of how MODx stores its snippets, chunks and templates (amongst other things) in the database (see http://modxcms.com/forums/index.php?topic=54902.0).

At butter, we've dealt with this need to have version control over our elements by creating an 'include' snippet, which allows us to create elements in the manager that merely serves to include the code from a file.

example of using the include snippet

This Works well until you start to creating a lot of elements, which other team members would have to do the same for each one on their own development platform. Which is why we wrote the File Elements Mirror plugin - to find these files automatically and create the elements for you.

We usually structure our elements in a separate folder like this:

web_assets/
    chunks/
        optional_sub_dir/
            chunk_name.html
    templates/
        template_name.html
    snippets/ 
        snippet_name.php 

The plugin will go through these directories, creating the necessary elements (chunk/snippet/template) for your, as well as creating/assigning it to a category if there's an optional sub directory in the folder structure. Remember to tell the plugin your directory structure:

Settings for the fem plugin

Unlike our manual approach, these elements are standalone - they don't use include statements to the original file, but the plugin will update these elements everytime its run, effectively mirroring the contents in the files.

Some limitations:

  • If you rename or remove your file element, you'll have to manually remove it from MODx.
  • The plugin is triggered by the OnHandleRequest event, so you would want to disable the plugin in staging/production environments.

You can get the plugin package here. Happy coding!

- The Ninja

Tags: MODx Addon, FEM