使用Codeigniter Template Parser获取子数组信息

时间:2018-01-08 22:17:00

标签: php codeigniter parsing codeigniter-3

我使用CodeIgniter3模板解析器来避免在我的视图中包含太多PHP。

现在我遇到了麻烦,因为我通常会通过一个"大"使用一些subArrays(允许我对信息进行编目)的数组到Parser,如下所示:

$vars['userdata'] = $CI->session->all_userdata();
$vars['gameslist'] = $CI->Game_model->getGamesList();
$vars['baseurl'] = $baseurl;

$content  = $this->parse('header', $vars);
$content .= $this->parse($template_name, $vars);
$content .= $this->parse('footer', $vars);

现在,如果我尝试以这种方式访问​​标题中的userdata subArray:

{userdata}
   {username}
{/userdata}

我收到此错误:

Message: Invalid argument supplied for foreach()

实际上,具有此格式的Parser期望得到类似项的列表(并且用户数据不是,因为是包含登录用户数据的subArray)。

是否有任何方法可以访问subArray数据?像{userdata: username}这样的东西真的很有用。

编辑:

这是Parse功能

protected function _parse($template, $data, $return = FALSE)
    {
        // First check if we've got something to parse
        if ($template === '') {
            return FALSE;
        }

        // Retrieve and load all CI vars
        $data = array_merge($data, $this->CI->load->get_vars());

        // Check for loop statements
        $template = $this->_parse_loops($template, TRUE);

        // Variable replacement pre-processing
        $replace = array();
        foreach ($data as $key => $val) {
            $replace = array_merge(
                $replace,
                is_object($val) ? $this->_parse_object($key, $val, $template) :
                (is_array($val) ? $this->_parse_pair($key, $val, $template) :
                $this->_parse_single($key, (string) $val, $template))
            );
        }
        // Variable replacement processing (actually viable only for every variable defined in $data, not tags defined in the parsed template)
        foreach ($replace as $from => $to) {
            $template = str_ireplace($from, ( ! is_null($to) ? $to : '%EMPTY_VAR%'), $template);
        }

        // Datas fully replaced, we don't need it anymore
        unset($data);

        // Unparsed tags replacement (each one will be replaced by a Parser special tag - {EMPTY_VAR})
        $template = $this->_replace_unparsed($template);

        // Check for helpers calls
        $template = $this->_parse_helpers($template);

        // Check for conditional statements
        $template = $this->_parse_switch($template, TRUE);
        $template = $this->_parse_conditionals($template, TRUE);

        if ($return === FALSE) {
            $this->CI->output->append_output($template);
        }

        // And last, unparsed tags removal
        $template = $this->_remove_unparsed($template);

        return $template;
    }

这就是$ vars [' userdata']给我的:

Array ( [__ci_last_regenerate] => 1515464554 [id] => 2 [username] => Scimiazzurro [email] => test@test.it [rank] => 1 )

0 个答案:

没有答案