Documentación GVHIDRA 3.1.5
Referencia de la Clase Template_PHPLIB

Métodos públicos

 Template_PHPLIB ($root=".", $unknowns="remove", $fallback="")
 setRoot ($root)
 setUnknowns ($unknowns="keep")
 setFile ($handle, $filename="")
 setBlock ($parent, $handle, $name="")
 setVar ($varname, $value="", $append=false)
 subst ($handle)
 pSubst ($handle)
 parse ($target, $handle, $append=false)
 pParse ($target, $handle, $append=false)
 getVars ()
 getVar ($varname)
 getUndefined ($handle)
 finish ($str)
 p ($varname)
 get ($varname)
 _filename ($filename)
 _varname ($varname)
 _loadFile ($handle)
 halt ($msg)
 haltMsg ($msg)

Campos de datos

 $debug = false
 $file = array()
 $file_fallbacks = array()
 $root = ""
 $_varKeys = array()
 $_varVals = array()
 $unknowns = "remove"
 $haltOnError = "report"
 $_lastError = ""

Descripción detallada

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>

Autor:
Bjoern Schotte <bjoern@rent-a-phpwizard.de>
Martin Jansen <mj@php.net> (PEAR conformance)
Versión:
1.0

Definición en la línea 28 del archivo PHPLIB.php.


Documentación de las funciones miembro

_filename ( filename)

Complete filename

Complete filename, i.e. testing it for slashes

private

Parámetros:
stringfilename to be completed
Devuelve:
string completed filename

Definición en la línea 456 del archivo PHPLIB.php.

    {
//        if (substr($filename, 0, 1) != "/") {
//            $filename = $this->root."/".$filename;
//        }

        if (file_exists($filename)) return $filename;
        if (is_array($this->file_fallbacks) && count($this->file_fallbacks) > 0) {
            reset($this->file_fallbacks);
            while (list(,$v) = each($this->file_fallbacks)) {
                if (file_exists($v.basename($filename))) return $v.basename($filename);
            }
            $this->halt(sprintf("filename: file %s does not exist in the fallback paths %s.",$filename,implode(",",$this->file_fallbacks)));
            return false;
        } else {
            $this->halt(sprintf("filename: file %s does not exist.",$filename));
            return false;
        }

        return $filename;
    }
_loadFile ( handle)

load file defined by handle if it is not loaded yet

private

Parámetros:
stringhandle
Devuelve:
bool FALSE if error, true if all is ok

Definición en la línea 497 del archivo PHPLIB.php.

    {
        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];
        if (function_exists("file_get_contents")) {
            $str = file_get_contents($filename);
        } else {
            if (!$fp = @fopen($filename,"r")) {
                $this->halt("loadfile: couldn't open $filename");
                return false;
            }

            $str = fread($fp,filesize($filename));
            fclose($fp);
        }

        if ($str=='') {
            $this->halt("loadfile: While loading $handle, $filename does not exist or is empty.");
            return false;
        }

        $this->setVar($handle, $str);

        return true;
    }
_varname ( varname)

Protect a replacement variable

private

Parámetros:
stringname of replacement variable
Devuelve:
string replaced variable

Definición en la línea 485 del archivo PHPLIB.php.

    {
        return "{".$varname."}";
    }
finish ( str)

Finish string

public

Parámetros:
stringstring to finish
Devuelve:
finished, i.e. substituted string

Definición en la línea 409 del archivo PHPLIB.php.

    {
        switch ($this->unknowns) {
            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;
    }
get ( varname)

Get finished variable

public public

Parámetros:
stringvariable to get
Devuelve:
string string with finished variable

Definición en la línea 442 del archivo PHPLIB.php.

    {
        return $this->finish($this->getVar($varname));
    }
getUndefined ( handle)

Get undefined values of a handle

public

Parámetros:
stringhandle name
Devuelve:
mixed false if an error occured or the undefined values

Definición en la línea 375 del archivo PHPLIB.php.

    {
        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;
        }
    }
getVar ( varname)

Return one or more specific variable(s) with their values.

public

Parámetros:
mixedarray with variable names or one variable name as a string
Devuelve:
mixed array of variable names with their values or value of one specific variable

Definición en la línea 349 del archivo PHPLIB.php.

    {
        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;
        }
    }
getVars ( )

Return all defined variables and their values

public

Devuelve:
array with all defined variables and their values

Definición en la línea 331 del archivo PHPLIB.php.

    {
        reset($this->_varKeys);

        while (list($k, ) = each($this->_varKeys)) {
            $result[$k] = $this->getVar($k);
        }

        return $result;
    }
halt ( msg)

Error function. Halt template system with message to show

public

Parámetros:
stringmessage to show
Devuelve:
bool

