Date: 05/12/00
- Next message: Sascha Schumann: "Re: [phplib] Protected Files"
- Previous message: Aric Caley: "[phplib] Protected Files"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Assalamu'alaikum wr. wb.
Hi,
I just add some new feature to template, basically it let's you
add <!-- IF var -->...<!-- ELSE var -->...<!-- ENDIF var -->
and let the template do the job like cpp
#ifdef var
...
#else
...
#endif.
I also integrate some fix's that spread among mailing list archive.
Oh I almost forgot, now you could also set filter (another preg)
on template so that you could replace the final bits of your template.
Just take a look at set_filter and subst.
I hope it would be usefull.
The patch is in the attachment.
Here's my testbed code:
-----------------------------------------------------------
<?php
include("template.inc");
//header("Content-Type: text/plain");
$tpl = new Template("./", "keep");
$tpl->set_file("simple", "simple.ihtml");
$tpl->set_var("Title", "Testing New Template");
$tpl->set_var("self", $PHP_SELF);
$tpl->set_var("e-mail", "me <email protected>");
$tpl->set_var("ONE", $one);
$tpl->set_var("TWO", $two);
$tpl->set_var("THREE", $three);
$tpl->set_var("SHOW_FORM", !$hide);
$tpl->parse("out", "simple", FALSE, TRUE);
$tpl->p("out");
?>
-----------------------------------------------------------
Here's the simple.ihtml template:
-----------------------------------------------------------
<html><head><title>{Title}</title></head>
<body bgcolor="white">
<h1>{Title}</h1>
<hr>
<form action="{self}" method="get">
<!-- IF SHOW_FORM -->
<input type="checkbox" name="one"
<!-- IF ONE -->checked<!-- ENDIF ONE -->>One<br>
<input type="checkbox" name="two"
<!-- IF TWO -->checked<!-- ENDIF TWO -->>Two<br>
<input type="checkbox" name="three"
<!-- IF THREE -->checked<!-- ENDIF THREE -->>Three<br>
<input type="submit" name="action" value="Action">
<input type="submit" name="hide" value="Hide Form">
<input type="reset" value="Default">
<!-- ELSE SHOW_FORM -->
<input type="hidden" name="one" value="{ONE}">
<input type="hidden" name="two" value="{TWO}">
<input type="hidden" name="three" value="{THREE}">
<input type="submit" name="show" value="Show Form">
<!-- ENDIF SHOW_FORM -->
</form>
<!-- IF ONE -->
<ul>
<li>One: {ONE}
<li>Another One text
<!-- IF TWO -->
<ul>
<li>Two: {TWO}
<li>Another Two text
<!-- IF THREE -->
<ul>
<li>Three: {THREE}
<li>Another Three text
</ul>
<!-- ENDIF THREE -->
</ul>
<!-- ENDIF TWO -->
</ul>
<!-- ENDIF ONE -->
<hr>
<address><a href="mailto:{e-mail}">{e-mail}</a></address>
</body></html>
-----------------------------------------------------------
Wassallam,
-- Zakaria
PT. Asia Karsa Indah z4k4ri4 <email protected>
Advanced Technologies z4k4ri4 <email protected>
Jl. Raya Kalimalang 4B, Jakarta zakaria <email protected>
Telp : (62-21) 8649318 http://www.asia-karsa.com
Fax : (62-21) 8649316 http://linux.or.id/pemula
--- template.inc.orig Fri May 12 21:36:22 2000
+++ template.inc Fri May 12 21:43:47 2000
@@ -37,6 +37,12 @@
/* last error message is retained here */
var $last_error = "";
+ /* filter to apply to subst */
+ var $filter_pat;
+ var $filter_rep;
+
+ /* variable pattern */
+ var $var_pat = '[-_a-zA-Z0-9]+';
/***************************************************************************/
/* public: Constructor.
@@ -104,7 +110,7 @@
$name = $handle;
$str = $this->get_var($parent);
- $reg = "/<!--\s+BEGIN $handle\s+-->(.*)<!--\s+END $handle\s+-->/sm";
+ $reg = "/<!--\s*BEGIN\s+$handle\s*-->(.*)<!--\s*END\s+$handle\s*-->/sm";
preg_match_all($reg, $str, $m);
$str = preg_replace($reg, "{$name}", $str);
$this->set_var($handle, $m[1][0]);
@@ -120,25 +126,82 @@
*/
function set_var($varname, $value = "") {
if (!is_array($varname)) {
- if (!empty($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(list($k, $v) = each($varname)) {
- if (!empty($k))
+ 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)
+ /* public: set_filter(string $pattern, string $replacer)
+ * public: set_filter(array $pattern, array $replacer)
+ *
+ * pattern: pattern to replace
+ * replacer: the replacer
+ */
+ function set_filter($pattern, $replacer) {
+ $this->filter_pat = $patter;
+ $this->filter_rep = $replacer;
+ }
+
+ /* public: clear_filter()
+ * Clear the filter
+ */
+ function clear_filter() {
+ $this->filter_pat = "";
+ $this->filter_rep = "";
+ }
+
+ /* private: preproc(string &$str)
+ * str: string to preprocess
+ * handle <!-- IF var -->....<!-- ELSE var -->....<!-- ENDIF var -->
+ */
+ function preproc(&$str) {
+ $if_pat = "/^(.*)<!--\s*IF\s+($this->var_pat)\s*-->(.*?)"
+ . "<!--\s*ENDIF\s+\\2\s*-->(.*)$/s";
+
+ while ( preg_match($if_pat, $str, $part) ) {
+ $head = $part[1]; $varname = $part[2]; $ifcontent = $part[3];
+ $tail = $part[4];
+
+ // Handle else part
+ $else_pat = "/^(.*)<!--\s*ELSE\s+$varname\s*-->(.*)$/s";
+
+ if ( preg_match($else_pat, $ifcontent, $part) ) {
+ $ifcontent = $part[1]; $elsecontent = $part[2];
+ if ( $this->get_var($varname) ) {
+ $this->preproc($ifcontent);
+ $str = $head . $ifcontent . $tail;
+ } else {
+ $this->preproc($elsecontent);
+ $str = $head . $elsecontent . $tail;
+ }
+ } else {
+ if ( $this->get_var($varname) ) {
+ $this->preproc($ifcontent);
+ $str = $head . $ifcontent . $tail;
+ } else {
+ $str = $head . $tail;
+ }
+ }
+ }
+ }
+
+ /* public: subst(string $handle, boolean $preproc)
* handle: handle of template where variables are to be substituted.
+ * preproc: preprocess the string (handle <!-- IF ... -->)
*/
- function subst($handle) {
+ function subst($handle, $preproc = false) {
if (!$this->loadfile($handle)) {
$this->halt("subst: unable to load $handle.");
return false;
@@ -146,14 +209,22 @@
$str = $this->get_var($handle);
$str = <email protected>($this->varkeys, $this->varvals, $str);
+
+ if ($this->filter_pat) {
+ $str = <email protected>($this->filter_pat, $this->filter_rep, $str);
+ }
+ if ($preproc) {
+ $this->preproc($str);
+ }
+
return $str;
}
/* public: psubst(string $handle)
* handle: handle of template where variables are to be substituted.
*/
- function psubst($handle) {
- print $this->subst($handle);
+ function psubst($handle, $preproc = false) {
+ print $this->subst($handle, $preproc);
return false;
}
@@ -164,9 +235,9 @@
* handle: handle of template to substitute
* append: append to target handle
*/
- function parse($target, $handle, $append = false) {
+ function parse($target, $handle, $append = false, $preproc = false) {
if (!is_array($handle)) {
- $str = $this->subst($handle);
+ $str = $this->subst($handle, $preproc);
if ($append) {
$this->set_var($target, $this->get_var($target) . $str);
} else {
@@ -175,7 +246,7 @@
} else {
reset($handle);
while(list($i, $h) = each($handle)) {
- $str = $this->subst($h);
+ $str = $this->subst($h, $preproc);
$this->set_var($target, $str);
}
}
@@ -183,8 +254,8 @@
return $str;
}
- function pparse($target, $handle, $append = false) {
- print $this->parse($target, $handle, $append);
+ function pparse($target, $handle, $append = false, $preproc = false) {
+ print $this->parse($target, $handle, $append, $preproc);
return false;
}
@@ -227,7 +298,7 @@
return false;
}
- preg_match_all("/\{([^}]+)\}/", $this->get_var($handle), $m);
+ preg_match_all("/\{($this->var_pat)\}/", $this->get_var($handle), $m);
$m = $m[1];
if (!is_array($m))
return false;
@@ -253,11 +324,11 @@
break;
case "remove":
- $str = preg_replace("/\{[^}]+\}/", "", $str);
+ $str = preg_replace("/\{$this->var_pat\}/", "", $str);
break;
case "comment":
- $str = preg_replace("/\{([^}]+)\}/", "<!-- Template $handle: Variable \\1 undefined -->", $str);
+ $str = preg_replace("/\{($this->var_pat)\}/", "<!-- Template $handle: Variable \\1 undefined -->", $str);
break;
}
@@ -301,14 +372,14 @@
* handle: load file defined by handle, if it is not loaded yet.
*/
function loadfile($handle) {
- if ($this->varkeys[$handle] and !empty($this->varvals[$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->filename($this->file[$handle]);
+ $filename = $this->file[$handle];
$str = implode("", <email protected>($filename));
if (empty($str)) {
---------------------------------------------------------------------
To unsubscribe, e-mail: phplib-unsubscribe <email protected>
For additional commands, e-mail: phplib-help <email protected>
- Next message: Sascha Schumann: "Re: [phplib] Protected Files"
- Previous message: Aric Caley: "[phplib] Protected Files"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

