#29 Strip comments from templates

open
nobody
None
5
2003-04-09
2003-04-09
Richard Archer
No

This idea from a post to the -users list by Sven:

Add a method to Template to strip comments from a
template file.

This would be useful to:
1. remove comments which are present only to make the
template easier to maintain. These are for the use of
the site builders and should never be sent to the user.
2. remove untouched BEGIN/END block comments. I often
have either/or blocks, one of which gets deleted and
the other one stays there, comments and all. The user
has no need to see those comments.

Default behaviour could be for finish to strip comments
from the template, just as it currently processes
remaining {} tags.

Original post from Sven:

Hello

Give it an easy way to delete comments in a template
file so that the output not have the comments?
I will not use the HTML-method (<!-- ... -->) for
commenting my template files.

My idea is a comment beginn with '{*' and end with '*}'
or so.
The first idea was to make an owe function in
template.inc to filter that
comments. But i hope it give an easy way as an own
function.

PS: I working with a cvs version of phplib.

Discussion

  • Sven Eichler
    Sven Eichler
    2004-05-02

    Logged In: YES
    user_id=253680

    --- /CVS/php-lib-stable/php/template.inc 2004-04-22
    23:17:01.000000000 +0200
    +++ /CVS/mylib/template.inc.php 2004-05-02 19:31:16.000000000
    +0200
    @@ -52,6 +52,13 @@
    *
    */

    +/*
    + * Change log by Koala 02.05.2004
    + *
    + * new function delete_comment($str)
    + *
    + */
    +
    /**
    * The template class allows you to keep your HTML code in some external
    files
    * which are completely free of PHP code, but contain replacement fields.
    @@ -170,6 +177,24 @@
    */
    var $last_error = "";

    + /**
    + * Comments in templates can begin with this.
    + *
    + * @var string
    + * @access public
    + */
    + var $left_delimiter = '{*';
    +
    + /**
    + * Comments in templates can end with this.
    + *
    + * @var string
    + * @access public
    + */
    + var $right_delimiter = '*}';
    +
    +
    +
    /
    ******************************************************************************
    * Class constructor. May be called with two optional parameters.
    * The first parameter sets the template directory the second parameter
    @@ -777,14 +802,17 @@
    function finish($str) {
    switch ($this->unknowns) {
    case "keep":
    + $str = $this->delete_comment($str);
    break;

    case "remove":
    $str = preg_replace('/{[^ \t\r\n}]+}/', "", $str);
    + $str = $this->delete_comment($str);
    break;

    case "comment":
    $str = preg_replace('/{([^ \t\r\n}]+)}/', "<!-- Template variable \\1
    undefined -->", $str);
    + $str = $this->delete_comment($str);
    break;
    }

    @@ -986,5 +1014,29 @@
    printf("<b>Template Error:</b> %s<br>\n", $msg);
    }

    +
    + /
    ******************************************************************************
    + * This function strip comments from templates.
    + *
    + * Returns:
    + *
    + * @param $str
    + * @access public
    + * @return string
    + */
    + function delete_comment($str) {
    + if ($this->debug & 1) {
    + print("<b>Varname:</b> $str<br>\n");
    + }
    +
    + // Quote regular expression characters
    + $ldq = preg_quote($this->left_delimiter, '!');
    + $rdq = preg_quote($this->right_delimiter, '!');
    +
    + $str = preg_replace("!$ldq(.*?)$rdq!se", "", $str);
    +
    + return $str;
    + }
    +
    }
    ?>