Do you have some feature in mind that you'd love to see in Couch? Let us know.
3 posts Page 1 of 1
@KK, please consider a new event for an unknown tag possibly located at couch/parser/parser.php::490.

I would like to have an option to run some code and customize the error message before the page dies with a warning. I am considering a kind of suggestion list there or correct spelling hint. I am often confused whether it is a <cms:addslashes> or <cms:add_slashes> (as in <cms:add_querystring), <cms:json_escape> or <cms:escape_json> ;)

I have already written a wrapper for some ambiguously named custom tag pairs like <cms:json_print> & <cms:print_json> or <cms:base64encode> or <cms:base64_encode>. So, I am going to do the same for the <cms:addslashes> but I might have a shot at suggestion algorithms and a described event will probably help there.

Some tags belong to addons and it will probably be helpful to display a note for tags like <cms:pp_..>.
Done.

Please use the latest commit from GitHub and you'll have an event that should allow you to customize the error message when an unknown tag is encountered.

Actually, going beyond the brief, it would also allow you to do much more e.g. executing the correct tag if it was misspelled (which is what you have been trying to do).

Some example code (to be pasted in kfunctions.php) -
Code: Select all
class MyAltTags{
    var $aliases = array( /* 'alias' => 'real_tag' */
        'add_slashes' => 'addslashes',
        'xyz' => 'show',
    );

    function __construct(){
        global $FUNCS;
        $FUNCS->add_event_listener( 'tag_unknown', array($this, 'alt_tag') );
    }

    function alt_tag( $tagname, &$node, &$html ){
        global $FUNCS;

        if( array_key_exists($tagname, $this->aliases) ){
            $node->name = $this->aliases[$tagname]; // correct the tag name ..

            $ok = self::execute_tag( $node, $html ); // and execute
            if( $ok ){
                return 1; // skip further processing this tag
            }
            else{
                $html = 'ERROR! Unknown tag: "'. $tagname . '"';
            }
        }
    }

    static function execute_tag( &$node, &$html ){
        global $TAGS, $FUNCS;

        $tagname = $node->name;

        if( !in_array($tagname, array('if', 'else', 'else_if', 'while', 'break', 'continue', 'not', 'extends')) ){
            if( method_exists($TAGS, $tagname) || array_key_exists( $tagname, $FUNCS->tags) ){
                $params = $FUNCS->resolve_parameters( $node->attributes );

                // HOOK: alter_tag_<tag_name>_execute
                $skip = $FUNCS->dispatch_event( 'alter_tag_'.$tagname.'_execute', array($tagname, &$params, &$node, &$html) );

                if( !$skip ){
                    if( method_exists($TAGS, $tagname) ){
                        $html = call_user_func( array($TAGS, $tagname), $params, $node );
                    }
                    else{
                        $html = call_user_func( $FUNCS->tags[$tagname]['handler'], $params, $node );
                    }
                }

                // HOOK: tag_<tag_name>_executed
                $FUNCS->dispatch_event( 'tag_'.$tagname.'_executed', array($tagname, &$params, &$node, &$html) );

                return 1;
            }
        }
        return 0;
    }
}// end class MyAltTags
new MyAltTags();

With the above code, you only need add all the aliases for the tags troubling you to a single array and they'll automatically execute the correct tag.

Hope this helps. Please test and let me know.
Awesome, Kamran. Now we can safely intercept a running tag and avoid redundant bloat in the list of available tags.

What will you now do with the <cms:is> which is a shorter alias for <cms:arr_val_exists>? Event placed on the latter will not fire if the former is used instead, which kinda requires (for now) having 2 event listeners - each for every tag's name.
3 posts Page 1 of 1
cron