PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Template-Class


BruceCompanys
31.12.2006, 15:18:18
<?php
###########################################
#Author: BruceCompanys (C) => Simon Brass
#Required: 31.12.2006
#
#OpenSource
###########################################

class template {

var $tmp_file;
var $error = 'Template nicht vorhanden';
var $content;
var $vars = array();

function template($file) {

$this->tmp_file = $file;
$this->content = "";
$this->read_template();

}

function read_template() {

$file = fopen($this->tmp_file, "r");

if(!$file) {
echo $this->error;
} else {

while(!feof($file)) {
$temp = fgets($file, 4096);
$this->content .= $temp;

}

}
}

function set_var($title, $value) {

if(!isset($this->vars[$title])){
$this->vars[strtoupper($title)] = $value;
} else {

$thisk = $this->vars[strtoupper($title)];

if(!is_array($thisk)){
settype($thisk, "array");
}

$count = count($thisk);
$thisk[$count] = $value;
$this->vars[strtoupper($title)] = $thisk;
}

}

function set_array($array){

foreach($array as $k => $v){
if(!isset($this->vars[strtoupper($k)])){
$this->vars[strtoupper($k)] = $v;
} else {

$thisk = $this->vars[strtoupper($k)];

if(!is_array($thisk)){
settype($thisk,"array");
}

$count = count($thisk);
$thisk[$count] = $v;
$this->vars[strtoupper($k)] = $thisk;
}
}

}

function parse() {

foreach($this->vars as $k => $v){
if(!is_array($this->vars[strtoupper($k)])){
$this->content = str_replace("{" . strtoupper($k) . "}", $v, $this->content);
} else {
for($i=0; $i < count($this->vars[strtoupper($k)]); $i++){
$this->content = str_replace("{" . strtoupper($k) . "}", $this->vars[strtoupper($k)][$i], $this->content);
}
}
}

echo $this->content;
}
}
?>

Ich habe ein sehr sehr einfaches Template-System gefunden und hab mir gedacht, weißte wat, nimm es und mach was raus!

Alle Funktionne überarbeitet oder neu!

Und nu:
Was hab ich falsch gemacht!

BenniG.
31.12.2006, 15:19:36
Was hab ich falsch gemacht!
http://www.lugbz.org/documents/smart-questions_de.html nicht gelesen? ;)
Was ist Deine Frage, bzw. WAS geht nicht?

BruceCompanys
31.12.2006, 15:37:37
Das ist rein profilaktisch!

Ich meine natürlich damit (was ihr natürlich nicht wisst, ihr könnt ja schlecht meine Gedankengämge verstehen!), ist das ihr, wenn irh Lust, mir sagen könntet, was man besser schreiben könnte oder was im Code falsch, also wie man es NICHT schreiben sollte!

Lobe sind natürlich auch schön^^
der kleine Bruce ja auch mal sowas^^

mfg

BenniG.
31.12.2006, 15:51:08
Das z.B. versteh ich nicht

function set_var($title, $value) {
if(!isset($this->vars[$title])){
$this->vars[strtoupper($title)] = $value;
} else {

$thisk = $this->vars[strtoupper($title)];

if(!is_array($thisk)){
settype($thisk, "array");
}

$count = count($thisk);
$thisk[$count] = $value;
$this->vars[strtoupper($title)] = $thisk;
}

}

mach doch gleich

function set_var($title, $value) {
$title=strtoupper($title);
if(!isset($this->vars[$title])){
$this->vars[$title] = array($value);
} else {
$this->vars[$title][] = $value;
}

}

Dann brauchst du das ganze handling ob Array oder nicht garnicht.. Wozu willst du das eigentlich über Arrays machen? Kann ein und derselbe Template-Parameter denn mehrere Werte bekommen?? Was soll dann passieren? Momentan ersetzt du {Variable} durch den ersten Wert im Array..

BruceCompanys
31.12.2006, 16:37:18
Ja...
in einer Schleife^^

(verstehste später, wenn ich das fertig hab)

BenniG.
31.12.2006, 16:40:12
Naja, das hier

for($i=0; $i < count($this->vars[strtoupper($k)]); $i++){
$this->content = str_replace("{" . strtoupper($k) . "}", $this->vars[strtoupper($k)][$i], $this->content);
}

in deiner Parse-Methode kannst du dir halt sparen, weil das str_replace nur einmal was machen wird.. Danach ist es schon replaced..

BruceCompanys
31.12.2006, 17:01:19
Ja ist geändert^^

und ne frage:

<!-- BEGIN BLOCK -->
{irgendein Text}
<!-- END BLOCK -->

Das ist meine Schleife...

So wie schaffe ich es jetzt via Regex das aus dem Content zu filtern?
mfg

meikel
31.12.2006, 17:29:54
Einfach mal gucken, wie andere sowas schon gemacht haben:
http://www.kuerbis.org/asap/article/12/

