One of the features that sets Modx apart from competing content management systems is the way it handles PHP and HTML code. Both types of code can be freely used in the creation of any of the pages for your Modx content. However, PHP code in particular can be used in reusable pieces of code called SNIPPETS. Snippets in Modx are defined as saved portions of code that can be repeatedly called for use within the Modx website. In order to give you a basic understanding of the use of snippets, we will walk you through an example of creating and using a very simple snippet.
Using snippets require an understanding of PHP coding. This example only shows a very simple use of PHP code.
The following steps walk you through creating a snippet within the Modx Administrator Dashboard.
<?php
$output="<p>A simple website<br />
Bits of words and pictures make<br />
Something with nothing</p>";
return $output;
?>
The next part of this tutorial uses the snippet that you created (called testsnippet) in a simple Modx document. You will see how to reference the snippet with a simple tag (for more information on tags, go to Commonly Used Tags in Modx) and how the output from the snippet appears on the page.
<center>
<h1>A Simple Sample Web Page</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur ac ullamcorper ante. Integer in urna nibh. Aenean vel risus tellus.</p>
<p> </p>
<p>My Haiku of the Day:</p>
<p>[[testsnippet]]</p>
</center>
This completes the use of referencing a very simple SNIPPET within a Modx document. Note how the snippet is referenced within the document web page. The following screenshots indicate how the snippet appears in the document settings in Modx and then in the view of the page from a web browser:
In the screenshot above, the snippet is outputting 3 lines of text (a simple haiku). This is a very simplistic view of how SNIPPETS can work, but as you grow more experienced with Modx and coding the snippets with PHP code, you can see the power of snippets as they are used throughout your Modx website.
Email: | support@WebHostingHub.com | Ticket: | Submit a Support Ticket |
---|---|---|---|
Call: | 757-416-6627 | Chat: | Click To Chat Now |
We value your feedback!
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
new! - Enter your name and email address above and we will post your feedback in the comments on this page!