Hallo!
Ich habe ein Problem!
Ich benutze eine Template-Klasse.
Dort will ich in einem Template eine variable mit dem inhalt einer anderen Template setzen.
Mein Code:
Meine Template-Klasse:
Ich habe ein Problem!
Ich benutze eine Template-Klasse.
Dort will ich in einem Template eine variable mit dem inhalt einer anderen Template setzen.
Mein Code:
PHP-Code:
<?
$tpl = new Template('templates/standart/gcp.htm');
if($action == "anzeigen" || !$action){
}
if($action == "hochladen"){
$tmpl = new Template('templates/standart/gcp_upload.htm');
/* BILD-UPLOAD-FUNKTION */
if($_FILES["bild"]["name"] != ""){
$type = $_FILES['bild']['type']; $size = getimagesize($_FILES['bild']['tmp_name']);
if($type == "image/gif" or $type == "image/jpg" or $type == "image/jpeg"){
if($size[0] < 201 && $size[0] < 351){
if($type == "image/gif"){ $type = ".gif"; }
if($type == "image/jpg"){ $type = ".jpg"; }
if($type == "image/jpeg"){ $type = ".jpeg"; }
$bild = "./gallery/".details_user('Nickname')."/".MD5(time()).$type;
if(move_uploaded_file($_FILES['bild']['tmp_name'], $bild)){
$tmpl->assign("meldung","Erfolgreich geupped!");
} else { $tmpl->assign("meldung","Dein Bild konnte leider nicht hochgeladen werden!"); }
} else { $tmpl->assign("meldung","Sorry, aber dein Bild muss kleiner sein als 200x350 pixel sein!"); }
} else { $tmpl->assign("meldung","Sorry, aber dein Bild muss im Format .gif, .jpg oder .jpeg sein!"); }
}
}
$tpl->out();
?>
PHP-Code:
<?php
define("TEMPLATE_ERR_FILE","Could not load template file.");
/**
* Apolda Simple Template class.
* This file is part of Apolda Web Tool Suite.
*
* The complete apolda classes are distributed under the
* GNU Lesser General Public License.
* See the lesser.txt file for details.
*
* @author Ralf Geschke <ralf@kuerbis.org>
* @copyright 2002 by Ralf Geschke
* @version $Id: class_template.inc.php,v 1.6 2002/07/16 09:05:07 geschke Exp $
* @access public
*/
class Template
{
var $delimiterStart = "{";
var $delimiterEnd = "}";
var $t;
var $templatefile;
/**
* Constructor function.
* If a template filename is submitted, this function will
* initialize the template object tree.
*
* @param string $filename Name of template file.
* @access public
* @return void
*/
function Template($filename = "")
{
/* todo:
- remove setting error messages from constructor
( to a base class ? )
*/
$this->loadTemplateFile($filename);
}
/**
* Set start delimiter
* Call this function if you wish to change the default start
* delimiter '{' to another character.
*
* @param string $delim
* @return void
*/
function setStartDelim($delim="{")
{
$this->delimiterStart = $delim;
}
/**
* Set end delimiter
* Call this function if you wish to change the default end
* delimiter '}' to another character.
*
* @param string $delim
* @return void
*/
function setEndDelim($delim="}")
{
$this->delimiterEnd = $delim;
}
/**
* Load and initialize template file.
* This is only useful if it is not possible to
* set a template filename by creating an instance of
* the template class.
*
* @param string $filename Name of template file.
* @access public
* @return void
*/
function loadTemplateFile($filename = "")
{
if (!$filename)
return false;
if ($filename)
$this->templatefile = $filename;
if (!$fp = @fopen($this->templatefile,'r'))
{
die(TEMPLATE_ERR_FILE);
}
$this->t = fread($fp,filesize($this->templatefile));
fclose($fp);
$this->_initTemplate();
}
/**
* Submit a string variable as template content.
* This is useful if your template doesn't exist as file,
* e.g. if it is saved in a database.
*
* @param string $templatestring
* @access public
* @return void
*/
function loadTemplateContent($templatestring="")
{
$this->t = $templatestring;
$this->_initTemplate();
}
/**
* Parse the template.
* This function creates the template object tree and replaces contents
* of blocks with simple placeholders.
*
* @access private
* @return void
*/
function _initTemplate()
{
preg_match_all("/<!--\s+BEGIN\s+(.*)?\s+-->\s*\n*\s*(.*)\s*\n*\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 Template();
$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
*/
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
*/
function assign($varName,$varValue=false)
{
if (is_array($varName))
{
foreach ($varName as $key => $value)
{
$this->pl[$key][] = $value;
}
}
else
{
$this->pl[$varName][] = $varValue;
}
}
/**
* Delete the contents of submitted variables.
*
* @param none
* @access public
*/
function reset()
{
unset($this->pl);
}
/**
* Print a template with all replacements done.
*
* @param none
* @access public
*/
function out()
{
print $this->get();
}
/**
* Returns a template with all replacements done.
*
* @param none
* @access public
* @return string parsed template content
*/
function get()
{
if (is_array($this->pl))
{
foreach ($this->pl as $key => $value)
{
$search = $this->delimiterStart . $key . $this->delimiterEnd;
$replaceText = "";
for ($i = 0; $i < count($this->pl[$key]); $i++)
{
if (is_object($this->pl[$key][$i]))
$replaceText .= $this->pl[$key][$i]->get();
else
$replaceText .= $this->pl[$key][$i];
}
$this->t = str_replace($search,$replaceText,$this->t);
}
}
return $this->t;
}
}
?>
Kommentar