Versión en castellano

Wednesday 2 January 2013

Siviglia Templating Published

I've finally published a first version of my templating system for php , Siviglia Templating.
But, despite the code is relatively new, the idea behind is something i've been working on for some years now.This implementation is the first that fullfills all the requisites i wanted for it.
There's a manual with examples that i hope is enough to get started, but, after some years building web sites based with it, i'd like to share some advanced uses of the template system, not covered in the manual.

As you can use a widget path to define where the system will look for the widgets used in a template, you can have different paths for different user agents (full-blown browsers, mobile browsers, search engines), output types (html, json, xml) or any other criteria (skins, language...).
You can combine this with the way the engine works when a certain subcomponent of a widget is not specified, or doesnt exist..
For example, suppose you have 3 news boxes in your homepage.Your template may look like this:


[*PAGE]
    [_CONTENTS]
          [*HOMEPAGE_NEWS_CONTAINER]
                   [_NEWSBOX1]...[#]
                   [_NEWSBOX2]...[#]
                   [_NEWSBOX3]...[#]
          [#]
     [#]
[#]

Now, you want to have a version for mobile devices, that only displays the news box specified in NEWSBOX1 in the homepage.

You could do it like this:


[*PAGE]
    [_CONTENTS]
          <?php if(!$deviceIsMobile) {?>
          [*HOMEPAGE_NEWS_CONTAINER]
                   [_NEWSBOX1]...[#]
                   [_NEWSBOX2]...[#]
                   [_NEWSBOX3]...[#]
          [#]
          <?php } else { ?>
          [*HOMEPAGE_NEWS_CONTAINER]
                   [_NEWSBOX1]...[#]
          [#]
          <?php } ?>
     [#]
[#]

The problem with this approach is, not only that it's a bit uglier, but also, you'll have to code the HOMEPAGE_NEWS_CONTAINER widget to work well if all three or only one of the newsboxes are specified.
An alternative solution, is to create two different HOMEPAGE_NEWS_CONTAINER widgets, stored in the same relative path, but in two different folders.And to leave the template device independent.

The device detection code now is moved to the template engine initialization.



  ....
  /* You'll need a different cache file for each kind of device. Specify a file name
     for the cache file, and specify it along with the TEMPLATE definition */
  if($deviceIsMobile)
  {
     $widgetPath=array("/widgets/mobile/");
     $cacheFile=....;
  }
  else
  {
     $widgetPath=array("/widgets/nonMobile/");
     $cacheFile=....;
  }
  $widgetPath[]="/widgets/common";
   
  $oManager=new CLayoutManager("html",$widgetPath,array("L"=>array("lang"=>"en")));

    $definition=array("TEMPLATE"=>PROJECTPATH."/templates/PathWidget.html","TARGET"=>$cacheFile);

  $oManager->renderLayout($definition,$oLParser,true); 

Now, you can add two different implementations for the HOMEPAGE_NEWS_CONTAINER,
one located in widgets/mobile/HOMEPAGE_NEWS_CONTAINER.wid,
and other in widgets/nonMobile/HOMEPAGE_NEWS_CONTAINER.wid.
In the mobile version, only one of the newsboxes is handled, while the other are left empty.Of course, most probably, even the PAGE widget is different for each device, so the same strategy apply. In this way, the template file is kept clean, device-independent.And, also, expressed in a very high level, so in a glance, its purpose can be understood.

No comments:

Post a Comment