This shows you the differences between two versions of the page.
en:public:developer:template_system:start [2012/11/20 11:29] admin [Why is separating PHP from templates important?] |
en:public:developer:template_system:start [2012/11/20 11:38] (current) admin |
||
---|---|---|---|
Line 42: | Line 42: | ||
For a syntax comparison of PHP vs ArKadia, see [[Syntax Comparison]]. | For a syntax comparison of PHP vs ArKadia, see [[Syntax Comparison]]. | ||
- | ===== 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.: | ||
- | <code html> | + | ===== Web designers and PHP ===== |
- | <#doctype> | + | |
- | <html> | + | |
- | <head> | + | |
- | <#meta_title> | + | |
- | <#meta_description> | + | |
- | </head> | + | |
- | <body> | + | |
- | <#frame name="topframe"> | + | |
- | </body> | + | |
- | </html> | + | |
- | </code> | + | |
- | Why use a text-based template instead of an XML-based one (like Zope's TAL)? We wanted ArKadia's template | + | A common question: "Web designers have to learn a syntax anyways, why not PHP?" Of course web designers |
- | language to be usable for more than just XML/HTML templates. We use it for emails, JavaScript and CSV. | + | can learn PHP, and they may already be familiar with it. The issue isn't their ability to learn PHP, it |
- | You can use the template language for any text-based format. | + | is about the maintenance of PHP mixed with HTML. **<#tags>** are simpler, more intuitive, and less fragile |
+ | than PHP statements. Templates also restrict what can be put in a template. PHP makes it too easy to add | ||
+ | code into templates that doesn't belong there. You could teach designers the rules of application design, | ||
+ | but this is should be unnecessary (now they are developers!) The PHP manual is intended for developers. | ||
+ | Designers would only need a small fraction of this manual, and it doesn't make it easier for them to find | ||
+ | what they need. ArKadia gives web designers exactly the tools they need, and gives developers fine-grained | ||
+ | control over these tools. | ||
- | ===== Variables ===== | + | ===== Implementation is Important ===== |
- | Variables look like this: <#variable>. When the template engine encounters a variable, it | + | Although ArKadia gives you the tools to make a clean separation of presentation from application code, |
- | evaluates that variable and replaces it with the result. Variable names consist of any | + | it also gives you plenty of room to bend those rules. A poor implementation (i.e. injecting PHP in |
- | combination of alphanumeric characters and the underscore ("_"). The dot (".") also appears | + | templates) will cause more problems than the presentation separation was meant to resolve. The |
- | in variable sections, although that has a special meaning, as indicated below. Importantly, | + | documentation does a good job of indicating what things to watch out for. Also see the [[Best |
- | you cannot have spaces or punctuation characters in variable names. | + | Practices]] section. |
- | + | ||
- | 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"> | + | ===== What does ArKadia look like, and how do I use it? ===== |
- | 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 === | + | The [[Crash Course]] section gives a good overview how ArKadia is typically implemented in a PHP application. |
- | If a variable is false or empty, use given default. Otherwise, use the value of the variable | + | ===== How does it work? ===== |
- | For example: | + | |
- | <code html> | + | |
- | <#value default="nothing"> | + | |
- | </code> | + | |
- | === lowercase === | + | Under the hood, ArKadia compiles copies of the templates as PHP scripts. This way you get the benefits |
+ | of both template tag syntax and the speed of PHP. Compilation happens once when each template is | ||
+ | first invoked, and then the compiled versions are used from that point forward. ArKadia takes care | ||
+ | of this for you, so the template designer just edits the ArKadia templates and never has to manage | ||
+ | the compiled versions. This approach keeps the templates easy to maintain, and yet keeps execution | ||
+ | times extremely fast. Since the compiled versions are PHP, op-code accelerators such as APC or | ||
+ | ZendCache continue to work on the compiled scripts. | ||
- | === uppercase === | + | ===== Why not use XML/XSLT syntax? ===== |
- | + | ||
- | === xmlencode === | + | |
- | + | ||
- | === htmlencode === | + | |
- | + | ||
- | === httpencode === | + | |
- | + | ||
- | === JavascriptEncode === | + | |
- | + | ||
- | === rtfencode === | + | |
+ | There are a couple of good reasons. First, ArKadia can be used for more than just XML/HTML based | ||
+ | templates, such as generating emails, javascript, CSV, and PDF documents. Second, XML/XSLT syntax | ||
+ | is even more verbose and fragile than PHP code! It is perfect for computers, but horrible for humans. | ||
+ | ArKadia is about being easy to read, understand and maintain. |