Beginners guide on how to include snippets in the document/page

From MODx Wiki

Jump to: navigation, search

Writing your own snippet, and learning how to use them, a VERY simple example. Remember that a snippet is actually a PHP function: it's a bit of code that outputs something. That something can be a simple little bit of text, or it can be a huge and complete HTML document.

  1. Login to the admin panel at www.yoursite.com/manager
  2. Go to Resources-->Manage Resources-->Snippets
  3. Select 'New Snippet'
  4. Give your snippet a name, e.g. 'hello_world' (without the quotes)
    To reference this snippet on a page (e.g. within a template), you'd put [[hello_world]] on the page. That causes the snippet to execute and its output will replace the double-square-bracket placeholder.
  5. Write some code for your snippet. Here's the VERY simple example of the PHP code for this snippet:
<?php
  echo 'Hello world!';
?>

Save the snippet. Now head back to one of your pages and edit it. Within the page's content, you can paste in the [[hello_world]] text and save it, just make sure you are looking at the raw html code for the page's content. I'm not sure if this works if you have one of the formatting editors enabled.

When you visit the page, you should see "Hello world!".

Personal tools