Tag Syntax

Tag Syntax Website development
Tags are MODX elements that allow you to call HTML or PHP code, text from a dictionary, or document variables. The syntax of tags is unified and they are all declared in square brackets with a symbol before the name, which simplifies the logic of work and increases productivity.

MODX Tag Variations

MODX provides a convenient array of tags, distinguished by a token or set of tokens that appear before a string. The table below identifies these tokens and where and how they can be used.

Type Token Example Usage
Comment - [[-Comment]] Defines an unparsed comment.
For example: [[-This is a comment. It will be removed from the page output.]]
Resource field * [[*fieldName]] Displays the value of the field associated with the current resource.
For example: [[*pagetitle]]
Template variable (TV) * [[*tvName]] Print the value of the template variable.
For example: [[*tags]]
Chunk $ [[$chunkName]] Specifies a static snippet of code to render.
For example: [[$header]]
Snippet [[snippetName]] Specifies a snippet of PHP code to execute.
For example: [[pdoResources]]
Placeholder + [[+placeholder]] Defines a placeholder for values ​​from the query results.
For example: [[+pagetitle]]
Link ~ [[~link]] Returns a link derived from the value.
For example: [[~3? &scheme=full]]
Settings ++ [[++settingName]] Defines a placeholder specifically for values ​​defined in system settings.
For example: [[++site_name]]
Language % [[%language]] For example: [[%string? &language=en &namespace=generic &topic=topic]]

Deconstructing the MODX tag

The MODX tag can be extended with additional indicators and properties. The table below completely deconstructs the MODX tag and illustrates how and where these optional indicators and properties can be used.

Type Usage
[[ Opening MODX tag.
! indicating tag is NOT cacheable (optional)
Token element type $ – chunk, * – element field or TV, + – placeholder, etc.
See above for more options
Name The name of the item value being queried.
@propertysetН The property set to use (optional)
:modifier=`value` Specifies the output filter or modifier to use (optional)
Example::gt=`0`:then=`Item is available!`
? Tells MODX that properties accompany this call.
Required if properties are present
&property=`value` Specifies the property and value to be used with the call. Each set of properties is separated by &.
For example: &prop1=`1` &prop2=`2`
]] Closing MODX tag.

Building a MODX tag

By using and combining all of the above information, we can create a complex MODX tag that looks like this:
[[!MySnippet@myPropSet:filter1:filter2=`modifier`? &prop1=`x` &prop2=`y`]]
However, while MODX allows for complex conditional filters, users should be careful when building complex tag logic. Unlike PHP, if you have invalid MODX tag syntax, there are no helpful messages with line numbers indicating the location of the error.

Having tags that require debugging detracts from the clean look of the layer. Keep them clean and simple.

A good rule of thumb is that your tags should fit on one line, even if you mark them up across multiple lines for readability. If you rely on if statements and other conditions in your MODX tags, then you may need to rethink your flow logic.

Note MODX is ambiguous about whitespace, so both of the following examples would also be acceptable:
[[!pdoResources?
&parents=`21`
&limit=`7`
]]

Properties

All MODX tags can accept properties, not just Snippets.

In the example below, we have a simple chunk named “Hello”.

Hello [[+name]]!
Within this chunk, we have a placeholder setup [[+name]] for the value to be displayed. We can pass this value directly into our chunk with the following code:
[[$Hello? &name=`Nikolay`]]
This call will look like this:
Hello Nikolay!

Caching

Any tag can be called uncached by inserting an exclamation mark immediately after the opening double bracket:

[[!snippet]][[!$chunk]][[!+placeholder]][[!*template_var]], and so on.

If you have some advanced setup where the site_url parameter is set for every request, but your [[~[[*id]]]] links are not being generated properly, remember that any tag can be called uncached, including the link or anchor tag: [[!~[[*id]]]]

However, you only need this when the site_url is set dynamically, can be different for every request, and you are generating full URLs instead of relative ones. Any normal usage can be cached.

Parse Order

If you call an uncached snippet, it will be executed after all cached tags have been processed.

If you have cached placeholders below this, they will be evaluated before this snippet is executed – that is, they will get the last value that was previously cached by this snippet (or empty if not set yet).

If you want to call a Snippet without caching that sets a placeholder, you need to make sure that the placeholders are also set to uncached:
[[!Profile]]
Hello [[!+username]]

Timing

MODX has several timing tags:

  • [^qt^] – Request Time – shows how much time MODX spent talking to the database
  • [^q^] – Query Count – shows how many database queries MODX performed
  • [^p^] – Parse Time – shows how long it took MODX to parse the page
  • [^t^] – Total Time – shows the total time spent on parsing / rendering the page.
  • [^s^] – Source – shows the source of the page, whether it is a database or cache
  • [^m^] – Memory Usage – Shows the total amount of memory used to parse / render the page.

Syntax Check

For many beginners, the syntax of tags may seem complicated, so you can use the SyntaxChecker plugin to check the correctness of the tags.

Or better yet, use the Ace editor, which highlights the syntax of MODX tags well.

Rate article
MODX 3
Add a comment

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