BruceCompanys
31.12.2006, 17:35:05
Dat is es ja net...

Ich will nur den Regex wissen^^

ich würd es ja sonst net machen, wenn mir so ne Klasse reichen würde^^

meikel
31.12.2006, 17:46:22
Ich will nur den Regex wissen^^
Saugen und gucken, wies richtig gemacht wird. Oder wenigstens das angucken:

/**
* Parse the template.
* This function creates the template object tree and replaces contents
* of blocks with simple placeholders.
*
* @return void
*/
protected function _initTemplate()
{
preg_match_all("/<!--\s+BEGIN\s+(.*)?\s+-->(.*)<!--\s+END\s+(\\1)\s+-->/ms",$this->t,$ma);
for ($i = 0; $i < count($ma[0]); $i++)
{
$search = "/\s*\n*<!--\s+BEGIN\s+(" . $ma[1][$i] . ")?\s+-->(.*)<!--\s+END\s+(" . $ma[1][$i]. ")\s+-->\s*\n*/ms";
$replace = $this->delimiterStart . $ma[1][$i] . $this->delimiterEnd;

$this->bl[$ma[1][$i]] =& new $this->className('',$this->params);
$this->bl[$ma[1][$i]]->loadTemplateContent($ma[2][$i]);
$this->t = preg_replace($search,$replace,$this->t);
}
}

/**
* Fetch a block out of the template.
* If the block exists, this function returns a Template object,
* otherwise nothing (false).
* When parsing the template, the blocks will removed
* into Template objects and replaced with placeholders.
* The name of the placeholder is identical to the name
* of the removed block.
*
* @param string $blockName
* @access public
* @return object Template or boolean false
*/
public function fetchBlock($blockName)
{
if (isset($this->bl[$blockName]))
return $this->bl[$blockName];
else
return false;
}

/**
* Assign value to an existing placeholder.
* If this function is called multiple, the contents
* will be added.
*
* The parameter $varName can be a string, an associative
* array or a Template object.
*
* @param mixed $varName
* Allowed types: Requirements:
* string $varValue
* array Array format:
* array ("name_of_placeholder" => Value,
* ... )
* object Template object or any object which
* returns HTML code via get() method.
*
* @param string $varValue (optional)
* @access public
*/
public function assign($varName,$varValue=false)
{
if (is_array($varName))
{
foreach ($varName as $key => $value)
{
if (is_object($value))
$this->pl[$key][] = clone $value;
else
$this->pl[$key][] = $value;
}
}
else
{
if (is_object($varValue))
$this->pl[$varName][] = clone $varValue;
else
$this->pl[$varName][] = $varValue;
}
}


ich würd es ja sonst net machen, wenn mir so ne Klasse reichen würde^^
Also mir reicht KTemplate. Kann mit dynamischen Blöcken umgehen und Variablenarrays verwursten.

BruceCompanys
01.01.2007, 09:20:53
...^^

meikel du bist einfach genial^^

danke für den tipp...
und froh's neujahr...

mfg

meikel
01.01.2007, 09:41:46
Danke für das Mus, Bruce. <g>

Gesundes neues Jahr.

feuervogel
01.01.2007, 12:30:46
ich hab zwar ktemplate noch nie ausprobiert, aber ich halte auch http://smarty.php.net für empfehlenswert.

xabbuh
01.01.2007, 15:32:35
Ich würde übrigens an deiner Stelle die Möglichkeiten der objektorientierten Programmierung nutzen, die PHP 5 dir bietet (insbesondere Sichtbarkeitsklassifizierer), wenn du eh gerade die Templateklasse quasi von Grund auf entwickelst.

BruceCompanys
02.01.2007, 09:52:39
^^

Nein, jetzt noch nicht, mein Server läuft noch auf 4...
Das ändert sich im nächsten Monat^^
Dann leg ich alles auf 5 um

BruceCompanys
05.01.2007, 15:01:25
Ich hab nen bissl gestöbert und habe das jetzt mal fertig kreiert:

http://meine-matrix.freeprojekt.de/file/template/

ich hoffe es ist nen bissl besser...
steinigt mich bitte nicht wegen der schlechten Klassen+Methoden Führung^^
mfg

Madokan
09.01.2007, 11:06:51
@BruceCompanys: So richtig nachvollziehen kann es nicht, wieso noch eine weitere TemplateClass her muss, ich persönlich bevorzuge smarty vor allem wegen dem Caching. Aber abgesehen davon finde ich es immer wieder interessant neue Lösungsansätze zu bestaunen. Was den Aufbau betrifft schweige ich wie ein Grab, schließlich wolltest du ja nicht gesteinigt werden. ;)

Liebe Grüße,
Matze K.

BruceCompanys
10.01.2007, 16:27:27
Gegen Kreuzigungen hab ich nix^^


Als betoniert mich ans Kreuz, oder ins Kreuz^^

mfg