TinyButStrong: display hierarchies recursively while using objects

Published: 01/07/09 11:15 PM


TinyButStrong is a template Engine for php. It’s pretty easy to use, as long as you have simple cases and flat hierarchies, but it can take some time to manage more complex stuff. This article describes how to display a hierarchy of objects (which means nested lists of objects – thus lists of objects, that contain lists of objects).

To read this article, you should have a glance on the TinyButStrong-manual and you should have an idea of the way TinyButStrong works, .

Let’s say you have managed to represent your website as a model of php-classes.

You have created a class Document, which provides the method getContentParagraphs() among others. This method returns an array of objects of the type Paragraph.

The class Paragraph provides the method getRichText() (and getTitle(), getSubTitle() and so on).

So a single page on your website can contain a list of n documents with m paragraphs. Paragraphs cannot be represented directly, but due to calling the method getRichText().

The easiest way would be to work with arrays. But let’s say, you don’t want to reprocess your clean object-oriented-stuff to tons of arrays, only for a Template-Engine.

Here comes the code. The example classes first (uncomplete, only for you to have an idea):

/*DOCUMENT.php*/
class Document{
  private $contentParagraphs = Array();
  private $title;
  public function &getContentParagraphs(){
    return $this->contentParagraphs;
  }
  public function &getTitle(){
    return $this->title;
  }
  ...
}
/*PARAGRAPH.php*/
class Paragraph{
 private $richtext;
 public function &getRichtext(){
  return $this->richtext;
 }
 ...
}

And here’s how you would manage to implement the presentation with TinyButStrong:

/*index.php*/
/*your logic already built the model, you retreive an array of Document-objects:*/
$documents = $controler->getCurrentFolder()->getDocuments();
//Here comes TinyButStrong
//include and initialize the template engine
include_once('tbs/tbs_class_php5.php') ;
$TBS = new clsTinyButStrong("", "tbs") ;
$TBS->LoadTemplate('templates/overview.xhtml') ;
/*TinyButStrong handles objects, so all we need to do to be able to display our
documents is to make the array $documents visible (giving it the name 'tbsArticleList'),
which is an array of Document-objects:*/
$TBS->MergeBlock('tbsArticleList', 'array','documents');
/*
Here comes the tricky part:
TinyButStrong cannot handle nested blocks (afaik), but needs to be provided so called dynamic
queries. %p1% is a parameter, that must be set in the template itself.
The engine just analyzes any part of the query (the third parameter) and seems not to
care about -> or [] syntax.
The aim of the query is to take the '%p1%'th element of the list documents
(which is a Document-object) and to call the method getContentParagraphs() on it.
*/
$TBS->MergeBlock('tbsParagraphList','array','documents[%p1%][getContentParagraphs]');
$TBS->Show() ;

Here comes the template (without any xhtml, because that just doesn’t matter here):
Note, that htmlconv=no is set, because I use richtext, which may contain html-tags.
I just don’t want them to be escaped. Simply skip htmlconv, if you want to escape html-tags.

<!-- start the list of documents-->
[tbsArticleList;block=begin]
 <!-- display the title of the document / article -->
 [tbsArticleList.getTitle();htmlconv=no]
 <!--start the list of paragraphs for each document, by assigning the parameter p1 the index of
the current article. This is done using the .$ syntax. It is important to wrap extra brackets
around this part, otherwise it will not be evaluated. -->
 [tbsParagraphList;p1=[tbsArticleList.$];block=begin]
  <!-- call the method getRichtext() on the currently iterated paragraph -->
  [tbsParagraphList.getRichtext();htmlconv=no]
 [tbsParagraphList;block=end]
[tbsArticleList;block=end]

That’s it. Pretty simple, he? Just the fact, that the documentation doesn’t cover it can be annoying..


Kommentieren Sie diesen Artikel

MFT-Bitmap corrupt Acronis

Java access-modifiers