Writing a Good Chunk
Last updated Oct 14th, 2026 | Page history | Improve this page | Report an issue
Support the team building MODX with a monthly donation.
The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.
Backers
Budget
$194 per month—let's make that $500!
Learn moreDo NOT Over-Rely on Output Modifier Logic!¶
Output Modifiers are a small set of parser rules that manipulate data in your view layer — in Chunks and Templates.
The most common mistake is putting too much logic there. Some filter or an occasional if in a Chunk is fine; keep it to a minimum. MODX's architecture expects the view layer to stay clean, and heavy logic in Chunks and Templates works against that.
The WRONG Way to do It¶
Here's an example of a tpl Chunk that got completely out of hand:
[[+tv.RemovePin:eq=`active`:then=`[[+modx.user.id:memberof=`
Members`:then=`{address:'[[+tv.country]], [[+pagetitle]]', data:'<div class="googleBubble">
[[+tv.Socialicons:replace=`||==`:replace=`facebook==<img src="/assets/images/mapof/facebook.png"
alt="Facebook" width="16" height="16" class="pull-right mR2">`:replace=`linkedin==<img src="/assets/images/mapof/linkedin.png" alt="linked in" width="16" height="16" class="pull-right mR2">`:replace=`twitter==<img src="/assets/images/mapof/twitter.png" alt="twitter" width="16" height="16" class="pull-right mR2">`]]<b>[[+tv.GPGroupName:replace=`'=='`:default=`Dr [[+createdby:userinfo=`fullname`:ucwords]]`]]</b><br>[[+tv.occupation:notempty=`Occupation: [[+tv.occupation]]<br>`]][[+tv.other:notempty=`Occupation: [[+tv.other]]<br><br>`]]
[[+content:replace=`
==<br>`:replace=`'=='`:strip]]<br>[[+tv.leadadmin:notempty=`<br>Admin contact: [[+tv.leadadmin]]`]] [[+tv.opentomembers:notempty=`<br>Open to new members?: [[+tv.opentomembers]]`]]
<br>[[+tv.showEmailAddress:eq=`Yes`:then=`Email: <a href="mailto:[[+createdby:userinfo=`email`]]">
[[+createdby:userinfo=`email`]]</a>`:else=``]]</div>',options:{[[+tv.leadadmin:neq=``:then=`icon: new
google.maps.MarkerImage('/assets/images/mapof/pin-gold.png')`:else=`icon: new
google.maps.MarkerImage('/assets/images/mapof/pin-blue.png')`]]}},`:else=`{address:'[[+tv.country]], [[+pagetitle]]', data:'<div class="googleBubble"><h3>Login to view</h3><p>To view the details of this pin please <a href="[[~702]]">create an account</a> or <a href="[[~702]]">login</a></p>
</div>',options:{[[+tv.leadadmin:neq=``:then=`icon: new
google.maps.MarkerImage('/assets/images/mapof/pin-gold.png')`:else=`icon: new
google.maps.MarkerImage('/assets/images/mapof/pin-blue.png')`]]}},`]]`]]
If your Chunks look like this, stop now: rethink the problem and move the logic into a Snippet or elsewhere.
Reasons Why Output Modifiers Might Destroy Your Site¶
- Increased Maintenance costs: it will take much more time to edit your HTML if you obscure it with complex logic.
- Speed! You cannot fully cache a dynamic Chunk or Template that uses logical conditions. Output Modifiers are much slower than simple PHP.
- Unlike PHP, Output Modifier errors are not shown in the page output: they are written to the MODX error log (without a line number pointing into your Chunk), so it's much, much harder to debug errors!
- Output Modifiers do not use brackets, and it's impossible to match back-ticks in a text editor, so it's very easy to make syntax errors in your code.
If you really do need them, this article shows how to use Output Modifiers as conditionals with a minimum of overhead.
The Right Way to Do It¶
Keep your Chunks and Templates clean. They should be easy to read and easy to edit.
Dear [[+first_name]], it was nice to see you last [[+day_of_week]]
Good Chunk tpl-keeping Checklist¶
- Give your Chunk tpl a good description so people will know what it contains and which Snippets rely on it.
- Do not include other Chunks in Chunks! Things can get very confusing if you start going down that rabbit-hole.
- Use proper indenting so your HTML is easy to read.
- If you do use some logic in your Chunk tpls, make sure you use it responsibly. Do not nest tags, and do not chain long if-else statements together. A good rule-of-thumb is this: if your Output Modifier does not fit on one line, then you should do it another way.
See Also¶
- Templating Your Snippets
- Adding CSS and JS to Your Pages Through Snippets
- How to Write a Good Snippet
Support the team building MODX with a monthly donation.
The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.
Backers
Budget
$194 per month—let's make that $500!
Learn more










