如何将翻译解析添加到rain.tpl?

时间:2013-12-19 00:19:49

标签: php regex templates smarty

我不熟悉正则表达式,我遇到了改变rain.tpl类的巨大问题。全班可用here(我不想在这里复制粘贴整个文件)。

问题是我想将{_(word)}解析为<?= __('word') ?>,因为它比{function="__('word')"}更容易使用。

我试图自己更改但我无法将正确的正则表达式放入方法中,输出会出现越来越多的错误。

修改
负责解析{function="__('word')"}等的解析函数部分如下所示:

//function
elseif( preg_match( '/\{function="(\w*)(.*?)"\}/', $html, $code ) ){

    //tag
    $tag = $code[ 0 ];

    //function
    $function = $code[ 1 ];

    // check if there's any function disabled by black_list
    $this->function_check( $tag );

    if( empty( $code[ 2 ] ) )
        $parsed_function = $function . "()";
    else
        // parse the function
        $parsed_function = $function . $this->var_replace( $code[ 2 ], $tag_left_delimiter = null, $tag_right_delimiter = null, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level );

    //if code
    $compiled_code .=   "<?php echo $parsed_function; ?>";
}

调用解析的函数(也有一些正则表达式)看起来像这样:

protected function compileTemplate( $template_code, $tpl_basedir ){

    //tag list
    $tag_regexp = array( 'loop'         => '(\{loop(?: name){0,1}="\${0,1}[^"]*"\})',
                         'loop_close'   => '(\{\/loop\})',
                         'if'           => '(\{if(?: condition){0,1}="[^"]*"\})',
                         'elseif'       => '(\{elseif(?: condition){0,1}="[^"]*"\})',
                         'else'         => '(\{else\})',
                         'if_close'     => '(\{\/if\})',
                         'function'     => '(\{function="[^"]*"\})',
                         'noparse'      => '(\{noparse\})',
                         'noparse_close'=> '(\{\/noparse\})',
                         'ignore'       => '(\{ignore\}|\{\*)',
                         'ignore_close' => '(\{\/ignore\}|\*\})',
                         'include'      => '(\{include="[^"]*"(?: cache="[^"]*")?\})',
                         'template_info'=> '(\{\$template_info\})',
                         'function'     => '(\{function="(\w*?)(?:.*?)"\})'
                        );

    $tag_regexp = "/" . join( "|", $tag_regexp ) . "/";

    //split the code with the tags regexp
    $template_code = preg_split ( $tag_regexp, $template_code, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );

    //path replace (src of img, background and href of link)
    $template_code = $this->path_replace( $template_code, $tpl_basedir );

    //compile the code
    $compiled_code = $this->compileCode( $template_code );

    //return the compiled code
    return $compiled_code;

}

希望您能够帮助我而无需下载和阅读整个班级文件。希望有人知道这个模板,可以帮助我:)。

1 个答案:

答案 0 :(得分:1)

到了等待的时候,我自己找到了解决办法。这是:

1.我将行'translation' => '(\{_\([^)]*(\w*)\)\})',添加到数组$tag_regexp中 2.我添加了新的 elseif

//translation
            elseif( preg_match( '/\{_\((\w*)\)\}/', $html, $code ) ){

                //function
                $word = $code[ 1 ];

                //if code
                $compiled_code .=   "<?= __('$word'); ?>";
            }

也许它会帮助某人:)