The Bug Genie
The Bug Genie
star_faded.png
Please log in to subscribe to updates for this article
The Bug Genie:Development:Templates
Last updated at Tue Aug 23 16:40, by zegenie

Template files  ⇑ top

Template files are generally where the output is generated and presented to the user (with the exception of possible plaintext or json output directly from an action). TheBugGenie does not use a specific template syntax or engine which means TheBugGenie templates are just regular snippets of PHP that contain mainly HTML and some PHP to handle template logic and variable output.

All template files are located in the templates/ subfolder of the module where they reside. TheBugGenie also ships with layout templates, located in core/templates/ - these contain the layout skeleton and also takes care of js and css loading.

Naming templates  ⇑ top

There are three types of templates:
  • Regular templates - these are included at the end of regular action calls, and are being passed the information set in the action
  • Template partials - smaller snippet of template code, meant to be reused. These partials does not contain any business logic
  • Component partials - same as template partials but with the difference that these also contains business logic.


Regular templates  ⇑ top

Regular templates are named after the action being invoked combined with the intended format. For example, the action runIssues() can have both html, rss and json output templates, in which case they will be called
 templates/issues.html.php
 templates/issues.rss.php
 templates/issues.json.php


Tips:
  • TheBugGenie will only apply the standard layout decoration to html templates - the json and rss templates will not be encapsulated in the general layout files from core/templates/
  • The template will not be displayed or loaded if the associated action returns a redirect, forward or custom output


Template partials  ⇑ top

These are also located in the templates/ subdirectory where the module is located, but are prefixed with an underscore, and does not have any associated format, but instead an "inc.php" suffix. As such, the template partial "main/issue" (theoretically used to display one issue in a list of issues) will be located in the main module, named
 templates/_issue.inc.php


Template partials are included in regular templates using the include_template() function:
<h1><?php echo __('Issues'); ?></h1>
<ul>
  <?php foreach ($issues as $issue): ?>
    <?php include_template('main/issue', array('issue_to_display' => $issue)); ?>
  <?php endforeach; ?>
</ul>
As shown above, you can pass information (variables) to template partials when they are included by using key => value pairs in the parameters array (the second parameter to the include_template() function). The variables passed to the template will be named with the array key. In the above example, the main/issue partial could contain:
<li><?php echo $issue_to_display->getFormattedIssueTitle(); ?></li>


Note: Variables passed to template partials and component partials are available directly from $<array_key> and in a component partial's action from $this-><array_key>

Component partials  ⇑ top

Component partials follows the same rules as template partials, with the addition of the component action. This is an action located in the actioncomponents.class.php file:
class mainActionComponents extends TBGActionComponent
{
  public function componentUserdropdown()
  {
    $this->user = TBGContext::factory()->TBGUser($this->user_id);
  }
}
In the example above, the component takes a $user_id variable passed to it and populates a user object, then assigns it to the component template for outputting (using $this->. The template would contain something like this:
<div id="user_<?php echo $user_id; ?>_username" class="username"><?php echo $user->getUsername(); ?></div>


The component partial is included in the same way as a template partial, only using the include_component() function instead of the include_template function.

Template variables  ⇑ top

For more detailed information about variable handling in templates, as well as a list of available core variables, see the template variables documentation.

Page titles, additional template content  ⇑ top

In addition to the basic template handling shown so far, you can also use the response object to manage more advanced templating and response functionality. For more information about how to do this, see the response documentation.


Article attachments

There are no file attached to this article

Article comments (0)

There are no comments