[PHPLIB-DEV] cvs commit From: kir (phplib-dev <email protected>)
Date: 03/30/00

From: kir
Date: Thu Mar 30 15:43:06 2000
Modified files:
      php-lib/CHANGES
      php-lib/php/template.inc

Log message:

  - More story about templates. Now Template class from template.inc
    supports conditional blocks:
    <!-- BEGIN var IF {expression} --> {another_var} html code
    <!-- END var -->
    So you can define IF-ed blocks. This feature works in loops too.
    Sorry, I know nothing about sgml and my english too poor to update
    documentation :(.
    
    An Example:
    -------------- template.ihtml -----------------
    <TABLE>
    <!-- BEGIN loop -->
    
    <TR><TD>Name: {name}<BR>
            <!-- BEGIN email_if IF {email_test} -->
            Email: {email}
            <!-- END email_if-->
    </TD>
    <!-- END loop -->
    </TABLE>
    -----------------------------------------------
    // Appropriate code:
    $t = new Template(...);
    $t->set_file("fl","template.ihtml");
    $t->set_block("fl", "loop", "loop_handle");
    $t->set_block("loop", "email_if", "email_handle");

    while ( has_more_data )
    {
        // here we get $name, $email
        // $email may be empty
        $t->set_var("name", $name);
        $t->set_var("email", $email);
        $t->set_var("email_test", !empty($email));

        $t->parse("email_handle", "email_if", false);
        $t->parse("loop_handle", "loop", true);
    }
    

Index: php-lib/CHANGES
diff -u php-lib/CHANGES:1.165 php-lib/CHANGES:1.166
--- php-lib/CHANGES:1.165 Wed Mar 29 15:53:28 2000
+++ php-lib/CHANGES Thu Mar 30 15:42:35 2000
@@ -1,4 +1,45 @@
-$Id: CHANGES,v 1.165 2000/03/29 13:53:28 kir Exp $
+$Id: CHANGES,v 1.166 2000/03/30 13:42:35 kir Exp $
+
+30-Mar-2000 kir
+ - More story about templates. Now Template class from template.inc
+ supports conditional blocks:
+ <!-- BEGIN var IF {expression} --> {another_var} html code
+ <!-- END var -->
+ So you can define IF-ed blocks. This feature works in loops too.
+ Sorry, I know nothing about sgml and my english too poor to update
+ documentation :(.
+
+ An Example:
+ -------------- template.ihtml -----------------
+ <TABLE>
+ <!-- BEGIN loop -->
+
+ <TR><TD>Name: {name}<BR>
+ <!-- BEGIN email_if IF {email_test} -->
+ Email: {email}
+ <!-- END email_if-->
+ </TD>
+ <!-- END loop -->
+ </TABLE>
+ -----------------------------------------------
+ // Appropriate code:
+ $t = new Template(...);
+ $t->set_file("fl","template.ihtml");
+ $t->set_block("fl", "loop", "loop_handle");
+ $t->set_block("loop", "email_if", "email_handle");
+
+ while ( has_more_data )
+ {
+ // here we get $name, $email
+ // $email may be empty
+ $t->set_var("name", $name);
+ $t->set_var("email", $email);
+ $t->set_var("email_test", !empty($email));
+
+ $t->parse("email_handle", "email_if", false);
+ $t->parse("loop_handle", "loop", true);
+ }
+
 
 29-Mar-2000 kir
   - Make Tmpl_Control::set_file() compatible with Template::set_file()
Index: php-lib/php/template.inc
diff -u php-lib/php/template.inc:1.13 php-lib/php/template.inc:1.14
--- php-lib/php/template.inc:1.13 Sat Jan 15 15:11:24 2000
+++ php-lib/php/template.inc Thu Mar 30 15:42:35 2000
@@ -5,7 +5,7 @@
  * (C) Copyright 1999 NetUSE GmbH
  * Kristian Koehntopp
  *
- * $Id: template.inc,v 1.13 2000/01/15 14:11:24 carmelo Exp $
+ * $Id: template.inc,v 1.14 2000/03/30 13:42:35 kir Exp $
  *
  */
 
@@ -168,6 +168,20 @@
   function subst($varname) {
     $str = $this->get_var($varname);
     $str =  <email protected>($this->varkeys, $this->varvals, $str);
+
+ if (!empty($this->block[$varname]["extra"]))
+ {
+ $evalstr =  <email protected>($this->varkeys, $this->varvals,
+ $this->block[$varname]["extra"]);
+ // echo "IF $varname, $beforeeval, $evalstr";
+ $flag=0;
+ eval("\$flag=$evalstr;");
+ if (!$flag)
+ {
+ $str="";
+ }
+ }
+
     return $str;
   }
   
@@ -385,25 +399,28 @@
     
     /* get parent variable */
     $str = $this->get_var($parent);
-
+
+ //print "\n\n$varname $parent $alias ".$str;
+
     /* find the subblock we are looking for and extract it */
     $reg = "/<!--\\s+BEGIN $varname\\s+-->(.*)<!--\\s+END $varname\\s+-->/sm";
- preg_match_all($reg, $str, $m);
+ if (!preg_match_all($reg, $str, $m))
+ {
+ $reg = "/<!--\\s+BEGIN $varname\\s+IF\\s+(.*?)\\s*-->(.*)<!--\\s+END $varname\\s+-->/sm";
+ if (!preg_match_all($reg, $str, $m))
+ {
+ halt("implode_block - no match for $varname variable");
+ }
+ $this->block[$varname]["extra"] = $m[1][0];
+ $m[1][0] = $m[2][0];
+ }
     
     /* implode the subblock to the requested alias */
- //check the version of php
- //carmelo
- $checkversion=1;
- if ("{$checkversion}"==1) $str = preg_replace($reg, "\{$alias}", $str);
- else $str = preg_replace($reg, "{$alias}", $str);
-
- if ($this->debug & 4) {
- $checkversion=1;
- if ("{$checkversion}"==1) {
- printf("<b>implode_block:</b> extract <b>$varname</b> from <b>$parent</b>, leaving \{$alias}<br>\n");
- } else {
- printf("<b>implode_block:</b> extract <b>$varname</b> from <b>$parent</b>, leaving {$alias}<br>\n");
- }
+ $str = preg_replace($reg, "{"."$alias}", $str);
+
+ if ($this->debug & 4)
+ {
+ printf("<b>implode_block:</b> extract <b>$varname</b> from <b>$parent</b>, leaving {"."$alias}<br>\n");
         }
     //end change
         

-
PHPLIB Developers Mailing List. Send messages to <phplib-dev <email protected>>.
To unsubscribe, send "unsubscribe" to <phplib-dev-request <email protected>> in
the body, not the subject, of your message.