Utilizing e107's Templating and Shortcode engine.

Templating Overview

  • Templates and shortcodes function together to allow a developer to place dynamic content into their code.
  • Templates are portions of HTML which contain markers that are replaced by dynamic content when parsed e107's parseTemplate()function.
  • These markers are called 'shortcodes'.
  • A shortcode is always CAPITALIZED and is surrounded by curly brackets and it may contain letters and underscores. For example: {MY_SHORTCODE}
  • Each shortcode has a corresponding function/method which is run during the parsing of the template. These functions are always lowercase and begin with the prefix sc_ . eg. sc_my_shortcode(). This means that {MY_SHORTCODE} is replaced by what is returned by sc_my_shortcode().
  • Shortcodes may also contain parameters which are sent to the corresponding method. For example: {MY_SHORTCODE: x=foo&y=bar}

Creating Templates

Create a folder called templates inside your plugin directory, and inside create an empty file using the name of your plugin folder, followed by _template.php. eg. myplugin_template.php Inside this file add an array by the same name, but in UPPERCASE: eg. $MYPLUGIN_TEMPLATE['xxxx'] xxxx can be anything you want, but we suggest using start, item, end etc. when applicable. This value should always be lowercase. Here's a simple example of the contents of myplugin_template.php:
<?php

$MYPLUGIN_TEMPLATE['start'] = "<ul>";
$MYPLUGIN_TEMPLATE['item'] = "<li>{MYPLUGIN_ITEM}</li>";
$MYPLUGIN_TEMPLATE['end'] = "</ul>";

?>
If your plugin will use several different types of templates, eg. a listing page and an input form. You can do something like this:
<?php

$MYPLUGIN_TEMPLATE['list']['start'] = "<ul>";
$MYPLUGIN_TEMPLATE['list']['item'] = "<li>{MYPLUGIN_ITEM}</li>";
$MYPLUGIN_TEMPLATE['list']['end'] = "</ul>";

$MYPLUGIN_TEMPLATE['form']['start'] = "<form>";
$MYPLUGIN_TEMPLATE['form']['body'] = "<div>{MYPLUGIN_FORMINPUT}</divi>";
$MYPLUGIN_TEMPLATE['form']['end'] = "</form>";

?>

Loading Templates

You may load a template file in the following way:

$template   = e107::getTemplate('myplugin'); // loads e107_plugins/myplugin/templates/myplugin_template.php

You can now use the $template array for parsing.

Example
$text = e107::getParser()->parseTemplate($template['start'], true, $scObj); 

// or

$text = e107::getParser()->parseTemplate($template['form']['start'], true, $scObj);



Creating Shortcodes

Create an empty file in your plugin directory called myplugin_shortcodes.php where myplugin is the name of your plugin's folder.
Add the following code replacing myplugin with the name of your plugin's folder.

class myplugin_shortcodes extends e_shortcode
{
    // Enter Shortcode methods here
}

Add your shortcode methods using the sc_ prefix and what follows in lower case.
In the example below we will create a shortcode for {SAY_HELLO} which will say 'hello' to the logged in user.
class myplugin_shortcodes extends e_shortcode
{
    function sc_say_hello($parm='')
    {
       return "Hello ".USERNAME;
    }
}

Advanced example using shortcode parameters. eg. {SAY_HELLO: name=john}

class myplugin_shortcodes extends e_shortcode
{
    function sc_say_hello($parm)
    {
       return "Hello ".vartrue($parm['name']);
    }
}

Advanced example using database values. See $sc->setVars($row); in example below.

class myplugin_shortcodes extends e_shortcode
{
    function sc_say_hello($parm)
    {
       return "Hello ".vartrue($this->var['name']);
    }
}

Loading Shortcodes

Loading shortcodes may be done in the following way:

$sc = e107::getScBatch('myplugin',TRUE); // loads e107_plugins/myplugin/myplugin_shortcodes.php



Putting it all together

Parsing the Template

Use the parseTemplate() method from the text-parser class to replace shortcodes with their real value.
$tp->parseTemplate('template','use-core-shortcodes','shortcode-class');

Basic Example

$template   = e107::getTemplate('myplugin', 'form'); // load the 'form' template from the plugin folder. 
$tp = e107::getParser(); // get Parser Class.
$sc = e107::getScBatch('myplugin',true); // get myplugin shortcodes from plugin folder. 

echo $tp->parseTemplate($template['start'], true, $sc); // parse the 'start' part of the 'form' template.

Full Example with loop

$sql = e107::getDb(); // get the database Class. 
$tp = e107::getParser(); // get Parser Class.
$sc = e107::getScBatch('myplugin',true); // get myplugin shortcodes from plugin folder. 

$template   = e107::getTemplate('myplugin', 'list'); // get template array of 'list' from plugin folder. 

echo $tp->parseTemplate($template['start'], true, $sc); // parse the 'start' part of the 'list' template.

$rowArray = $sql->retrieve("SELECT field FROM #myplugin", true);	// load the values from the plugin's database table. 

foreach($rowArray as $row)    // loop through the database results. 
{
	$sc->setVars($row);	    // send the value of the $row to the class. 
	echo $tp->parseTemplate($template['item'], false, $sc);    // parse the 'item' 
}

echo $tp->parseTemplate($template['end'], false, $sc); // parse the 'end' part of the 'list' template.









Social Links