Definición en la línea 538 del archivo PHPLIB.php.

    {
        $this->_lastError = $msg;

        if ($this->haltOnError != "no") {
//            return $this->haltMsg($msg);
            $this->haltMsg($msg);
        }

        if ($this->haltOnError == "yes") {
            die("<b>Halted.</b>");
        }

        return false;
    }
haltMsg ( msg)

printf error message to show

public

Parámetros:
stringmessage to show
Devuelve:
object PEAR error object

Definición en la línea 561 del archivo PHPLIB.php.

    {
//        PEAR::raiseError(sprintf("<b>Template Error:</b> %s<br>\n", $msg));
        printf("<b>Template Error:</b> %s<br>\n", $msg);
    }
p ( varname)

Print variable to the browser

public

Parámetros:
stringname of variable to print

Definición en la línea 430 del archivo PHPLIB.php.

    {
        print $this->finish($this->getVar($varname));
    }
parse ( target,
handle,
append = false 
)

Parse handle into target

Parses handle $handle into $target, eventually appending handle at $target if $append is defined as TRUE.

public

Parámetros:
stringtarget handle to parse into
stringwhich handle should be parsed
booleanappend it to $target or not?
Devuelve:
string parsed handle

Definición en la línea 291 del archivo PHPLIB.php.

    {
        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;
    }
pParse ( target,
handle,
append = false 
)

Same as parse, but printing it.

public parse

Parámetros:
stringtarget to parse into
stringhandle which should be parsed
should$handlebe appended to $target?
Devuelve:
bool

Definición en la línea 319 del archivo PHPLIB.php.

    {
        print $this->finish($this->parse($target, $handle, $append));
        return false;
    }
pSubst ( handle)

Same as subst but printing the result

public subst

Parámetros:
stringhandle of template
Devuelve:
bool always false

Definición en la línea 272 del archivo PHPLIB.php.

    {
        print $this->subst($handle);
        return false;
    }
setBlock ( parent,
handle,
name = "" 
)

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.

public

Parámetros:
stringparent handle
stringblock name handle
stringvariable substitution name

Definición en la línea 196 del archivo PHPLIB.php.

    {
        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);
    }
setFile ( handle,
filename = "" 
)

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.

public

Parámetros:
mixedhandle for a filename or array with handle/name value pairs
stringname of template file
Devuelve:
bool

Definición en la línea 155 del archivo PHPLIB.php.

    {
        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);
            }
        }
    }
setRoot ( root)

Sets the template directory

public

Parámetros:
stringnew template directory
Devuelve:
bool

Definición en la línea 110 del archivo PHPLIB.php.

    {
        if (!is_dir($root)) {
            $this->halt("setRoot: $root is not a directory.");
            return false;
        }
    
        $this->root = $root;
    
        return true;
    }
setUnknowns ( unknowns = "keep")

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

public

Parámetros:
stringunknowns

Definición en la línea 135 del archivo PHPLIB.php.

    {
        $this->unknowns = $unknowns;
    }
setVar ( varname,
value = "",
append = false 
)

Set corresponding substitutions for placeholders

public

Parámetros:
stringname of a variable that is to be defined or an array of variables with value substitution as key/value pairs
stringvalue of that variable
booleanif true, the value is appended to the variable's existing value

Definición en la línea 224 del archivo PHPLIB.php.

    {
        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;
            }
        }
    }
subst ( handle)

Substitute variables in handle $handle

public

Parámetros:
stringname of handle
Devuelve:
mixed string substituted content of handle

Definición en la línea 254 del archivo PHPLIB.php.

    {
        if (!$this->_loadFile($handle)) {
            $this->halt("subst: unable to load $handle.");
            return false;
        }

        return @str_replace($this->_varKeys, $this->_varVals, $this->getVar($handle));
    }
Template_PHPLIB ( root = ".",
unknowns = "remove",
fallback = "" 
)

Constructor

public

Parámetros:
stringtemplate root directory
stringhow to handle unknown variables
arrayfallback paths

Definición en la línea 96 del archivo PHPLIB.php.

    {
        $this->setRoot($root);
        $this->setUnknowns($unknowns);
        if (is_array($fallback)) $this->file_fallbacks = $fallback;
    }

Documentación de los campos

$_lastError = ""

Definición en la línea 85 del archivo PHPLIB.php.

$_varKeys = array()

Definición en la línea 58 del archivo PHPLIB.php.

$_varVals = array()

Definición en la línea 64 del archivo PHPLIB.php.

$debug = false

Definición en la línea 34 del archivo PHPLIB.php.

$file = array()

Definición en la línea 40 del archivo PHPLIB.php.

$file_fallbacks = array()

Definición en la línea 46 del archivo PHPLIB.php.

$haltOnError = "report"

Definición en la línea 78 del archivo PHPLIB.php.

$root = ""

Definición en la línea 52 del archivo PHPLIB.php.

$unknowns = "remove"

Definición en la línea 72 del archivo PHPLIB.php.


La documentación para esta clase fue generada a partir del siguiente fichero: