Shortcodes:Prerender Shortcode

From e107 Wiki
Jump to: navigation, search

e107 Wiki: English | Русский | Deutsch | Français | Magyar | Português | Български | Česky | Nederlands | Ελληνικά | Italiano | Norske | Polska | Slovenščina | Español | Svenska | Translate: Wiki | Page

logo_wiki.png

Current Release 1.0.4 | e107.org | Download | Changelog | Forum | Plugins | Themes | RSS Feed of Latest Changes | Atom Feed of Latest Changes


e107 Shortcode Guide
Main page > I : First Aid > II : Development > III : Administration > IV : How To's > V : Plugin Writing > VI : Theming & Shortcodes



Pre-rendering shortcodes

Some shortcodes can be pre-rendered in your theme.php or admin_template.php etc, so you can alter your themes output based on what is contained within your pre-renders.

This is best illustrated with an example...

In the admin area you will have noticed that after 30 days a menu appears that warns you that your password hasn't been updated and that you should now recreate it. This menu is rendered by the shortcode {ADMIN_PWORD} which is found in your admin_template.php.

Perhaps you feel that the warning isn't strong enough and you want to display a big red warning message graphic above the menu when it is displayed.

If you were to do the following code in your admin_template.php you will quickly notice a problem:

  1. $ADMIN_HEADER .= "<img src='big_red_warning.png' /> {ADMIN_PWORD}";


The problem is that your big red warning graphic will always show, even when the users password is not in need of being updated and the menu isn't being displayed.

A way round this problem would be to pre-render the shortcode so you can test if its active and if so display your graphic and if not, don't display it.

The first step then is to do the pre-render:

  1. $admin_password = $tp -> parseTemplate('{ADMIN_PWORD}'); 


We do this before we define $ADMIN_HEADER. What has happened is that the shortcode has been processed early and the output from it, if there is any (ie. if the password needs updating and the menu is to be displayed), has been collated into the variable we created called $admin_password.

If the menu isn't to be displayed, this variable wont contain any data - i.e. it will be an empty string: "".

Now we know this information we can create a rule when building our $ADMIN_HEADER to determine if the $admin_password variable contains the menu or an empty string and if it does we display our big red warning graphic, if it doesn't we don't display the graphic. Here's the code:

  1. $admin_password = $tp -> parseTemplate('{ADMIN_PWORD}');
  2.
  3. if ($admin_password!="") {
  4. $ADMIN_HEADER .= "<img src='big_red_warning.png' /> ".$admin_password;
  5. } 

Obviously, you need to know PHP to take full advantage of pre-rendering, but this additional feature will allow you to create truly dynamic themes.