Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 包括语言解析器的codeigniter模板引擎_Php_Codeigniter_Template Engine - Fatal编程技术网

Php 包括语言解析器的codeigniter模板引擎

Php 包括语言解析器的codeigniter模板引擎,php,codeigniter,template-engine,Php,Codeigniter,Template Engine,我没有看到在模板解析器重写的视图中引用语言密钥的任何机制。现在,我的视图包含如下字符串: {register} {userid} {password} 我希望用语言定义中的匹配字符串替换这些字符串,就像我在视图中这样做一样: <?=$this->lang->line('register')?> <?=$this->lang->line('userid')?> <?=$this->lang->line('pas

我没有看到在模板解析器重写的视图中引用语言密钥的任何机制。现在,我的视图包含如下字符串:

{register}
{userid}
{password} 
我希望用语言定义中的匹配字符串替换这些字符串,就像我在视图中这样做一样:

   <?=$this->lang->line('register')?>
   <?=$this->lang->line('userid')?>
   <?=$this->lang->line('password')?> 
这在我看来相当愚蠢。我认为模板解析器类应该自动支持任何已定义语言密钥的扩展。可能通过使用第二组分隔符:

[register]
[userid]
[password] 
或者可以使用特定的指示符,如下划线:

{_register}
{_userid}
{_password} 
或者可能是GetText风格:

_(register)
_(userid)
_(password) 

有人能告诉我我正在寻找的功能是否真的可用,而我只是错过了它吗?否则,我的建议是否合理,或者是否有更好的替代方案?

不幸的是,CI的内置模板解析器类没有此功能。您可以在中查看,有多个sparks集成了许多模板引擎,如smarty或twig,可以对其进行调整以创建类似的内容

此外,您还可以尝试扩展
CI_解析器
类,这样做:

<?php

class MY_Parser extends CI_Parser {

    const LANG_REPLACE_REGEXP = '!\{_\s*(?<key>[^\}]+)\}!';
    public $CI = null;

    public function parse($template, $data, $return = FALSE) {
        $this->CI = get_instance();
        $template = $this->CI->load->view($template, $data, TRUE);
        $template = $this->replace_lang_keys($template);

        return $this->_parse($template, $data, $return);
    }

    protected function replace_lang_keys($template) {
        return preg_replace_callback(self::LANG_REPLACE_REGEXP, array($this, 'replace_lang_key'), $template);
    }

    protected function replace_lang_key($key) {
        return $this->CI->lang->line($key[1]);
    }
}

完美答案!现在在我的所有项目中都使用它,我如何修改正则表达式以忽略把手模板<代码>{{each element}}{{name}

{{/each}
@thiagobraga,只要你没有
{{
在你的把手块中,你应该可以按原样使用模式。@complex857抱歉,忘了说,我已经完美地使用了你的类一段时间,语言翻译来自数据库。但是我将PHP文件中的语言键修改为
{i18n_text}
。现在我在视图文件的
标记中使用了Handlebar模板,最近修改了
{i18n_text}
的语言键,使其与Handlebar不冲突,但正则表达式在这两种情况下不能正常工作。我认为你的想法很好,也许我将键改为
{i18n_text}
以避免此错误。谢谢!我认为如果您在混合中添加一些消极的先行/后向,您可以使此工作正常,因此类似于:
{35;(?!\}){i18n{u(?[^\}]+)\}{i18n}{u35;
应该只匹配
{i18n}文本}
,如果周围没有额外的
{code>{code>
}
<?php

class MY_Parser extends CI_Parser {

    const LANG_REPLACE_REGEXP = '!\{_\s*(?<key>[^\}]+)\}!';
    public $CI = null;

    public function parse($template, $data, $return = FALSE) {
        $this->CI = get_instance();
        $template = $this->CI->load->view($template, $data, TRUE);
        $template = $this->replace_lang_keys($template);

        return $this->_parse($template, $data, $return);
    }

    protected function replace_lang_keys($template) {
        return preg_replace_callback(self::LANG_REPLACE_REGEXP, array($this, 'replace_lang_key'), $template);
    }

    protected function replace_lang_key($key) {
        return $this->CI->lang->line($key[1]);
    }
}