MODX Snippets

Snippets Website development
Snippets are the way MODX allows you to run dynamic PHP code on any of your pages. They can provide content such as menus, blog or news lists, search, any other form-based functionality, and anything else your site needs to generate on demand.

Using Snippets

Once you have a Snippet installed, you can use it by simply placing its tags in a template, block, TV, or document content wherever you want the Snippet output to appear.

[[MySnippet]]

If you expect the Snippet code to be dynamic for different users, you can also call the Snippet without caching:

[[!MySnippet]]

Snippet Properties

Snippets can have Properties that can be passed in to the Snippet call, like this:

[[!Wayfinder? &startId=`0` &level=`1`]]

You can also combine these properties into Property Sets, which is a dynamic set of properties that can be attached to any snippet (or element for that matter). This allows you to share common property configs across a snippet call in one place.

Let’s say you have a property set called ‘Menu’ with startId set to 0 and level set to 1:

[[!Wayfinder@Menu]]

The snippet will automatically load these properties into the snippet. And even these properties can be overridden:

[[!Wayfinder@Menu? &level=`2`]]

which will override the level set to 1, setting it to 2 instead.

Installing Snippets

You can also download and install snippets via Package Management. See the Package Installation Guide for more information.

Rate article
MODX 3
Add a comment

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