Forum for discussing general topics related to Couch.
4 posts Page 1 of 1
@KK, there are some tag calls around which do execute tags directly via $TAGS object - some in the core code, which is okay, some in custom code that raises up my concern. Events are not fired in such calls and therefore anything that is triggered by alter_tag_xxx_execute or tag_xxx_executed is skipped.

I want to write a wrapper for the tag <cms:addslashes> - <cms:add_slashes) (I spoke about it in the recent topic).
If I go like this I lose my mod - extended functionality of addslashes (particularly ability to escape a comma (\,))
Code: Select all
$FUNCS->register_tag( 'add_slashes', 'my_new_tag_add_slashes' );
function my_new_tag_add_slashes( $params, $node ){
   global $TAGS;

   return $TAGS->addslashes( $params, $node );
}


Here are my options:
#1 Use couch code, parse it with parser and embed as code.
#2 Dispatch events myself in the function my_new_tag_add_slashes().

What is the best way to address the problem? Are there any other better ways to run original tag?
Both the options are valid but, personally, I'd go with the first one (raise events myself) as it is more direct.

If you have several such overriding tags you could also create a separate generic function that, when invoked with the original tag-name and parameters, would call the original function sandwiched between the two events (basically mimicking what the code in parser.php does) -
Code: Select all
$FUNCS->register_tag( 'add_slashes', 'my_new_tag_add_slashes' );
function my_new_tag_add_slashes( $params, $node ){
    global $MY;
   
    // delegate to the original routine
    return $MY->execute_tag( 'addslashes', $params, $node );
}

Hope this helps.
I was resistant to the duplication of tag-running procedures potentially subject to change in native Couch code. So here is an example of the second approach which delegates to native tag -
Code: Select all
<?php

/**
*   <cms:add_slashes /> is a syn for <cms:addslashes>
*
*   @category New Tags
*   @example <cms:add_slashes quote='single' ></cms:add_slashes>
*   @link
*   @author Anton Smirnov aka Trendoman <tony.smirnov@gmail.com>
*   @date   04/02/2020
*/

$FUNCS->register_tag( 'add_slashes', 'my_new_tag_add_slashes' );
function my_new_tag_add_slashes( $params, $node ){

    // call the children
    foreach( $node->children as $child ){
        $html .= $child->get_HTML();
    }

    // set params
    foreach( $params as $p ){
        $attr .= $p['lhs'] . $p['op'] . "'{$p['rhs']}' ";
    }

    // run original tag
    $code = "<cms:addslashes {$attr}>{$html}</cms:addslashes>";
    $parser = new KParser( $code );
    $result = $parser->get_HTML();

    return $result;
}


I've literally copy-pasted this to use with json_escape/escape_json (this pair always gives me a headscratch due to native php function json_encode pops up in memory first).

Thank you very much, Kamran, for your suggestion.
As I said, this approach will work but would fast become tedious when applied to a number of tags.

Please see viewtopic.php?f=3&t=12536&p=35189#p35189
Perhaps that would be a more manageable solution.
4 posts Page 1 of 1