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 - $currentTemplate = new Template("sample");
-
$sample = new HTMLoutput($currentTemplate);
-
// some code
-
$sample->writeContent();
-
// 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
4 1650
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
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.
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" - <?php
-
// load scripts
-
require_once("load.main.php");
-
// prepare classes
-
$main = new HTMLplus("main");
-
// start content output/layout
-
$main->include("prolog");
-
$main->Titel();
-
$main->Meta();
-
$main->CSS();
-
$main->JS();
-
?>
-
<!-- some static <head> files -->
-
</head>
-
<body>
-
<!-- header section html code -->
-
<?php
-
$main->Navi();
-
$main->Inhalt();
-
?>
-
<!-- footer section html code -->
-
</body>
-
</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. - <?php
-
/**
-
* this class defines methods to print template related files to screen.
-
* the necessary template has to be provided by the subclass.
-
*/
-
abstract class aHTML
-
{
-
/**
-
* Template object to get template files from.
-
*/
-
protected $inTPL = NULL;
-
-
/**
-
* read out the <interface> from template config xml file. act accordingly
-
* backend method for template include processing. the frontend method
-
* calls this method as TransformTemplate(__FUNCTION__) (magic constant
-
* for manual call). attention is to be kept, if frontend method names
-
* are to be changed, a template config xml update is to be made then -
-
* or the parameter has to be manually entered... if all goes well, this
-
* will print the result.
-
* to avoid typos, the keys of Template::$mp_page_obj are taken from
-
* the template config xml using its <interface>, so no keys have to be
-
* entered manually.
-
*/
-
protected function TransformTemplate($function_name, $f_empty = XSLT_EMPTY_RESULT)
-
{
-
# if this happens, hang up the responsible developer
-
if (!($this->inTPL instanceof Template))
-
{
-
$emsg = "extended class failed to load correct object";
-
throw new CodeException(30, $emsg);
-
}
-
# set parameter for interface read out
-
$param = array('funktion' => $function_name, 'page' => $this->inTPL->Name);
-
try {
-
# make array from interface, mapping calling method's name to
-
# listed object keys, which are used for coming output.
-
$rg_funcs = $this->readInterface($param);
-
# use each key (i.e. each object)
-
foreach ($rg_funcs as $method)
-
{
-
# get file names for current "template method"
-
$inSEITE_method = $this->inTPL->getObject($method);
-
if ($inSEITE_method->INCLUDE)
-
{
-
$this->inTPL->getInclude($method);
-
}
-
else
-
{
-
# returns parameter value or ''
-
$par_val = $this->transformParam($method);
-
# let do processing and printing
-
print $this->Transform($inSEITE_method, $par_val, $f_empty);
-
}
-
}
-
}
-
catch (MyException $me) // xml/xsl coding error
-
{
-
print $me;
-
}
-
return true;
-
}
-
-
/**
-
* do an XSL transformation based on the given page info object.
-
* dynamic parameter values can be passed.
-
* the "Seite" object holds the file(s) necessary for a XHTML snippet
-
*/
-
protected function Transform(Seite $inSEITE_in, $sy_par = NULL, $f_empty = XSLT_FORCE_RESULT)
-
{
-
# get filenames from Template
-
$rf_xml = $inSEITE_in->XML;
-
$rf_xsl = $inSEITE_in->XSL;
-
# set parameter correctly
-
$param = ($sy_par === NULL and isset($this->ID)) ? $this->ID : $sy_par;
-
$mp_param = ($inSEITE_in->PAR_NAME) ? $inSEITE_in->getValue('PAR_ARRAY', $param) : array();
-
# start XSLT
-
$inXSLT_process = new XSLTransform();
-
try {
-
$data = $inXSLT_process->Process($rf_xml, $rf_xsl, $mp_param, (bool) $f_empty);
-
}
-
catch (ProcException $pe)
-
{
-
# handling incomplete
-
}
-
-
return $data;
-
}
-
-
/**
-
* builds a required array from template config xml by means of WDDX.
-
* currently used only once, but applicable if parameter array is
-
* necessary.
-
*/
-
private function readInterface($param)
-
{
-
$xml = "files.template.xml";
-
$xsl = "sys.interface.xsl";
-
$inXSLT_process = new XSLTransform();
-
$inWDDX_array = new WDDXProcess(__METHOD__);
-
$ch_data = $inXSLT_process->Process($xml, $xsl, $param);
-
$inWDDX_array->deserialXML($ch_data);
-
$mp_result = $inWDDX_array->isResultArray(XSLT_EMPTY_RESULT);
-
return $mp_result;
-
}
-
-
/**
-
* gets XSLT parameter value (if max. 1) from template config xml.
-
* if parameter there is set to "get value at runtime" (i.e. no value)
-
* set to page ID (if any) or ''.
-
*/
-
private function transformParam($method)
-
{
-
$xml = "files.template.xml";
-
$xsl = "sys.parameter.value.xsl";
-
$param = array('method' => $method, 'page' => $this->inTPL->Name);
-
$inXSLT_para = new XSLTransform();
-
$param = $inXSLT_para->Process($xml, $xsl, $param, XSLT_EMPTY_RESULT);
-
# set parameter to page ID if available
-
if (!$param)
-
{
-
$param = (isset($this->ID)) ? $this->ID : '';
-
}
-
return $param;
-
}
-
-
abstract public function Inhalt();
-
}
-
?>
and finally a personal tip for OOPhp coders: __get(), __call() and __autoload() are awesome and handy functions.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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>...
|
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...
|
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...
|
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...
|
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...
|
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.
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
| |