eZ Community » Forums » Developer » Template syntax checker
expandshrink

Template syntax checker

Template syntax checker

Wednesday 14 September 2011 8:35:15 am - 8 replies

Hi,

Does any body know how to check a template syntax ?

Before version 2011.8, there was a eztc.php script that allow to do that (as far as I remember) or maybe it was the pre compiler.

Thank you for answers.

 

Max

Wednesday 14 September 2011 3:54:27 pm

And it still is in bin/php/, as far as my zip of 2011.8 is concerned... :-P

Wednesday 14 September 2011 5:58:43 pm

Of course, eztc.php is the template compiler... the bin/php/eztemplatecheck.php looks like what you want... although I don't find that script to be all that useful.

This is what I use:

Edit: The syntax-higlighter adds |> to line 111 - I can't edit it here because it looks good in the edit form.  Either edit line 111 if you cut and paste here, or look at the comment below for the un-highlighted code.

 #!/bin/bash
tplfile=${@:$#} 
cmd=`basename $0`
#case {case {[/c][ca][as][se][e/]}
gettags ()
{
cat - <<EOF |grep -v "^#"
div <div </div>
span <span </span>
li <li[^n] </li>
ul <ul </ul>
ol <ol </ol>
tr <tr </tr>
td <td </td>
table <table </table>
if {if {/if} {else
case {case {/case}
switch {switch {/switch}
let {let {/let}
default {default {/default}
foreach {foreach {/foreach}
section {section[^-] {/section} {section-
append {append-block {/append-block}
cache {cache-block {/cache-block}
bracket-space {[[:space:]]  this_should_never_match_on_anything
EOF
}
matchtag ()
{
        matchtag=`echo $1|sed 's/\[.*\]$//'` #This is needed to stop the <li
        cat "$tplfile"|grep "$1"|                    #from matching on link with <li[^n]
        awk -v matchtag=$matchtag -v endtag=$endtag -v elsetag=${elsetag:-"this_is_a_long_dummy_string"} 'BEGIN{FS=endtag}{
                for(i=1;i<=NF;i++) {
                        if ( matchtag != elsetag && $i ~ elsetag ) {
                                continue
                        } else {
                                if ( $i ~ matchtag ) {total = total + 1}
                        }
                }
        } END {
        print total
        }'
}
checktags ()
{
else=0
dirtyflag=0
elsetag=""
gettags|while read label opentag closetag elsetag
do
        endtag=${closetag:$((${#closetag} - 1)):1}
        closetag=${closetag:0:$((${#closetag} - 1))}
        open=`matchtag $opentag|tail -1`
        close=`matchtag $closetag|tail -1`
        if [ -n "$elsetag" ]
        then
                else=`matchtag $elsetag|tail -1`
                if [ "${opt}" = "-v" ]
                then
                        echo $label $opentag ${open:-0} $closetag ${close:-0} ${else:+elsetag $else}
                fi
        else
                if [ "${opt}" = "-v" ]
                then
                        echo $label $opentag ${open:-0} $closetag ${close:-0}
                fi
        fi
        if [ "${open:-0}" -ne "${close:-0}" ]
        then 
                if [ "$1" = "verbose" ]
                then
                        echo -e "Uneven ${label} tags: ${label}-open: ${open:-0} ${label}-close: ${close:-0}\c"
                        if [ -n "$elsetag" -a ${else:-0} -ne 0 ]
                        then
                                echo -e " ${label}-else: ${else:-0}\c"
                        fi
                        echo
                else
                        dirtyflag=1
                        echo $dirtyflag
                fi
        fi
done
}
########
# MAIN #
########
if [ $# -lt 1 ]
then
        echo -e "Usage:\t $cmd <file|directory>"
        echo -e "\t\t<directory> is recursive"
        echo -e "\t\t-v (verbose) also displays clean files"
        exit
elif [ "$1" != "$tplfile" -a "$1" = "-v" ]
then
        opt="-v"
fi
if [ "$tplfile" = "." ]
then
        tplfile=$PWD
elif [ "$tplfile" = ".." ]
then
        tplfile=`dirname $PWD`
fi
if [ -d "$tplfile" ]
then
        echo "#### Starting from $tplfile ####"
        find $tplfile -name "*.tpl" -print|while read file
        do
                $0 ${opt}|> ${file}|>
        done
        exit
fi
 
dirtyflag=`checktags`
if [ -n "${dirtyflag}" ]
then
        echo "##### "$tplfile" #####"
        checktags verbose
elif [ "${opt}" = "-v" ]
then
        echo "##### "$tplfile" #####"
        echo "`basename $tplfile` appears clean"
fi

Modified on Sunday 18 September 2011 10:01:41 am by Steven E Bailey

Wednesday 14 September 2011 11:05:44 pm

Thanks !

@gaetano, damned, I'm a bit blind or what ? Of course it's still there.

@steven, what are the differences between your script and the eztemplatecheck (except for the language ) ?

Thursday 15 September 2011 8:26:36 am

Diffference?  Try it.  My shell script will actually tell you what it thinks is wrong with the template and it looks for more problems than templatecheck, apparently.  The down side  is It will throw false positives if you use self-closing case statements or if you close div tags in if/else statements or across different templates.

The truth is I've never much used templatecheck.  The first time I used it I was thinking that just knowing that something is invalid is nearly useless in any file that's over 50 lines.  And using -v -v -v  didn't change the output.  Just now I found out that if I use -v -v -v AND --logfile it'll dump the reason to the error.log but not to the screen.  Not sure if that's a bug, or what, but since templatecheck isn't actually documented anywhere I could find, I'm not sure what it is actually supposed to be doing.

Could be that I just missed the documentation too.  Dunno.

Sunday 18 September 2011 8:32:07 am

I also have been disappointed in the very limited bin/php/eztemplatecheck.php

@ Steven E. Bailey

I was interest in testing and using the bash script example you provided. When I did I encountered this error. Any suggestions on what it means?

 www-data@example:~/example.com/public$ ./tplck.sh -v extension/example/design/standard/templates
./tplck.sh: line 111: syntax error near unexpected token `newline'
./tplck.sh: line 111: `                $0 ${opt}|> ${file}|>'

Update: I'm using Ubuntu 10.04.3 LTS and Bash 4.1.5

Cheers,

Heath

Modified on Sunday 18 September 2011 8:41:46 am by // Heath

Sunday 18 September 2011 9:52:41 am

@ Heath

Looks like the syntax highlighter added some "extras"  Let me paste the code without the highlighting and edit the above post (specifically the |> stuff on line 111 shouldn't be there), actually, an earlier version is included with http://projects.ez.no/ez_shell_tools (maybe I should resurrect that, but with 9 downloads it wasn't very popular):

#!/bin/bash
tplfile=${@:$#}
cmd=`basename $0`
#case {case {[/c][ca][as][se][e/]}
gettags ()
{
cat - <<EOF |grep -v "^#"
div <div </div>
span <span </span>
li <li[^n] </li>
ul <ul </ul>
ol <ol </ol>
tr <tr </tr>
td <td </td>
table <table </table>
if {if {/if} {else
case {case {/case}
switch {switch {/switch}
let {let {/let}
default {default {/default}
foreach {foreach {/foreach}
section {section[^-] {/section} {section-
append {append-block {/append-block}
cache {cache-block {/cache-block}
bracket-space {[[:space:]]  this_should_never_match_on_anything
EOF
}
matchtag ()
{
        matchtag=`echo $1|sed 's/\[.*\]$//'` #This is needed to stop the <li
        cat "$tplfile"|grep "$1"|                    #from matching on link with <li[^n]
        awk -v matchtag=$matchtag -v endtag=$endtag -v elsetag=${elsetag:-"this_is_a_long_dummy_string"} 'BEGIN{FS=endtag}{
                for(i=1;i<=NF;i++) {
                        if ( matchtag != elsetag && $i ~ elsetag ) {
                                continue
                        } else {
                                if ( $i ~ matchtag ) {total = total + 1}
                        }
                }
        } END {
        print total
        }'
}
checktags ()
{
else=0
dirtyflag=0
elsetag=""
gettags|while read label opentag closetag elsetag
do
        endtag=${closetag:$((${#closetag} - 1)):1}
        closetag=${closetag:0:$((${#closetag} - 1))}
        open=`matchtag $opentag|tail -1`
        close=`matchtag $closetag|tail -1`
        if [ -n "$elsetag" ]
        then
                else=`matchtag $elsetag|tail -1`
                if [ "${opt}" = "-v" ]
                then
                        echo $label $opentag ${open:-0} $closetag ${close:-0} ${else:+elsetag $else}
                fi
        else
                if [ "${opt}" = "-v" ]
                then
                        echo $label $opentag ${open:-0} $closetag ${close:-0}
                fi
        fi
        if [ "${open:-0}" -ne "${close:-0}" ]
        then
                if [ "$1" = "verbose" ]
                then
                        echo -e "Uneven ${label} tags: ${label}-open: ${open:-0} ${label}-close: ${close:-0}\c"
                        if [ -n "$elsetag" -a ${else:-0} -ne 0 ]
                        then
                                echo -e " ${label}-else: ${else:-0}\c"
                        fi
                        echo
                else
                        dirtyflag=1
                        echo $dirtyflag
                fi
        fi
done
}
########
# MAIN #
########
if [ $# -lt 1 ]
then
        echo -e "Usage:\t $cmd <file|directory>"
        echo -e "\t\t<directory> is recursive"
        echo -e "\t\t-v (verbose) also displays clean files"
        exit
elif [ "$1" != "$tplfile" -a "$1" = "-v" ]
then
        opt="-v"
fi
if [ "$tplfile" = "." ]
then
        tplfile=$PWD
elif [ "$tplfile" = ".." ]
then
        tplfile=`dirname $PWD`
fi
if [ -d "$tplfile" ]
then
        echo "#### Starting from $tplfile ####"
        find $tplfile -name "*.tpl" -print|while read file
        do
                $0 ${opt} ${file}
        done
        exit
fi
dirtyflag=`checktags`
if [ -n "${dirtyflag}" ]
then
        echo "##### "$tplfile" #####"
        checktags verbose
elif [ "${opt}" = "-v" ]
then
        echo "##### "$tplfile" #####"
        echo "`basename $tplfile` appears clean"
fi

Monday 19 September 2011 1:29:27 am

@ Steven E. Bailey

Thank you for the excellent support. With your comments I was quickly able to run the script.

I agree forum posts are not the very best place to store useful source code. I would encourage you try continue distributing eZ knowledge in the very best forms available. Refreshing your your project might not be a bad idea. Linking to a pastebin or svn copy ensures forum related formatting errors don't occur.

 

Cheers,

Heath

Monday 19 September 2011 10:35:59 am

Hi Steven !

Thanks for this piece of valuable knowledge. I'd definitely recommend updating the project on projects.ez.no !

Cheers,

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from