Introduction

Introduction – What HTML_Template_Flexy can do

Introduction

HTML_Template_Flexy started its life as a simplification of HTML_Template_Xipe, However the long term aim of Flexy is to provide a universal Template Base API for Compiling and native PHP type templates.

Flexy currently supports a number of backends (Template Formats), and is designed to be extended to support more, The Key Formats are:

  • Standard - A rich Tokenizer driven engine, that uses {variable_placeholders}, attribute based (flexy:foreach="...."), and custom tag <flexy:tojavascript ... To rapidly create PHP code from simple markup. These templates are designed to be editable in WYSIWYG HTML editors, without breaking the tags
  • Regex - A classic templating backend, for supporting Smarty, Xipe, or Email type convertion to PHP code.
  • Raw - A non compiling backend, that enables you to create templates using PHP, Useful if you intend to redistribute your application and are concerned about compilation

Data can be assigned in two ways with flexy, depending on your preferred style of working.

  • Push - you put data into the template engine, using $flexy->setData(), and $flexy->setDataByRef(), this is similar to the way Smarty and other templates work.
  • Push/Pull - You provide the outputer with a Data Provider Object, (either a Data Object, or a Controller) that contains the data to display (and objects with methods that can be called). This has the added benefit of making the Variables in the template documentable, using PEAR standards, eg. PHPDoc comments.

With all this Flexibility, it still manages to achieve

  • Very Lightweight Simple API, which easy to learn
  • In Normal operation very little code is actually loaded (so it's fast)

How does HTML_Template_Flexy differ from other template systems

If you look around you will see there are other template systems available in PHP, they generally fall into two categories, Replacement Systems, or PHP Code builders.

Replacement systems like HTML_Template_IT, FastTemplate, PhpLib Template tend to be slower at doing block and nested block type templates and involve alot of code to add each variable to the template.

Php Code builders like Flexy, Smarty, SimpleTemplate (now HTML_Template_Xipe) tend to be better at more complex templates, and can offer a better approach to extendability. (the long term aim of Flexy is to integrate support for all of these PHP Generator templates into a simple package)

The Standard Compiling Backend uses a Tokenizer, which offers the possiblities of using HTML tags and attributes to provide looping and conditionals, and make dynamic XML_Tree like elements of HTML Forms that can be manipulated in your code. (This conversion is only done once when the template compiles)

Typical use example

Flexy template is normally called from within a Controller Class (in the Model,View,Controller paragam). You just send HTML_Template_Flexy, the name of the template, and the object to output. - any variable you want printing out just has to be set in the object being used to ouput.

Typical usage example for HTML_Template_Flexy

<?php

/* configure the application - probably done elsewhere */
require_once 'HTML/Template/Flexy.php';
require_once 
'PEAR.php';
$options = &PEAR::getStaticProperty('HTML_Template_Flexy','options');
$config parse_ini_file('example.ini',TRUE);
$options $config['HTML_Template_Flexy'];


/* the page controller class */

class controller_test 
{

    var 
$template "home.html"// name of template
    
var $title;                  // this relates to {title};
    
var $numbers = array();      // this relates to {numbers} , used with foreach
    
var $anObject;

    var 
$elements = array();      // this is where the elements are stored

    /* start section - deals with posts, get variables etc.*/

    
function controller_test() 
    {
        
$this->start();
        
$this->output();
    }


    function 
start() 
    {
        
// the title
        
$this->title "Hello World";

        
// store an object.
        
$this->anObject = new StdClass;

        
// assign a value to a member.
        
$this->anObject->member 'Object Member';

        
// if you need form elements - you have to include them.
        
require_once 'HTML/Template/Flexy/Element.php';

        
// create an HTML Element for the form element.
        
$this->elements['input'] = new HTML_Template_Flexy_Element;

        
// assign a value to it
        
$this->elements['input']->setValue('Hello');


        for (
$i 1;$i5;$i++) {
            
$this->numbers[$i] = "Number $i";
        }
    }

    
/* output section - probably best to put this in the default_controller class */

    
function output() {
        
$output = new HTML_Template_Flexy();
        
$output->compile($this->template);
        
$output->outputObject($this,$this->elements);
    }


    function 
someMethod() {
        return 
"<b>Hello From A Method</b>";
    }



}

/* the page controller instantaation - probably done with a factory method in your master page controller class */

new controller_test();

?>

Now the example template,

Example Template for HTML_Template_Flexy

//ignore any php tags before this comment
<html>
  <head>
    <title>{title}</title>
  </head>
  <body>
  <H1>{title}</H1>



    <form name="form">
    <table>
      <tr>
        <td>
         <?php /*  this will be merged with the date in the  $element['input']  object*/ ?>

          Input Box: <input name="input">
        </td>
      </tr>

      <?php /*  note here the use for flexy:foreach as an attribute value */ ?>

      <tr flexy:foreach="numbers,number,string">
        <td>

      <?php /*   note here the use for flexy:foreach as an attribute value */ ?>

          <a href="mypage.html?id=%7Bnumber%7D">{string}</a>
        </td>
      </tr>


    </table>

    <?php /*   there is some limited support for array access */ ?>

    this is number 2 : {numbers[2]}


    <?php /*  note that full stops separate object and variables or methods */ ?>

  This is a {anObject.member}

    <?php /* you can call methods of the object */ ?>

    {someMethod()}

    <?php /* by default everything is htmlspecialchar escaped use the modifier :h to prevent this. */ ?>

    {someMethod():h}
    
  </body>
</html>

<?php /*  I've used php for comments here as HTML comments didnt work when generating the
manual .. - you dont have to use them - it has nothing to do with the template engine */ 
?>

And the output

Output of example for HTML_Template_Flexy




Hello World

Input Box : [Hello     ]
Number 1
Number 2
Number 3
Number 4

this is number 2 : Number 2


This is a member Variable

<B>Hello From A Method</B>

Hello From A Method
HTML_Template_Flexy (Previous) Setting the defaults for HTML_Template_Flexy (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

There are no user contributed notes for this page.