- CMS Made Simple Development Cookbook
- Samuel Goldstein
- 573字
- 2025-02-22 11:23:08
Create a "Hello World" User-Defined Tag
If you have decided to create a User-Defined Tag to solve a problem, you might want to know what a UDT looks like.
This recipe shows you how to make a simple User-Defined Tag.
Getting ready
Like most of the recipes in this book, you will need to have CMS Made Simple installed and working. You will need login access to the site's Administration area as a member of the "Admin" group (or as a member of a group with "Modify User-defined Tags" and "Add Pages" permission settings).
How to do it...
- Log in to the Administration panel of your CMS.
- On the top menu, select "Extensions" and click on "User-Defined Tags".
- Click on the "Add User Defined Tag" button.
- Enter the name "helloworld" in the"Name" field for your new User-Defined Tag.
- Enter the following code snippet into the "Code" field, and click on"Submit".
echo "Hello World, Welcome to CMS Made Simple version ".CMS_VERSION;
- On the top level menu, select "Content" and click on "Pages".
- Click on the "Add New Content" button.
- Fill in the name of the new page as "Hello World".
- Enter "Hello World" as the menu text.
- For the page content, put in your new tag
{helloworld}
, and then hit "Submit". - View your site from the user side. Click on the new "Hello World" page.
- Admire the output from your User-Defined Tag!
How it works...
A User-Defined Tag is a way of attaching arbitrary PHP code to a Smarty tag. When the CMS Made Simple templates get parsed, Smarty will call the User-Defined Tag and execute the code. Any output from the code will be substituted into the template at the point of the tag.
Our basic example creates a Smarty tag which is called "helloworld." The code for the tag simply outputs a string of text, in this case, the welcome message. To add a bit of interest, we also have it output some useful information: the version of CMS Made Simple that you're running. That value is available in CMS Made Simple via a PHP define, and it can be used in any code run by the CMS.
There's more...
Keep in mind that CMS Made Simple page content is also sent to Smarty for processing — even though we consider it "Content", Smarty regards it as a template. This means that you can place your User-Defined Tag in your underlying page template or in your page content, and it will work just the same either way.
We've seen that any output from a User-Defined Tag gets substituted for that tag in the final rendering of the page. It may not be obvious at first, but UDTs aren't required to produce output at all! A User-Defined Tag may do invisible things behind the scenes: set a flag in the PHP session object, update a counter, and even generate an e-mail. If a UDT does not create any visible output, its Smarty tag will simply be removed from the finally rendered page.
Caching
CMS Made Simple has mechanisms built-in for caching pages in order to improve performance. Under normal circumstances, a User-Defined Tag is not cached — if the UDT creates output, that output is not cached. Similarly, if a UDT does not create any output, it will be called even if the page itself is cached.
See also
- Will a User-Defined Tag solve my problem recipe?
- Create a "Hello World" tag recipe