# Templates

{% hint style="danger" %}
**Please note:** This page is under construction and has not been finished yet.
{% endhint %}

## Introduction

* 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
<?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
<?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

&#x20;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` code array for parsing:

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

// or

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

```

## Overriding Core Templates

* All templates that are used in e107 can be overridden by copying them into specific folders within your current theme folder.&#x20;
* Core themes (located in `e107_core/templates/` ) should be copied into `e107_themes/YOURTHEME/templates/`&#x20;
* Plugin templates should be copied into `e107_themes/YOURTHEME/templates/PLUGIN-FOLDER`\
  Note: Older plugins may look for templates in the root folder of your theme. ie. `e107_theme/YOURTHEME/` &#x20;

### *Examples*

1\) The comment template is a core template, as it is located in e107\_core/templates/. To override this template, copy the file to e107\_themes/your\_theme\_folder/templates/.\
\
2\) The news template is located in e107\_plugins/news/. To override this template, copy the file over to e107\_themes/your\_theme\_folder/templates/news/.\
\
3\) The same for, for example, the chatbox menu template. The chatbox\_menu template is located in e107\_plugins/chatbox\_menu. Copy the template over to e107\_themes/your\_theme\_folder/templates/chatbox\_menu/

**Important:** For overriding plugin templates, the folder name within your\_theme\_folder/templates/ directory must match the **exact** plugin folder name.
