User Tools

Site Tools


en:public:developer:template_system:start

This is an old revision of the document!


Template system

ArKadia template system is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic. This implies that PHP code is application logic, and is separated from the presentation.

Philosophy

The ArKadia template system design was largely driven by these goals:

  1. clean separation of presentation from application code
  2. PHP backend, ArKadia template frontend
  3. compliment PHP, not replace it
  4. fast development/deployment for programmers and designers
  5. quick and easy to maintain
  6. syntax easy to understand, no PHP knowledge required
  7. flexibility for custom development
  8. security: insulation from PHP

Templates: Two camps of thought

When it comes to templating in PHP, there are basically two camps of thought. The first camp exclaims that “PHP is a template engine”. This approach simply mixes PHP code with HTML. Although this approach is fast from a pure script-execution point-of-view, many would argue that the PHP syntax is messy and difficult to maintain when mixed with presentation. PHP works well for programming, but not so well for templating.

The second camp exclaims that presentation should be void of all programming code, and instead use simple tags to indicate where application content is revealed. This approach is common with other template engines (and other programming languages), and is the approach that ArKadia takes. The idea is to keep the templates focused squarely on presentation, void of application code, and with as little overhead as possible.

Why is separating PHP from templates important?

There are many benefits of separating PHP code from templates, some of which are:

  • SYNTAX: Templates typically consist of semantic markup such as HTML. PHP syntax works well for application code, but quickly degenerates when mixed with HTML. ArKadia's simple <#tag> syntax is designed specifically to express presentation. ArKadia focuses your templates on presentation and less on “code”. This lends to quicker template deployment and easier maintenance. ArKadia syntax requires no working knowledge of PHP, and is intuitive for programmers and non-programmers alike.
  • FEATURES: The template engine has many features for presentation that would otherwise need to be developed, tested and maintained in your own application code. Tags also mask the complexity of PHP statements. For example:

    <?php echo strtolower(htmlspecialchars($title,ENT_QUOTES,UTF-8)); ?>
    {#title lowercase=1 htmlencode=1}

    No different than PHP being an abstraction layer on top of C to simplify development, ArKadia is an abstraction layer on PHP to simplify template maintenance.

  • SANDBOXING: When PHP is mixed with templates, there are no restrictions on what type of logic can be injected into a template. ArKadia insulates the templates from PHP, creating a controlled separation of presentation from business logic. ArKadia also has security features that can further enforce granular restrictions on templates.

Templates

Template is simply a text file. It can generate any text-based format (HTML, XML, CSV, etc.). A template contains variables, which get replaced with values when the template is evaluated, and tags, which control the logic of the template. Below is a minimal template that illustrates a few basics. Each element will be explained later in this document.:

<#doctype>
<html>
  <head>
    <#meta_title>
    <#meta_description>
  </head>
  <body>
    <#frame name="topframe">
  </body>
</html>

Why use a text-based template instead of an XML-based one (like Zope's TAL)? We wanted ArKadia's template language to be usable for more than just XML/HTML templates. We use it for emails, JavaScript and CSV. You can use the template language for any text-based format.

Variables

Variables look like this: <#variable>. When the template engine encounters a variable, it evaluates that variable and replaces it with the result. Variable names consist of any combination of alphanumeric characters and the underscore (“_”). The dot (”.”) also appears in variable sections, although that has a special meaning, as indicated below. Importantly, you cannot have spaces or punctuation characters in variable names.

Use a dot (.) to access attributes of a variable. <#section.title> will be replaced with the title attribute of the section object. If you use a variable that doesn't exist, the template system will insert '' (the empty string) by default.

Filters

You can modify variables for display by using filters. Filters look like this: <#name lowercase=“1”> This displays the value of the <#name> variable after being filtered through the lower filter, which converts text to lowercase. To give you a taste of what's available, here are some of the more commonly used template filters:

default

If a variable is false or empty, use given default. Otherwise, use the value of the variable For example:

<#value default="nothing">

lowercase

uppercase

xmlencode

htmlencode

httpencode

JavascriptEncode

rtfencode

en/public/developer/template_system/start.1353396445.txt.gz · Last modified: 2012/11/20 11:27 by admin