Vemplator 0.6.1 - Making MVC (Model/Vemplator/Controller) a reality Created by Alan Szlosek (http://www.greaterscope.net) Begun October 2005 Licensed under MIT License. See the LICENSE file for more (legal) information. INTRODUCTION ------------ Vemplator is a templator/templating system, written in PHP. A Templator basically replaces specifically formatted strings of text (variables, etc) contained in an HTML file. Fully exploiting this results in code without any interspersed (and hard to find) HTML tags. When implemented properly, a site's layout can be changed merely through HTML files without having to modify any code. See http://www.greaterscope.net/article/Website_Templating for my article on the subject. DESIGN GOALS ------------ All development decisions follow these guidelines: * Create a simple template syntax that is easy to parse (and keep it that way) * The algorithm for parsing the simple syntax should be succinct and comprehendable, not spread out over several files * If the parsing algorithm is easily understood then it should be easy to extend the simple syntax The above items (hopefully) contribute to being able to wrap your head around all of Vemplator: the full template syntax and the code that makes it work. FEATURES -------- * Ability to make function calls from within templates * Template variables * Conditionals: if/else * Conditionals: switch statements (case values can be string constants or other variables) * Dot-notation for class member variables (customer.name) * Numerically indexed arrays: record[1][3] * Associative arrays: customer['name'] * Array elements indexed by other template variables: customer[ key ] * Foreach looping * Caching of parsed templates * Template includes (included templates are parsed and cached separately) * Less bloat than alternatives DETAILS -------- * ~220 lines of code * Regular Expression matching for template compilation GUIDING PRINCIPLES ------------------ I. Keep HTML out of your code II. Keep code out of your HTML A. Say "No" to: 1. Being able to cause side-effects with in-template variable modification B. Say "Yes" to: 1. Being able to loop over data 2. If/Else statements 3. Template includes 4. Simple switch statements 4. Being able to make calls to functions and methods C. Maybe (Future) 1. elseif III. Syntax that is quick to type A. Say "No" to: 1. <%variable%> involves pressing shift twice 2. Smarty's {$variable} syntax is even too much B. Say "Yes" to: 1. {variable} 2. {objectVariable.member} 3. {arrayVariable['title']} 4. {if:thisVariableHasValue} Output here {else} Alternative output here {end} 5. {foreach:dataArray,i,value} {i}: {value}
{end} 6. {switch:variable} default case {case:testVariable} variable: testVariable {case:'textual value'} variable: 'textual value' {endswitch} 7. {rows[0]['name']} 8. {currency(dollarAmount)} - format dollarAmount using the currency() function (previously defined) C. Maybe (Future) 1. {if:a} A has a value {elseif:b} B has a value {elseif:c} C has a value {end} IV. Templates should be compiled/converted to a native scripting language and cached V. Require as little work as possible to plug in to existing web applications Vemplator, by default, looks for templates using the PHP include path environment variable (print get_include_path() to see what it looks like). It also will compile templates into /tmp, assuming you have /tmp and you can write to it.