Differences

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

quickstart [2009/09/19 11:45]
Kornel created
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 38: Line 42:
===== Layout template ====== ===== Layout template ======
-''<tal:block>'' is an invisible element. You can use it whenever you want some TAL functionality, but don't want to litter code 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>''.
-''main.xhtml'':+[[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>name();</code> | <code xml><div metal:use-macro="name" /></code> | 
 +| <code php>function name($argument_name) {…}</code> | <code xml><div metal:define-macro="name"> 
 +  <div metal:define-slot="argument_name"/> 
 +</div></code> | 
 +| <code php>name("argument data");</code> | <code xml><div metal:use-macro="name"> 
 +  <div metal:fill-slot="argument_name">argument data</div> 
 +</div></code> | 
 + 
 + ''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 metal:define-slot="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>
-</html> 
- 
''subpage.xhtml'': ''subpage.xhtml'':
<code xml> <code xml>
-<tal:block metal:use-macro="main.xhtml/layout";> +<tal:block metal:use-macro="main.xhtml/layout" tal:define="title 'title of the page'&quot;>
-  <;tal:block metal:fill-slot="title">This is title of the page!&lt;/tal:block>+
  <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:*-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.