官术网_书友最值得收藏!

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...

  1. Log in to the Administration panel of your CMS.
  2. On the top menu, select "Extensions" and click on "User-Defined Tags".
  3. Click on the "Add User Defined Tag" button.
  4. Enter the name "helloworld" in the"Name" field for your new User-Defined Tag.
  5. Enter the following code snippet into the "Code" field, and click on"Submit".

    echo "Hello World, Welcome to CMS Made Simple version ".CMS_VERSION;

  6. On the top level menu, select "Content" and click on "Pages".
  7. Click on the "Add New Content" button.
  8. Fill in the name of the new page as "Hello World".
  9. Enter "Hello World" as the menu text.
  10. For the page content, put in your new tag {helloworld}, and then hit "Submit".
  11. View your site from the user side. Click on the new "Hello World" page.
  12. 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
主站蜘蛛池模板: 洪泽县| 聊城市| 隆化县| 陵川县| 榆中县| 大悟县| 依兰县| 大竹县| 阳城县| 华坪县| 宝山区| 内黄县| 盈江县| 太原市| 福建省| 贺州市| 罗城| 客服| 衢州市| 平原县| 长岛县| 庄河市| 沅陵县| 孙吴县| 田林县| 寿宁县| 凤城市| 蒲城县| 南岸区| 临泽县| 武功县| 麻城市| 依兰县| 白水县| 日喀则市| 西乡县| 云梦县| 南开区| 曲周县| 阳山县| 兴隆县|