Add two functions to phplib template to solve the problem of image, CSS and JS including path in template file.

Source: Internet
Author: User
Tags array empty variables php file relative reset

Added two functions, using the same as the original, specific look at the code inside the description.
Just specify the path to the picture when you declare it, and default to the current directory, which is the directory where the php file resides.
Note that the picture directory is relative to the template file directory, template files and picture files can not be in the same level directory.
<?php

/*

* Session Management for PHP3

*

* (C) Copyright 1999-2000 netuse GmbH

* Kristian Koehntopp

*

* $Id: template.inc,v 1.5 2000/07/12 18:22:35 KK Exp $

*

*/

Class Template {

var $classname = "Template";

/* If set, echo assignments * *

var $debug = false;

/* $file [handle] = "filename"; */

var $file = array ();


/* Relative filenames are relative to this pathname * *

var $root = "";

/* $varkeys [key] = "key"; $varvals [Key] = "value"; */

var $varkeys = array ();

var $varvals = array ();

/* "Remove" => remove undefined variables

* "comment" => replace undefined variables with comments

* "Keep" => keep undefined variables

*/

var $unknowns = "Remove";

* * "yes" => Halt, "" "=> the" ", Continue," no "=> ignore error quietly * *

var $halt _on_error = "yes";

/* Last error message was retained here/*

var $last _error = "";

var $path = ".";

/*********************************/

/* Public:constructor.

* Root:template directory.

* Unknowns:how to handle unknown variables.

*/

function Template ($root = ".", $path = ".", $unknowns = "Remove") {

$this->set_root ($root);

$this->set_path ($path);

$this->set_unknowns ($unknowns);

}

/* Set Picture path

*/

function Set_path ($path) {

if ($path = = ".") return true;

if (!is_dir ($this->root. "/" . $path)) {

$this->halt ("Set_path: $path is not a directory.");

return false;

}

$this->path = $path;

return true;

}

/* Public:setroot (pathname $root)

* Root:new Template directory.

*/

function Set_root ($root) {

if (!is_dir ($root)) {

$this->halt ("Set_root: $root is not a directory.");

return false;

}

$this->root = $root;

return true;

}

/* Public:set_unknowns (enum $unknowns)

* Unknowns: "Remove", "comment", "keep"

*

*/

function set_unknowns ($unknowns = "Keep") {

$this->unknowns = $unknowns;

}

/* Public:set_file (array $filelist)

* Filelist:array of handle, filename pairs.

*

* Public:set_file (String $handle, String $filename)

* Handle:handle for a filename,

* Filename:name of template file

*/

function Set_file ($handle, $filename = "") {

if (!is_array ($handle)) {

if ($filename = = "") {

$this->halt ("set_file:for handle $handle filename is empty.");

return false;

}

$this->file[$handle] = $this->filename ($filename);

} else {

Reset ($handle);

while (the list ($h, $f) = each ($handle)) {

$this->file[$h] = $this->filename ($f);

}

}

}


/* Public:set_block (String $parent, String $handle, String $name = "")

* Extract the template $handle from $parent,

* Place variable {$name} instead.

*/

function Set_block ($parent, $handle, $name = "") {

if (! $this->loadfile ($parent)) {

$this->halt ("Subst:unable to load $parent.");

return false;

}

if ($name = = "")

$name = $handle;

$str = $this->get_var ($parent);

$reg = "/<!--s+begin $handles +--> (. *) \ns*<!--s+end $handles +-->/sm";

Preg_match_all ($reg, $str, $m);

$str = Preg_replace ($reg, "{".) $name} ", $STR);

$this->set_var ($handle, $m [1][0]);

$this->set_var ($parent, $STR);

$this->set_var ($name);

}

/* Public:set_var (array $values)

* Values:array of variable name, value pairs.

*

* Public:set_var (String $varname, String $value)

* Varname:name of a variable which is defined

* Value:value of that variable

*/

function Set_var ($varname, $value = "") {

if (!is_array ($varname)) {

if (!empty ($varname))

if ($this->debug) print "Scalar:set * $varname * to * $value *<br>\n";

$this->varkeys[$varname] = "/". $this->varname ($varname). " /";

$this->varvals[$varname] = $value;

} else {

Reset ($varname);

while (the 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). " /";

$this->varvals[$k] = $v;

}

}

}

/* PUBLIC:SUBST (String $handle)

* Handle:handle of template where variables are to be substituted.

*/

function subst ($handle) {

if (! $this->loadfile ($handle)) {

$this->halt ("Subst:unable to load $handle.");

return false;

}

$str = $this->get_var ($handle);

$str = @preg_replace ($this->varkeys, $this->varvals, $STR);

return $str;

}

/* PUBLIC:PSUBST (String $handle)

* Handle:handle of template where variables are to be substituted.

*/

function Psubst ($handle) {

Print $this->subst ($handle);

return false;

}

