473,387 Members | 1,596 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to best store nested values

Dormilich
8,658 Expert Mod 8TB
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
4 1650
dlite922
1,584 Expert 1GB
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
8,658 Expert Mod 8TB
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
8,658 Expert Mod 8TB
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
1,584 Expert 1GB
@Dormilich
...yes they are! :)



Dan
Dec 1 '08 #5

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

Similar topics

12
by: jacob nikom | last post by:
Hi, I would like to store XML files in MySQL. What is the best solution: 1. Convert it to string and store it as CLOB/text 2. Serialize it and store as byte array 3. Flatten it out and create...
16
by: PeteCresswell | last post by:
I was happily coding along, putting some calculation results (rolling annualized rates of return - too compute-intensive to calculate on-the-fly - had tb staged beforehand via a batch job) into...
17
by: Woody Splawn | last post by:
I am finding that time after time I have instances where I need to access information in a variable that is public. At the same time, the books I read say that one should not use public variables...
2
by: Joe | last post by:
Anyone can suggest the best method of reading XML and adding data to ListView? Here is the xml data structure:: <xml> <site> <url>http://www.yahoo.com</url> <lastupdate></lastupdate>...
4
by: ink | last post by:
Hi all, I am trying to pull some financial data off of an HTML web page so that I can store it in a Database for Sorting and filtering. I have been thinking about this for some time and trying...
0
by: coolsti | last post by:
To the more experienced C# programmers, how do I do this best? I have a 2 dimensional mapping of values in a database, which represent a somewhat round (but not exactly round) shape when drawn out...
9
by: Gabriel Rossetti | last post by:
Hello, I can't get getattr() to return nested functions, I tried this : .... def titi(): .... pass .... f = getattr(toto, "titi") .... print str(f) .... Traceback...
2
by: John O'Hagan | last post by:
Hi Pythonistas, I'm looking for the best way to pass an arbitrary number and type of variables created by one function to another. They can't be global because they may have different values...
0
by: John O'Hagan | last post by:
On Tue Sep 30 11:32:41 CEST 2008, Steven D'Aprano Thanks, both to you and Bruno for pointing this out, I'll certainly be using it in future.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.