Jump to main content Jump to doc navigation

What is the @SNIPPET Binding?

The @SNIPPET Binding executes the specified MODX snippet. It should be used with careful security precautions.

Syntax

@SNIPPET snippet_name [properties_as_json]

Binds the variable to a snippet. Where snippet_name is the name of the snippet. The returned value is the output of the snippet.
The JSON formatted properties are optional and are passed as scriptProperties to the snippet.

Usage

let's have a snippet datefmt with this code

<?php
$locale = $modx->getOption('locale',$scriptProperties,'en_US');
$pattern = $modx->getOption('pattern',$scriptProperties,'YYYY/MM/dd');

$fmt = datefmt_create(
    $locale,
    IntlDateFormatter::FULL,
    IntlDateFormatter::FULL,
    null,
    null,
    $pattern
);
return datefmt_format($fmt, time());

In Default Text simply put a snippet name after the @SNIPPET tag:

@SNIPPET datefmt
@SNIPPET datefmt {"locale":"de-DE","pattern":"EEEE, dd.MM.YYYY"}

Examples

There's a whole page showing an example of using the SNIPPET binding to generate input-options: Creating a multi-select box for related pages in your template.

db-driven input-option-values by using migxLoopCollection

Let's say we want to have a dropdown-box to select a user-id by username.
Requirements: MIGX installed.

Then create a listbox TV with this input-options:

@SNIPPET migxLoopCollection {"classname":"modUser","tpl":"@CODE:[[+username]]==[[+id]]","outputSeparator":"||","wrapperTpl":"@CODE:-- select a user--==0||[[+output]]"}

See Also

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

  • modmore
  • STERC
  • Digital Penguin
  • Jens Wittmann – Gestaltung & Entwicklung
  • Fabian Christen
  • Dannevang Digital
  • Sepia River Studios
  • CrewMark
  • Chris Fickling
  • deJaya
  • eydolan
  • Lefthandmedia
  • Murray Wood
  • Following Sea
  • Anton Tarasov
  • Stéphane Jäggi
  • Raffy
  • Snow Creative
  • A. Moreno
  • Nick Clark
  • JT Skaggs
  • Helen
  • YJ
  • krisznet
  • Richard
  • Yanni

Budget

$306 per month—let's make that $500!

Learn more