Open Power Template
Encyclopedia
Open Power Template is a web template engine written in PHP
PHP
PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document...

 5. A common strategy in designing web application
Web application
A web application is an application that is accessed over a network such as the Internet or an intranet. The term may also mean a computer software application that is coded in a browser-supported language and reliant on a common web browser to render the application executable.Web applications are...

 is separation of the application logic (i.e. data processing) from the presentation (displaying the data). OPT is a tool for implementing such separation. The presentation layer is represented by templates, text files with HTML code and extra instructions controlling the data substitution.

OPT uses a dedicated XML template language for writing templates. It is not a general-purpose, but a domain-specific language. It was primarily designed to support and simplify template-specific problems with a set of declarative
Declarative programming
In computer science, declarative programming is a programming paradigm that expresses the logic of a computation without describing its control flow. Many languages applying this style attempt to minimize or eliminate side effects by describing what the program should accomplish, rather than...

 instructions. Instead of implementing the rendering algorithms and statements, like in imperative programming
Imperative programming
In computer science, imperative programming is a programming paradigm that describes computation in terms of statements that change a program state...

, the template designer specifies the expected result and features. This aims to ease the costs and efforts associated with the software development and further maintenance
Software maintenance
Software Maintenance in software engineering is the modification of a software product after delivery to correct faults, to improve performance or other attributes....

.

The library provides an object-oriented
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

 API based on the solutions from popular frameworks. As it is the first member of a bigger project, Open Power Libs, it is built upon a small OPL core library which provides the basic features.

History

The project started in November 2004, as a template engine for a discussion board project inspired by Smarty
Smarty
Smarty is a web template system written in PHP. Smarty is primarily promoted as a tool for separation of concerns.Smarty is intended to simplify compartmentalization, allowing the presentation of a web page to change separately from the back-end...

. While it later failed, the library became independent. In July 2006, the version 1.0.0 was released. It offered a template language with Smarty-like syntax and a small set of declarative instructions.

In January 2007, the developers release the version 1.1.0 which brings some notable improvements, such as pagination support and tree rendering.

In January 2008, the developers form an open-source programming team, Invenzzia to develop OPT and other PHP projects. At the same time, the development of Open Power Template 2.0 began.

The last version of the 1.1 branch was released in May 2008 and the group focused on the OPT 2.0 development. The new library went into the beta-stage in December and the first stable version was released in July 2009.

Features

The OPT 2.0 template language is an XML
XML
Extensible Markup Language is a set of rules for encoding documents in machine-readable form. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards....

 application and allows to manipulate the XHTML document structure. The other features are:
  1. Template inheritance and other advanced template modularization mechanisms.
  2. Form
    Form (web)
    A webform on a web page allows a user to enter data that is sent to a server for processing. Webforms resemble paper or database forms because internet users fill out the forms using checkboxes, radio buttons, or text fields...

     rendering support (components)
  3. Abstract, declarative list generators (sections)
  4. Automated filtering against cross-site scripting
    Cross-site scripting
    Cross-site scripting is a type of computer security vulnerability typically found in Web applications that enables attackers to inject client-side script into Web pages viewed by other users. A cross-site scripting vulnerability may be used by attackers to bypass access controls such as the same...

     attacks.
  5. Internationalization support.
  6. XML manipulation instructions.
  7. Imperative control structures: conditions and loops.
  8. Expression
    Expression (programming)
    An expression in a programming language is a combination of explicit values, constants, variables, operators, and functions that are interpreted according to the particular rules of precedence and of association for a particular programming language, which computes and then produces another value...

    language optimized for XML and an abstraction layer making it independent from PHP data types and application-specific implementation details (data formats).


The built-in XML parser can be reconfigured to parse certain HTML documents or plain text content.

Sample application

Since the templates are separated from the application logic, you need at least two files. The first one contains the presentation code as an XML template:







{$pageTitle}



{$introduction}






  1. {$list.item}








The second one generates the data and configures the library:

require('./libs/Opl/Base.php');
Opl_Loader::setDirectory('./libs/');
Opl_Loader::register;

$tpl = new Opt_Class;
$tpl->sourceDir = './templates/';
$tpl->compileDir = './templates_c/';
$tpl->setup;

$view = new Opt_View('template.tpl');

// Assigning the script data to the template
$view->pageTitle = 'Sample OPT page';
$view->introduction = 'Sample text';
$view->list = array(0 =>
array('item' => 'Item 1'),
array('item' => 'Item 1'),
array('item' => 'Item 1')
);
$view->setFormat('list', 'Array');

$output = new Opt_Output_Http;
$output->render($view);

Links and references

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK