By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,434 Members | 2,963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,434 IT Pros & Developers. It's quick & easy.

how to best store nested values

Dormilich
Expert Mod 5K+
P: 8,639
Hi,

short: I want to figure out which construct is best suited to store nested values in a template class.

background: For my website, I have a template class, that provides my output class with the necessary files (filenames, resp.) which to include/process.
example: for a given site ("sample.php") I instantiate the template and feed it to my output class
Expand|Select|Wrap|Line Numbers
  1. $currentTemplate = new Template("sample");
  2. $sample = new HTMLoutput($currentTemplate);
  3. // some code
  4. $sample->writeContent();
  5. // some other code
the necessary files for that are static html includes, xml files, xsl files and their parameters, so the Template class will have to provide these ones according to the wantet template (in this case it's the "sample" template). Currently the HTMLoutput function uses the template in several of its methods (e.g. writeContent(), writeNavi(),...) so I need the files depending on template and method. furthermore, if I use xml I need a set of files (namely xml, xsl and parameter (xml → xhtml)). this would map to a threedimensional table.

question now: how do I best fit such a "table" in my template class?

possible solutions (I can think of):
- threedimensional array (urgh, there should be better ways)
- one or more MySQL tables (there are currently not so many values (~ 20), overkill?)
- an object (currently I have no idea how such an object should look like)
- a configuration file

any ideas how to solve this are highly appreciated

thanks in advance,
Dormi

PS: I'm using Apache & PHP 5.2.6
Nov 5 '08 #1
Share this Question
Share on Google+
4 Replies


dlite922
Expert 100+
P: 1,584
Well i'm not sure why you're doing your templating this way. Why not just do includes and use a templating system like Smarty ?

The object method would that there is one object. A member of that object is itself another object (2 dimensional), then that second object will have a third object as its member. so that to access the most inner object you would do

$ob1->ob2->ob3

Explain why you're doing this. Chances are you're not the only that has headed down this road and most likely they have turned back thus this is why it may not be the best way to achieve the solution. So tell us why you have a class building your files instead of just calling another file. or having different parts included at run time. This should not be that complicated.




Dan
Nov 7 '08 #2

Dormilich
Expert Mod 5K+
P: 8,639
Well i'm not sure why you're doing your templating this way. Why not just do includes and use a templating system like Smarty ?
well, template may not be the best word for the system I use. I use a lot of XSLT to produce my output (about 50% of my xhtml is generated from xml). So basicly my "template" has to provide either an include file or an xml file along with the matching xsl file and the parameters.

do you know any free templating system that does this?

Explain why you're doing this. Chances are you're not the only that has headed down this road and most likely they have turned back thus this is why it may not be the best way to achieve the solution. So tell us why you have a class building your files instead of just calling another file. or having different parts included at run time. This should not be that complicated.
as already stated I do it because of xslt. to be more precise (and the point why I use xslt at all) i can control the display of the pages (i.e. I can remove a page from display and automatically update all navigations that use this one control file (xml) so I don't have to worry about broken or missing links (btw. navigation consists of: main navigation bar, relational navigation and category pages))

and another point why I don't use template systems, I use this mainly to get coding experience. kind of playground.

for the mere chance that you want to have a look at the files, I can mail them to you (currently working under php4, php5 still under development, thus the question)

regards

PS
The object method would that there is one object. A member of that object is itself another object (2 dimensional), then that second object will have a third object as its member. so that to access the most inner object you would do

$ob1->ob2->ob3
this would sacrifice security, since I have to make the properties public (and writable) and in principle it looks like a 3D array to me. You may say that it doesn't matter, but what if the next admin has to update something? I don't want him to do anything in the php code unless there's no other way. The xml files can be protected from failing with a DTD or XSD, but there's no way to validate php like that.
Nov 7 '08 #3

Dormilich
Expert Mod 5K+
P: 8,639
final conclusion.

in the end I went for a combination of config file, array and object. the info is read from the file giving the template name as selector. then the info is deserialized into an array of objects.

my template (not the config file) looks now like this. the single pages are requested as "file.php?id=page_id"
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // load scripts
  3.     require_once("load.main.php");
  4. // prepare classes
  5.     $main  = new HTMLplus("main");
  6. // start content output/layout
  7.     $main->include("prolog");
  8.     $main->Titel();
  9.     $main->Meta();
  10.     $main->CSS();
  11.     $main->JS();
  12. ?>
  13. <!-- some static <head> files -->
  14.     </head>
  15.     <body>
  16. <!-- header section html code -->
  17. <?php
  18.     $main->Navi();
  19.     $main->Inhalt();
  20. ?>
  21. <!-- footer section html code -->
  22.     </body>
  23. </html>
the class HTMLplus is used to redirect the methods in the template file to the corresponding methods in the handling class.
and the class that handles all the called functions.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /**
  3.  * this class defines methods to print template related files to screen.
  4.  * the necessary template has to be provided by the subclass.
  5.  */
  6. abstract class aHTML
  7. {
  8.     /**
  9.      * Template object to get template files from.
  10.      */
  11.     protected $inTPL = NULL;
  12.  
  13.     /**
  14.      * read out the <interface> from template config xml file. act accordingly
  15.      *   backend method for template include processing. the frontend method
  16.      * calls this method as TransformTemplate(__FUNCTION__) (magic constant
  17.      * for manual call). attention is to be kept, if frontend method names
  18.      * are to be changed, a template config xml update is to be made then -
  19.      * or the parameter has to be manually entered... if all goes well, this
  20.      * will print the result.
  21.      *   to avoid typos, the keys of Template::$mp_page_obj are taken from 
  22.      * the template config xml using its <interface>, so no keys have to be
  23.      * entered manually.
  24.      */
  25.     protected function TransformTemplate($function_name, $f_empty = XSLT_EMPTY_RESULT)
  26.     {
  27.                 # if this happens, hang up the responsible developer
  28.         if (!($this->inTPL instanceof Template))
  29.         {
  30.             $emsg = "extended class failed to load correct object";
  31.             throw new CodeException(30, $emsg);
  32.         }
  33.                 # set parameter for interface read out
  34.         $param = array('funktion' => $function_name, 'page' => $this->inTPL->Name); 
  35.         try {
  36.                     # make array from interface, mapping calling method's name to
  37.                     # listed object keys, which are used for coming output.
  38.             $rg_funcs = $this->readInterface($param);
  39.                     # use each key (i.e. each object)
  40.             foreach ($rg_funcs as $method)
  41.             {
  42.                         # get file names for current "template method" 
  43.                 $inSEITE_method = $this->inTPL->getObject($method);
  44.                 if ($inSEITE_method->INCLUDE)
  45.                 {
  46.                     $this->inTPL->getInclude($method);
  47.                 }
  48.                 else
  49.                 {
  50.                             # returns parameter value or ''
  51.                     $par_val = $this->transformParam($method);
  52.                             # let do processing and printing
  53.                     print $this->Transform($inSEITE_method, $par_val, $f_empty);
  54.                 }
  55.             }
  56.         }
  57.         catch (MyException $me) // xml/xsl coding error
  58.         {
  59.             print $me;
  60.         }
  61.         return true;
  62.     }
  63.  
  64.     /**
  65.      * do an XSL transformation based on the given page info object.
  66.      * dynamic parameter values can be passed.
  67.      * the "Seite" object holds the file(s) necessary for a XHTML snippet
  68.      */
  69.     protected function Transform(Seite $inSEITE_in, $sy_par = NULL, $f_empty = XSLT_FORCE_RESULT) 
  70.     {
  71.                 # get filenames from Template
  72.         $rf_xml = $inSEITE_in->XML;
  73.         $rf_xsl = $inSEITE_in->XSL;
  74.                 # set parameter correctly
  75.         $param = ($sy_par === NULL and isset($this->ID)) ? $this->ID : $sy_par;
  76.         $mp_param = ($inSEITE_in->PAR_NAME) ? $inSEITE_in->getValue('PAR_ARRAY', $param) : array();
  77.                 # start XSLT
  78.         $inXSLT_process = new XSLTransform();
  79.         try {
  80.             $data = $inXSLT_process->Process($rf_xml, $rf_xsl, $mp_param, (bool) $f_empty);
  81.         }
  82.         catch (ProcException $pe)
  83.         {
  84.                 # handling incomplete
  85.         }
  86.  
  87.         return $data;
  88.     }
  89.  
  90.     /**
  91.      * builds a required array from template config xml by means of WDDX.
  92.      * currently used only once, but applicable if parameter array is
  93.      * necessary.
  94.      */
  95.     private function readInterface($param)
  96.     {
  97.         $xml = "files.template.xml";
  98.         $xsl = "sys.interface.xsl";
  99.         $inXSLT_process = new XSLTransform();
  100.         $inWDDX_array   = new WDDXProcess(__METHOD__);
  101.         $ch_data = $inXSLT_process->Process($xml, $xsl, $param);
  102.         $inWDDX_array->deserialXML($ch_data);
  103.         $mp_result = $inWDDX_array->isResultArray(XSLT_EMPTY_RESULT);
  104.         return $mp_result;
  105.     }
  106.  
  107.     /**
  108.      * gets XSLT parameter value (if max. 1) from template config xml.
  109.      * if parameter there is set to "get value at runtime" (i.e. no value)
  110.      * set to page ID (if any) or ''.
  111.      */
  112.     private function transformParam($method)
  113.     {
  114.         $xml = "files.template.xml";
  115.         $xsl = "sys.parameter.value.xsl";
  116.         $param = array('method' => $method, 'page' => $this->inTPL->Name);
  117.         $inXSLT_para = new XSLTransform();
  118.         $param = $inXSLT_para->Process($xml, $xsl, $param, XSLT_EMPTY_RESULT);
  119.                 # set parameter to page ID if available
  120.         if (!$param)
  121.         {
  122.             $param = (isset($this->ID)) ? $this->ID : '';
  123.         }
  124.         return $param;
  125.     }
  126.  
  127.     abstract public function Inhalt();
  128. }
  129. ?>
and finally a personal tip for OOPhp coders: __get(), __call() and __autoload() are awesome and handy functions.
Nov 29 '08 #4

dlite922
Expert 100+
P: 1,584
@Dormilich
...yes they are! :)



Dan
Dec 1 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.