Sections
At this point you already learned a lot about x:template and how to use it. But there is one big thing which you don't know yet: How to repeat certain parts of the template.
Create the template
If you define a template to display a list, you of course create the list entry only once and want to repeat it. x:template's sections make this possible. Everything you have to do, is to mark the node which shall be repeated. with an attribute name xsection. The value of this attribute is the name of the section.
This is how your template could look like:
<h1>News</h1> <div class="content"></div> <div class="news"> <div class="entry" xsection="news_entry"> <h2>Title</h2> <span class="date">date</span> <hr /> <div class="content"></div> <a href="#" xid="continue">continue</a> </div> </div>
Access the section
To access and repeat a section in your view class, you can either use the [Section] getSectionClone([string] $name)-method. Or you can use the abbreviation [Section] $this([string] $sectionName). Everytime you call one of these methods you get a new XTemplate\Section-object which is a new clone of the original section. These section object implement the selector interface which means you can access the content of the sections just like you are used to access nodes.
Example:
<?php /** * x:template - PHP based template engine * Copyright (c) 2011 - 2012 by Tobias Pohlen <tobias.pohlen@xtemplate.net> * * Released under the GPL License. * * @author Tobias Pohlen * @ignore */ // Load the x:template engine require_once "../../../Engine.php"; /** * A simple class to demonstrate how to use models. * * @author Tobias Pohlen * @version $Id$ * @package xtemplate * @ignore */ class View extends XTemplate\ComfortView { const TEMPLATE = "news_template.html"; const TITLE = 0; const DATE = 1; const CONTENT = 2; const URL = 3; /** * Define the default models */ protected function defineModelInterface() { $this->defModel("news", "array", array()); $this->defModel("description", "string", "Lorem ipsum, dolor sit amet."); } /** * Render the template and the sections */ protected function _render() { $this[".content"] = $this->getDatas("description"); // Iterator over the assigned news foreach ($this->getDatas("news") as $arrNews) { // Create a new section clone $objSection = $this("news_entry"); // Assign the values $objSection["h2"] = $arrNews[self::TITLE]; $objSection[".date"] = $arrNews[self::DATE]; $objSection[".content"] = $arrNews[self::CONTENT]; $objSection["#continue"]->setAttribute("href", $arrNews[self::URL]); } } } ?>
Name scopes
As you probably can see: The class .content is accessed through the view instance and through the section instance, but the result is different. That is because every section is an own name scope. So the select statement only accesses the nodes of the current scope. In the following image every section is marked with an own color. Select statements are only performed in one area.
For example: You select through the object of the main scope and only nodes which are within the light grey part are accessed.
Miscellaneous
The section class also implements the NodeEdit-Interface. So you can modify the section node if you want to.