Static elements

Static elements Website development
A static element is an element that gets its content from a file. When you edit it through the manager, the file will be updated. Similarly, when you edit a file through the IDE, it will be updated in MODX the next time the element is requested.

This feature is available for all elements, including templates, snippets, chunks, and plugins.

How to Create

To create an element as a static element, simply check the Static box, select Media Source, and add the path to the file specific to that media source to the Static File input. You can also click the icon in the Static File input to open the MODX browser to select the file instead of typing it in manually.

Creating static elements using a template example

This process is the same for all element types.

Files vs. Database

Keep in mind that MODX still stores the content in the database so that the element can be processed normally. MODX only checks the file content when the element is requested without caching, so be sure to clear the cache when developing. For this reason, static items may be most useful when working with uncached snippets or plugins, and somewhat less useful for templates and blocks.

To improve the editing experience with static items, there are a few additional features that can help automatically create items in the MODX database or automatically clear the cache for you.

Automatically Generate Static Items

You can automate the static item generation process in core. It removes the manual steps of creating a static item, marking the item as static, selecting the static item’s media source, and it automatically generates a file path name for your static file.

When an item is deleted or renamed, its associated file is also deleted.

Configure Automatic Static Items

  1. Go to System > System Settings. Select a Static Items theme.
  2. Enable static item automation for each item type you want to use the static item automation workflow. For example, enable static_elements_automate_snippets to use it for snippets, and static_elements_automate_chunks to use it for chunks.
  3. Select the default media source for static elements in static_elements_default_mediasource.
  4. Specify the static elements base path where files will be sent in static_elements_basepath. For example: components/static/elements/.
  5. If necessary, select the default category to use for static elements in the static_elements_default_category parameter.
  6. To ensure that the tpl extension works in templates, you need to add the tpl extension to the upload_files setting. MODX automatically generates static elements with this name.

File structure

Files will be created at the path: {static_elements_basepath}/{type}/{category}/{filename}.{extension} in the specified media source. For example: components/site/elements/chunks/html/footer.chunk.tpl.

Rate article
MODX 3
Add a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.