dongyue7796 2013-02-20 21:41
浏览 7
已采纳

它为什么吹过第一个?

I build a function that if you give it an array of paths it will look for a file in each path and if you give it an array of one path it will look in that path only for the file.

My questions, is when you give it an array of paths such as:

$array = array(
    'template_view_paths' => array(
        'path_name' => some/path/
        'path_name_two' => some/path/two/
    )
)

It seems to find the file in some/path/ but keeps going on to some/path/two/ and freaks out because the file doesn't exist. What would I have to change so that if it find the file in any path that it stops looking in other paths?

The code - NOT re-factored

public function render_view($template_name){
    if(!isset($this->_options['template_view_path'])){
        throw new AisisCore_Template_TemplateException('Not view path was set.');
    }

    if(is_array($this->_options['template_view_path'])){
        foreach($this->_options['template_view_path'] as $template=>$path){
            require_once($path . $template_name . '.phtml');
        }
    }else{
        if(!file_exists($this->_options['template_view_path'] . $template_name . '.phtml')){
            throw new AisisCore_Template_TemplateException('Could not find: ' . $template_name . '.phtml at ' . $path);
        }

        require_once ($this->_options['template_view_path'] . $template_name . '.phtml');
    }
}

Note: the part to focus on is the if(is_array()){} loop.

  • 写回答

1条回答 默认 最新

  • dongyi6195 2013-02-20 21:43
    关注

    Shouldn't:

    if(is_array($this->_options['template_view_path'])){
        foreach($templates as $template=>$path){
            require_once($path . $template_name . '.phtml');
        }
    }else{
    

    be:

    if(is_array($this->_options['template_view_path'])){
        foreach($this->_options['template_view_path'] as $template=>$path){
            if (file_exists($filename = $path . $template_name . '.phtml')) {
                require_once($filename);
                return;
            }
        }
    
        throw new AisisCore_Template_TemplateException('Could not find: ' . $template_name . '.phtml in any paths');
    }else{
    

    You're never actually looping $this->_options['template_view_path'].

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)