- CMS Made Simple Development Cookbook
- Samuel Goldstein
- 573字
- 2021-04-09 21:13:18
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
- Moodle 2.0 E/Learning Course Development
- CAXA CAD電子圖板2020工程制圖
- Solid Works 2021產品設計標準教程
- Excel 2010 商務數據分析與處理(第2版)
- SolidWorks2014基礎實例教程
- UG NX 9.0中文版基礎與實例教程
- 音樂日記:Logic Pro X場景x風格編曲實用教程
- Photoshop CS6實戰基礎培訓教程(全視頻微課版)
- Adobe創意大學InDesign產品專家認證標準教材(CS6修訂版)
- Power Query For Excel:讓工作化繁為簡
- 從零開始:Indesign CC 2019設計基礎+商業設計實戰
- 中文版Photoshop CS6完全自學手冊(超值版)
- 24小時學會Word-Excel-PowerPoint 2010三合一
- 企業虛擬化實戰:VMware篇
- EJB 3.1 Cookbook