Book Image

CMS Made Simple Development Cookbook

Book Image

CMS Made Simple Development Cookbook

Overview of this book

CMS Made Simple has great capabilities “out of the box,” but one of its great strengths is the ease of extending those capabilities. You can add a surprising amount of functionality just by customizing the core modules, but once you learn to write your own tags and modules, your ability to add features is virtually limitless.CMS Made Simple Development Cookbook will show you how to use custom PHP code to extend the power and features of CMS Made Simple, and make it do exactly what you want. This easy to use guide contains clear recipes that introduce the key concepts behind each approach to extending the CMS, while also providing examples of solutions to real-world problems.You will learn the differences between the various kinds of tags and modules in the CMS Made Simple environment, and to which purposes each is best fit. Each technology is then explored in detail with a series of practical recipes and examples.You will not only learn the basics of creating tags and modules, but you will explore the underlying APIs that you will use to solve real-world website problems. You will become proficient with the database and form APIs, so that the code you write is portable and maintainable. You'll learn to localize your code and use templates to add its flexibility. You'll master the safe handling of parameters and the creation of secure code. You’ll be familiar with the CMS Made Simple Developer's Forge, and how you can use it in conjunction with revision control as a community-focused code management system, complete with web-based bug tracking and feature requests. You will learn to code complex interactions between modules, both directly and via the creation and handling of events. You will gain exposure to an array of advanced tips and tricks, along with commentary from the distilled experience of someone who has written dozens of modules. The CMS Made Simple Developer's Cookbook offers an amazing wealth of knowledge in approachable, bite-sized recipes. Whether you're new to the CMS or an old hand, you're sure to find valuable tips and information that will have you creating a richer CMS.
Table of Contents (16 chapters)
CMS Made Simple Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface

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. 1. Log in to the Administration panel of your CMS.

  2. 2. On the top menu, select "Extensions" and click on "User-Defined Tags".

  3. 3. Click on the "Add User Defined Tag" button.

  4. 4. Enter the name "helloworld" in the"Name" field for your new User-Defined Tag.

  5. 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. 6. On the top level menu, select "Content" and click on "Pages".

  7. 7. Click on the "Add New Content" button.

  8. 8. Fill in the name of the new page as "Hello World".

  9. 9. Enter "Hello World" as the menu text.

  10. 10. For the page content, put in your new tag {helloworld}, and then hit "Submit".

  11. 11. View your site from the user side. Click on the new "Hello World" page.

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