/* Public:parse (String $target, String $handle, Boolean append)

* Public:parse (string $target, Array $handle, Boolean append)

* Target:handle of variable to generate

* Handle:handle of template to substitute

* Append:append to target handle

*/

Function Parse ($target, $handle, $append = False) {

if (!is_array ($handle)) {

$str = $this->subst ($handle);

if ($append) {

$this->set_var ($target, $this->get_var ($target). $str);

} else {

$this->set_var ($target, $STR);

}

} else {

Reset ($handle);

while (the list ($i, $h) = each ($handle)) {

$str = $this->subst ($h);

$this->set_var ($target, $STR);

}

}

return $str;

}

function Pparse ($target, $handle, $append = False) {

Print $this->parse ($target, $handle, $append);

return false;

}

/* Public:get_vars ()

*/

function Get_vars () {

Reset ($this->varkeys);

while (the list ($k, $v) = each ($this->varkeys)) {

$result [$k] = $this->varvals[$k];

}

return $result;

}

/* Public:get_var (String varname)

* Varname:name of variable.

*

* Public:get_var (array varname)

* Varname:array of variable names

*/

function Get_var ($varname) {

if (!is_array ($varname)) {

return $this->varvals[$varname];

} else {

Reset ($varname);

while (the list ($k, $v) = each ($varname)) {

$result [$k] = $this->varvals[$k];

}

return $result;

}

}

/* public:get_undefined ($handle)

* Handle:handle of a template.

*/

function get_undefined ($handle) {

if (! $this->loadfile ($handle)) {

$this->halt ("Get_undefined:unable to load $handle.");

return false;

}

Preg_match_all ("/{([^}]+)}/", $this->get_var ($handle), $m);

$m = $m [1];

if (!is_array ($m))

return false;

Reset ($m);

while (the list ($k, $v) = each ($m)) {

if (!isset ($this->varkeys[$v]))

$result [$v] = $v;

}

if (count ($result))

return $result;

Else

return false;

}

/* Public:finish (String $str)

* Str:string to finish.

*/

function Finish ($STR) {

Switch ($this->unknowns) {

Case "Keep":

Break

Case "Remove":

$str = Preg_replace ('/{[^ \t\r\n}]+}/', "", $str);

Break

Case "comment":

$str = Preg_replace ('/{([^ \t\r\n}]+)}/', "<!--Template $handle: Variable \1 undefined-->", $str);

Break

}

return $str;

}

/* PUBLIC:P (String $varname)

* Varname:name of variable to print.

*/

function P ($varname) {

Print $this->finish ($this->get_var ($varname));

}

function Get ($varname) {

return $this->finish ($this->get_var ($varname));

}

/********************************/

/* Private:filename ($filename)

* Filename:name to be completed.

*/

function filename ($filename) {

if (substr ($filename, 0, 1)!= "/") {

$filename = $this->root. " /". $filename;

}


if (!file_exists ($filename))

$this->halt ("Filename:file $filename does not exist.");
return $filename;

}

/* Private:varname ($varname)

* Varname:name of a replacement variable to be protected.

*/

function VarName ($varname) {

Return Preg_quote ("{". $varname. "}");

}

/* Private:loadfile (String $handle)

* handle:load file defined by handle, if it isn't loaded yet.

*/

function LoadFile ($handle) {

if (Isset ($this->varkeys[$handle]) and!empty ($this->varvals[$handle]))

return true;

if (!isset ($this->file[$handle])) {

$this->halt ("LoadFile: $handle is not a valid handle.");

return false;

}

$filename = $this->file[$handle];

$str = Implode ("", @file ($filename));

if (empty ($STR)) {

$this->halt ("Loadfile:while loading $handle, $filename does not exist or are empty.");

return false;

}

$str = $this->parsepath ($STR);//Replace picture path

$this->set_var ($handle, $STR);

return true;

}

/* Re-parse the specified path under the replacement template directory (typically the picture file path)

*/

function Parsepath ($str = "") {

if ("/"!= substr ($this->path,-1)) {

$this->path. = "/";

}

if ("./" = = $this->path) return $str;

$str = preg_replace ("|"). $this->path. "|", $this->root. " /". $this->path, $STR);

return $str;

}

/**********************/

/* Public:halt (String $msg)

* Msg:error.

*/

function Halt ($msg) {

$this->last_error = $msg;

if ($this->halt_on_error!= "no")

$this->haltmsg ($msg);

if ($this->halt_on_error = = "Yes")

Die ("<b>Halted.</b>");

return false;

}


/* Public, override:haltmsg ($msg)

* Msg:error.

*/

function Haltmsg ($msg) {

printf ("<b>template error:</b>%s<br>\n", $msg);

}

}

?>

?

This statement indicates that the Moban directory and the IMG directory are in the same level directory

$t = new Template ("Moban", ". /img ");

This indicates that the IMG directory is in the Moban directory.

$t = new Template ("Moban", "img");

?>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.