Personal tools

Wij template

From Deep Thought

Jump to: navigation, search

Click here for the other F-It parts-pieces.

Contents

Overview

class:wij_template purpose: process the view via template files

One of these is created for each template parsed.

This is not a singleton because it carries attributes and path over from the controller.

This will crawl "up the tree" from the lowest level method, to the class, to the application level templates. If it can't find a suitable matching template then the view will display the error template.

Properties

private array $_cache :: raw, unparsed template data. keys are template filenames.

private array $_attributes :: hash containing template variables.

private array $_path :: hash containing {class,method,vars}

Methods

private parse_file

Arguments

string $str :: the contents of a template or part of a template to be parsed.

Description

Searches $str for template syntax to be processed, and runs callback functions which replace the matches with the appropriate data. There are currently three types of syntax that this function handles (see #syntax):

  • conditions
  • loops
  • tags

private _tag_variable

Arguments

string $var :: a variable name (or hash) to evaluate.

Description

The following is a list of possible methods of parsing variables may go through:

  1. As a Hash: If the supplied $var matches "/^(\w+?)(\[.*\])$/", it will be mapped to a hash form the _attributes property. Recursion will occur for each sub-array needed.
  2. As a String Literal: If the $var is contained by single or double quotes, it will be treated as a literal string and returned.
  3. As a Numeric Literal: If the $var passed is either an integer or float, it will be treated as a literal number and returned.
  4. As a Boolean Value: If the $var is true or false, a boolean value will be returned.
  5. As a wij_template Attribute Name: If the $var is a reference to an existing key in the _attributes array, that value will be returned.

Syntax

methods: parse_condition, parse_loop, parse_tag

All parsed template syntax takes the form of <%<operator><arguments><%>

Operators

? :: condition

AKA, 'if' operator. Conditions may be nested within themselves and within loops. The conditional statement may consist of the following operators:

  • = :: the parameters are equivalent
  • != :: the parameters are not equivalent
  • > :: the first parameter has higher value
  • >= :: the first parameter has higher or equal value
  • < :: the first parameter has lower value
  • <= :: the first parameter has lower or equal value

Conditions accept the following types of parameters for comparison:

  • template variables :: these are variables contained in this->_attributes as keys.
  • loop variables :: variables set by a containing loop statement.
  • integers and floats :: [0-9.]*
  • boolean values :: true, false, maybe (actually, forget about maybe)

Format

<regular template content><%?<conditional statement>%><conditional template content><%?%><regular template content>

The above would render the conditional template content only if the conditional statement returned true.

~ :: loop

A loop is essentially a 'foreach' statement that will iterate for each key of the array argument. The contents of a loop can include regular template content, conditional statements, sub-loops, and loop variables. Loop variables are variables that are references to the values specific to a single iteration. These include:

  • =~key :: the key of the current iteration
  • =~value :: the value of the current iteration's key

Loop variables are called using both the variable operator and the loop operator, in that order. NOTE: loop variables reference ONLY the immediate container loop. You cannot access parent loop variables from within a sub-loop.

Take the following example:

we have array sally = {0=>'zero', 1=>'one', 2=>'two'}

<%~sally%>
 we're number <%=~key%>.  Spell it <%=~value%>.<br />
<%~%>

Will output:

we're number 0.  Spell it zero.<br />
we're number 1.  Spell it one.<br />
we're number 2.  Spell it two.<br />

Format

<regular template content><%~<loop array>%><loop template content><%~%><regular template content>

= :: variable

these tags are replaced by values from the _attributes hash.

Format

<%=<template variable>%>


! :: function

(undeveloped. more description needed.)

Format

<%=<template variable>%>


@ :: import

The import tag may be used to include some common page elements like the header and footer. (more description needed. include description of template selection algorithm.)

Format

<%=<template filename>%>