Differences

This shows you the differences between two versions of the page.

quickstart [2009/09/19 12:07]
Kornel
quickstart [2014/04/05 02:52] (current)
81.187.95.96 old revision restored
Line 9: Line 9:
<code php><?php <code php><?php
-require_once "PHPTAL/PHPTAL.php"+require_once "PHPTAL/PHPTAL.php";;
// name of template. You can set/change it later with setTemplate() // name of template. You can set/change it later with setTemplate()
Line 27: Line 27:
===== Gathering of data ===== ===== Gathering of data =====
-Avoid passing PHPTAL object around your entire program. MVC programs should be independent of their view. Instead of setting variables directly on PHPTAL object, make your methods return all required information. You can also pass helper objects to PHPTAL which will load data on demand.+This is not a requirement, but good advice: avoid passing PHPTAL object (or template abstraction wrapper) around your entire program. MVC programs should be independent of their views (mental exercise: would your application break if there was no view?).  
 + 
 +Instead of setting variables directly on PHPTAL object, make your methods //return// all required information. You can also pass helper objects to PHPTAL which will load data on demand.
<code php><?php <code php><?php
Line 33: Line 35:
$result_array['logged_in_user'] = find_logged_in_user(); $result_array['logged_in_user'] = find_logged_in_user();
$result_array['sidebar'] = new SidebarHelper(); $result_array['sidebar'] = new SidebarHelper();
 +
 +$phptal = new PHPTAL($page_template); // create PHPTAL object *after* getting data
foreach($result_array as $k => $v) $phptal->set($k,$v);</code> foreach($result_array as $k => $v) $phptal->set($k,$v);</code>
Line 40: Line 44:
''[[http://phptal.org/manual/en/split/phptal-blocks.html|<tal:block>]]'' is an invisible element you can use anywhere. It's useful when you want some TAL functionality, but don't want to litter markup with unnecessary ''<div>'' or ''<span>''. ''[[http://phptal.org/manual/en/split/phptal-blocks.html|<tal:block>]]'' is an invisible element you can use anywhere. It's useful when you want some TAL functionality, but don't want to litter markup with unnecessary ''<div>'' or ''<span>''.
-[[http://phptal.org/manual/en/#metal|METAL]] macros can be used to execute code from other templates. Conceptually, you macros are very similar to PHP functions:+[[http://phptal.org/manual/en/#metal|METAL]] macros can be used to execute code from other templates. Conceptually macros are very similar to PHP functions:
| <code php>function name() {…}</code> | <code xml><div metal:define-macro="name">…</div></code> | | <code php>function name() {…}</code> | <code xml><div metal:define-macro="name">…</div></code> |
Line 51: Line 55:
</div></code> | </div></code> |
- + ''main.xhtml'':
-''main.xhtml'':+
<code xml> <code xml>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\\  
-<html xmlns="http://www.w3.org/1999/xhtml" metal:define-macro="layout"> +<html xmlns="http://www.w3.org/1999/xhtml" metal:define-macro="layout">\\  
-  <head> +  <head>  
-    <title tal:content="title"/> +    <title tal:content="title"/>  
-    <tal:block metal:define-slot="other-stuff-in-head"/> +    <tal:block metal:define-slot="other-stuff-in-head"/>  
-  </head>+  </head>
  <body>   <body>
-    <div id="main"><tal:block metal:define-slot="maincontent"/></div> +    <div id="main"><tal:block metal:define-slot="maincontent"/></div>\\  
-  </body> +  </body>  
-</html>+</html>
</code> </code>
Line 71: Line 74:
<code xml> <code xml>
-<tal:block metal:use-macro="main.xhtml/layout" tal:define="title 'title of the page'>+<tal:block metal:use-macro="main.xhtml/layout" tal:define="title 'title of the page'">
  <p metal:fill-slot="maincontent">Hello world</p>   <p metal:fill-slot="maincontent">Hello world</p>
</tal:block> </tal:block>
</code> </code>
-Note that the code didn't use ''metal:define-slot''. Macros "see" variables defined in templates that call them, so simple values can be easily passed using variables.+Note that the code didn't use ''metal:*-slot'' to set title of the page. Macros "see" variables [[http://phptal.org/manual/en/split/tal-define.html|defined]] in templates that call them, so simple values can be easily passed using variables.
You don't have to fill all slots. Here ''main.xhtml'' defines ''other-stuff-in-head'' slot, which you could use e.g. for adding JavaScript or link to CSS, but you don't have to. You don't have to fill all slots. Here ''main.xhtml'' defines ''other-stuff-in-head'' slot, which you could use e.g. for adding JavaScript or link to CSS, but you don't have to.
-