 |
07.02.2007, 12:39
| Nach oben
#1 | | Benjamin Klaile
Registriert seit: 02.12.2004 Ort: Remagen
Beiträge: 4.481
| Probleme mit dem PEAR Paket HTML_Template_PHPLIB
Hi,
Fragen zum Grund, warum ich dieses Uralt package verwende brauchen nicht gestellt zu werden und ich werde sie auch nicht beantworten.
Ich habe folgenden Code: PHP-Code: $foo = new Foo(); $foo->bar();
class Foo {
private $tpl = null;
public function __construct() { $this->tpl = new Template_PHPLIB(); }
public function bar() {
$this->tpl->setFile('foobar_handle', 'foobar.html'); $this->tpl->setBlock('foobar_handle', 'FOOBAR', 'FOOBAR_block_handle'); $this->tpl->setBlock('FOOBAR_block_handle', 'ITEM', 'ITEM_handle');
$array = array(); $array[] = array('item' => 'hallo'); $array[] = array('item' => 'das ist'); $array[] = array('item' => 'ein test');
foreach($array as $item) { $this->tpl->setVar('item', $item['item']);
$this->tpl->parse('ITEM_handle', 'ITEM', true); }
$this->tpl->parse('FOOBAR', 'ITEM'); var_dump($this->tpl->finish($this->tpl->get('FOOBAR'))); }
}
mit folgenden template-Datei HTML-Code: <!-- BEGIN FOOBAR -->
<h1>{titel}</h1>
<ul>
<!-- BEGIN ITEM -->
<li>{item}</li>
<!-- END ITEM -->
</ul>
<!-- END FOOBAR --> Lasse ich die PHP-Datei laufen so erhalte ich schlichtweg keine Ausgabe  .
Kann mir einer dabei helfen herauszufinden, woran das liegen könnte?
Irgendwie blicke ich durch die Template-Engine nicht so ganz durch.
Und jaaa .. ich weiß, dass wir hier super Tutorials bzgl. Template-Engines haben!
Link zu einem Beispiel in der PEAR-Doku: http://pear.php.net/manual/en/packag...ate-phplib.php
Vielen Dank!
[EDIT]
So ein Mist  .. eine Stunde hat mich jetzt der Blödsinn hier gekostet und ich habe doch einfach nur vergessen dem Konstruktor das Template-Verzeichnis zu übergeben.
Geändert von Ben (07.02.2007 um 12:57 Uhr).
|
| |
07.02.2007, 13:00
| Nach oben
#2 | | Martin Breuer
Registriert seit: 17.08.2005 Ort: Berlin
Beiträge: 1.642
|
hm...lol ich wollte dir grade vorschlagen einfach mal was ausserhalb von foobar anzugeben, ob er dir das ausgibt...
du bist solche alte technik einfach nicht mehr gewohnt nach dem zf was?! |
| |
07.02.2007, 13:06
| Nach oben
#3 | | Benjamin Klaile
Registriert seit: 02.12.2004 Ort: Remagen
Beiträge: 4.481
|
Die Technik an sich ist ja nicht schlecht. Wird ja hier eigentlich ähnlich umgesetzt.
Nunja, so ganz klappt es noch nicht, aber ich hoffe mal, dass ich das Beispiel da nun auch auf den eigentlichen Code anwenden kann.
|
| |
07.02.2007, 14:32
| Nach oben
#4 | | Benjamin Klaile
Registriert seit: 02.12.2004 Ort: Remagen
Beiträge: 4.481
|
Nein, ich schaff das einfach nicht. Ich danke für Hilfe, die dringend und vor allem zeitnah benötigt wird!
[EDIT]
Wo liegt das Problem bzw. was klappt eigentlich nicht?
Also.
Wenn ich das so laufen lasse, wie ich es hier stehen habe wird {poll} nicht ersetzt.
Muss gerade noch etwas rumtesten, werde versuchen das etwas genauer zu beschreiben  ...
Folgende Template-Datei. HTML-Code: <!-- BEGIN POLL -->
<table class="umfrage_table" cellspacing="0" cellpadding="0">
<tr>
<td class="umfrage_ueberschrift">
Aktuelle Umfrage
</td>
</tr>
<tr>
<td class="umfrage_frage">
{question}
</td>
</tr>
<tr>
<td>
<form action="/poll.php">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="0" bgcolor="white">
<!-- BEGIN POLL_ITEM -->
<tr>
<td valign="top" align="center" class="umfrage_radios"><input type="radio" name="reply_id" value="{reply_id}"></td>
<td valign="top" class="umfrage_antworten" >{reply}</td>
</tr>
<!-- END POLL_ITEM -->
</table>
</td>
</tr>
<tr>
<td>
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><span class="s"><a href="/poll.php?action=result&poll_id={poll_id}"><strong>Ergebnis</strong></a></span><br></td>
<td align="right"><input type="submit" value="abstimmen!"></td>
</tr>
</table>
</td>
</tr>
</table>
<input type="hidden" name="action" value="poll">
<input type="hidden" name="poll_id" value="{poll_id}">
</form>
</td>
</tr>
</table>
<!-- END POLL --> Dazu noch diese hier: HTML-Code: <h1>hauptseite</h1>
<div style="margin:40px:padding:20px;border:1px solid #000;">
{poll}
</div>
Hier ist unten drunter.
Folgender PHP-Code PHP-Code: $foo = new Foo(); $bar = $foo->bar();
$tpl = new Template_PHPLIB(PH_TEMPLATE_DIR); $tpl->setFile('main_handle', 'foobar_x.html'); $tpl->setVar('foo', $bar);
$tpl->parse('MAIN', 'main_handle'); $tpl->p('MAIN');
class Foo {
private $tpl = null;
public function __construct() { $this->tpl = new Template_PHPLIB(PH_TEMPLATE_DIR); }
public function bar() {
$this->tpl->setFile('foobar_handle', 'foobar.html'); $this->tpl->setBlock('foobar_handle', 'POLL', 'POLL_handle'); $this->tpl->setBlock('POLL_handle', 'POLL_ITEM', 'POLL_ITEM_handle');
$array = array(); $array[] = array('question' => 'wie heißt du?', 'item' => 'jann'); $array[] = array('question' => 'wie heißt du?', 'item' => 'hendrik'); $array[] = array('question' => 'wie heißt du?', 'item' => 'benni');
$this->tpl->setVar('question', $array[0]['question']);
foreach($array as $item) { $this->tpl->setVar('item', $item['item']);
$this->tpl->parse('POLL_ITEM_handle', 'POLL_ITEM', true); }
$this->tpl->parse('POLL', 'foobar_handle'); return $this->tpl->get('POLL'); }
}
Jetzt fangt net an mit valide und Tabellen .. darum geht es hier gar nicht und ich bin auch nicht dafür verantwortlich!
Bin derzeit wirklich äußerst hilflos ..
Sehe ich den Wald vor lauter Bäumen nicht mehr?
Man muss sich auch nicht PEAR installieren, um das mal selbst austesten zu wollen. Danke nochmals ...
[Nachtrag]
Ein var_dump() des Template-Objekts ergibt Code: <pre style="font-size:12px;">object(Template_PHPLIB)#2 (9) {
["debug"]=>
bool(false)
["file"]=>
array(1) {
["foobar_handle"]=>
string(79) "/www/httpd/../_templates/foobar.html"
}
["file_fallbacks"]=>
array(0) {
}
["root"]=>
string(67) "/www/httpd/../_templates/"
["_varKeys"]=>
array(5) {
["foobar_handle"]=>
string(15) "{foobar_handle}"
["POLL"]=>
string(6) "{POLL}"
["question"]=>
string(10) "{question}"
["item"]=>
string(6) "{item}"
["POLL_ITEM_handle"]=>
string(18) "{POLL_ITEM_handle}"
}
["_varVals"]=>
array(5) {
["foobar_handle"]=>
string(13) "{POLL_handle}"
["POLL"]=>
string(1195) "
<table class="umfrage_table" cellspacing="0" cellpadding="0">
<tr>
<td class="umfrage_ueberschrift">
Aktuelle Umfrage
</td>
</tr>
<tr>
<td class="umfrage_frage">
{question}
</td>
</tr>
<tr>
<td>
<form action="/poll.php">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="0" bgcolor="white">
<!-- BEGIN POLL_ITEM -->
<tr>
<td valign="top" align="center" class="umfrage_radios"><input type="radio" name="reply_id" value="{reply_id}"></td>
<td valign="top" class="umfrage_antworten" >{reply}</td>
</tr>
<!-- END POLL_ITEM -->
</table>
</td>
</tr>
<tr>
<td>
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><span class="s"><a href="/poll.php?action=result&poll_id={poll_id}"><strong>Ergebnis</strong></a></span><br></td>
<td align="right"><input type="submit" value="abstimmen!"></td>
</tr>
</table>
</td>
</tr>
</table>
<input type="hidden" name="action" value="poll">
<input type="hidden" name="poll_id" value="{poll_id}">
</form>
</td>
</tr>
</table>
"
["question"]=>
string(13) "wie heißt du?"
["item"]=>
string(5) "benni"
["POLL_ITEM_handle"]=>
string(0) ""
}
["unknowns"]=>
string(6) "remove"
["haltOnError"]=>
string(6) "report"
["_lastError"]=>
string(32) "subst: unable to load POLL_ITEM."
}
</pre>
Geändert von Ben (07.02.2007 um 15:02 Uhr).
|
| |
07.02.2007, 15:12
| Nach oben
#5 | | Benjamin Klaile
Registriert seit: 02.12.2004 Ort: Remagen
Beiträge: 4.481
|
Das Problem liegt glaube ich an der Stelle, dass beim parse(handle, 'POLL'); das "handle" nicht bekannt ist, weil es zu einem anderen Template-Objekt außerhalb der Klasse gehört.
Ich will aber das Template-Objekt eigentlich nicht in die Klasse übergeben. Gründe seien mal dahingestellt. |
| |
07.02.2007, 15:15
| Nach oben
#6 | | Erfahrener Benutzer
Registriert seit: 27.02.2006
Beiträge: 146
|
Hallo,
ohne dass ich die Bibiothek genau kenne .... {item} hatte ich im Template im Bereich POLL_ITEM nicht gefunden. Und {question} liegt zwar im Bereich POLL, aber da vermisse ich das Setzen des Wertes in der Template Instanz.
Gruß,
Jumper, the II.
|
| |
07.02.2007, 15:20
| Nach oben
#7 | | Benjamin Klaile
Registriert seit: 02.12.2004 Ort: Remagen
Beiträge: 4.481
|
Kleinere Anpassungsfehler. (kann hier ja nicht den Originalcode reinstellen, sondern habe ja nur ein Beispiel gebaut, welches das verdeutlicht, was im Original passiert.)
Das ist aber nichts, was das Problem entstehen lässt. |
| |
07.02.2007, 15:28
| Nach oben
#8 | | Benjamin Klaile
Registriert seit: 02.12.2004 Ort: Remagen
Beiträge: 4.481
|
Vielleicht hilft es? PHP-Code: <?php // vim: set expandtab tabstop=4 shiftwidth=4: // This code that was derived from the original PHPLIB Template class // is copyright by Kristian Koehntopp, NetUSE AG and was released // under the LGPL. // // Authors: Kristian Koehntopp <kris@koehntopp.de> (original from PHPLIB) // Bjoern Schotte <bjoern@rent-a-phpwizard.de> (PEARification) // Martin Jansen <mj@php.net> (PEAR conformance) // // $Id: PHPLIB.php,v 1.14 2003/06/11 06:03:32 bjoern Exp $ //
require_once "PEAR.php";
/** * Converted PHPLIB Template class * * For those who want to use PHPLIB's fine template class, * here's a PEAR conforming class with the original PHPLIB * template code from phplib-stable CVS. Original author * was Kristian Koehntopp <kris@koehntopp.de> * * @author Bjoern Schotte <bjoern@rent-a-phpwizard.de> * @author Martin Jansen <mj@php.net> (PEAR conformance) * @version 1.0 */ class Template_PHPLIB { /** * If set, echo assignments * @var bool */ var $debug = false;
/** * $file[handle] = "filename"; * @var array */ var $file = array();
/** * fallback paths that should be defined in a child class * @var array */ var $file_fallbacks = array();
/** * Relative filenames are relative to this pathname * @var string */ var $root = "";
/* * $_varKeys[key] = "key" * @var array */ var $_varKeys = array();
/** * $_varVals[key] = "value"; * @var array */ var $_varVals = array();
/** * "remove" => remove undefined variables * "comment" => replace undefined variables with comments * "keep" => keep undefined variables * @var string */ var $unknowns = "remove";
/** * "yes" => halt, "report" => report error, continue, "no" => ignore error quietly * @var string */ var $haltOnError = "report";
/** * The last error message is retained here * @var string * @see halt */ var $_lastError = "";
/** * Constructor * * @access public * @param string template root directory * @param string how to handle unknown variables * @param array fallback paths */ function Template_PHPLIB($root = ".", $unknowns = "remove", $fallback="") { $this->setRoot($root); $this->setUnknowns($unknowns); if (is_array($fallback)) $this->file_fallbacks = $fallback; }
/** * Sets the template directory * * @access public * @param string new template directory * @return bool */ function setRoot($root) { if (!is_dir($root)) { $this->halt("setRoot: $root is not a directory."); return false; }
$this->root = $root;
return true; }
/** * What to do with unknown variables * * three possible values: * * - "remove" will remove unknown variables * (don't use this if you define CSS in your page) * - "comment" will replace undefined variables with comments * - "keep" will keep undefined variables as-is * * @access public * @param string unknowns */ function setUnknowns($unknowns = "keep") { $this->unknowns = $unknowns; }
/** * Set appropriate template files * * With this method you set the template files you want to use. * Either you supply an associative array with key/value pairs * where the key is the handle for the filname and the value * is the filename itself, or you define $handle as the file name * handle and $filename as the filename if you want to define only * one template. * * @access public * @param mixed handle for a filename or array with handle/name value pairs * @param string name of template file * @return bool */ function setFile($handle, $filename = "") { if (!is_array($handle)) {
if ($filename == "") { $this->halt("setFile: For handle $handle filename is empty."); return false; }
$this->file[$handle] = $this->_filename($filename);
} else {
reset($handle); while (list($h, $f) = each($handle)) { $this->file[$h] = $this->_filename($f); } } }
/** * Set a block in the appropriate template handle * * By setting a block like that: * * <!-- BEGIN blockname --> * html code * <!-- END blockname --> * * you can easily do repeating HTML code, i.e. output * database data nice formatted into a HTML table where * each DB row is placed into a HTML table row which is * defined in this block. * It extracts the template $handle from $parent and places * variable {$name} instead. *funab * @access public * @param string parent handle * @param string block name handle * @param string variable substitution name */ function setBlock($parent, $handle, $name = "") { if (!$this->_loadFile($parent)) { $this->halt("setBlock: unable to load $parent."); return false; }
if ($name == "") { $name = $handle; }
$str = $this->getVar($parent); $reg = "/[ \t]*<!--\s+BEGIN $handle\s+-->\s*?\n?(\s*.*?\n?)\s*<!--\s+END $handle\s+-->\s*?\n?/sm"; preg_match_all($reg, $str, $m); $str = preg_replace($reg, "{" . "$name}", $str);
if (isset($m[1][0])) $this->setVar($handle, $m[1][0]); $this->setVar($parent, $str); }
/** * Set corresponding substitutions for placeholders * * @access public * @param string name of a variable that is to be defined or an array of variables with value substitution as key/value pairs * @param string value of that variable * @param boolean if true, the value is appended to the variable's existing value */ function setVar($varname, $value = "", $append = false) { if (!is_array($varname)) {
if (!empty($varname)) if ($this->debug) print "scalar: set *$varname* to *$value*<br>\n";
$this->_varKeys[$varname] = $this->_varname($varname); ($append) ? $this->_varVals[$varname] .= $value : $this->_varVals[$varname] = $value;
} else { reset($varname);
while (list($k, $v) = each($varname)) { if (!empty($k)) if ($this->debug) print "array: set *$k* to *$v*<br>\n";
$this->_varKeys[$k] = $this->_varname($k); ($append) ? $this->_varVals[$k] .= $v : $this->_varVals[$k] = $v; } } }
/** * Substitute variables in handle $handle * * @access public * @param string name of handle * @return mixed string substituted content of handle */ function subst($handle) { if (!$this->_loadFile($handle)) { $this->halt("subst: unable to load $handle."); return false; }
return @str_replace($this->_varKeys, $this->_varVals, $this->getVar($handle)); }
/** * Same as subst but printing the result * * @access public * @brother subst * @param string handle of template * @return bool always false */ function pSubst($handle) { print $this->subst($handle); return false; }
/** * Parse handle into target * * Parses handle $handle into $target, eventually * appending handle at $target if $append is defined * as TRUE. * * @access public * @param string target handle to parse into * @param string which handle should be parsed * @param boolean append it to $target or not? * @return string parsed handle */ function parse($target, $handle, $append = false) { if (!is_array($handle)) { $str = $this->subst($handle);
($append) ? $this->setVar($target, $this->getVar($target) . $str) : $this->setVar($target, $str); } else { reset($handle);
while (list(, $h) = each($handle)) { $str = $this->subst($h); $this->setVar($target, $str); } }
return $str; }
/** * Same as parse, but printing it. * * @access public * @brother parse * @param string target to parse into * @param string handle which should be parsed * @param should $handle be appended to $target? * @return bool */ function pParse($target, $handle, $append = false) { print $this->finish($this->parse($target, $handle, $append)); return false; }
/** * Return all defined variables and their values * * @access public * @return array with all defined variables and their values */ function getVars() { reset($this->_varKeys);
while (list($k, ) = each($this->_varKeys)) { $result[$k] = $this->getVar($k); }
return $result; }
/** * Return one or more specific variable(s) with their values. * * @access public * @param mixed array with variable names or one variable name as a string * @return mixed array of variable names with their values or value of one specific variable */ function getVar($varname) { if (!is_array($varname)) { if (isset($this->_varVals[$varname])) { return $this->_varVals[$varname]; } else { return ""; } } else { reset($varname);
while (list($k, ) = each($varname)) { $result[$k] = (isset($this->_varVals[$k])) ? $this->_varVals[$k] : ""; }
return $result; } }
/** * Get undefined values of a handle * * @access public * @param string handle name * @return mixed false if an error occured or the undefined values */ function getUndefined($handle) { if (!$this->_loadFile($handle)) { $this->halt("getUndefined: unable to load $handle."); return false; }
preg_match_all("/{([^ \t\r\n}]+)}/", $this->getVar($handle), $m); $m = $m[1]; if (!is_array($m)) { return false; }
reset($m); while (list(, $v) = each($m)) { if (!isset($this->_varKeys[$v])) { $result[$v] = $v; } }
if (isset($result) && count($result)) { return $result; } else { return false; } }
/** * Finish string * * @access public * @param string string to finish * @return finished, i.e. substituted string */ function finish($str) { switch ($this->unknowns) { case "remove": | |