2013-02-22 42 views
0

我有以下php函數,基於路徑數組呈現模板數組。換句話說,如果你提供了一組陣列,如:我在哪裏拋出異常?

$array_template = array(
    'carousel' => 'carousel', //type=>name (with out extension). 
    'mini' => 'mini_feed' 
) 

$array_paths = array(
    'path_one' => 'path/to/one/', 
    'path_two' => 'path/to/two/' 
) 

給這個函數:

protected function _render_templates_array($templates, array $template_name){ 
    foreach($template_name as $type=>$name){ 
     foreach($templates as $template=>$path){ 
      if(file_exists($path . $name . '.phtml')){ 
       require_once($path . $name . '.phtml'); 
      } 
     } 
    } 

    return; 
} 

應該找到和渲染每一個文件,檢查該文件的每一條路徑。

我遇到的問題是,我想出瞭如何在找到所有文件後停止搜索,如何添加一個else到if並拋出我的錯誤?或者有什麼地方我應該拋出我的錯誤?

基本上我需要:

  1. 渲染的所有模板,確保在這些模板的所有路徑看。
  2. 如果在任何路徑中找不到模板,則會引發錯誤。
  3. 所有文件加載後停止處理。

想法?

+0

這一切都取決於你的應用程序需要做什麼。如果您可以應對未找到模板,請繼續並繼續使用其他模板。如果找到模板是基礎性的,並且找不到一個模板,則停止並拋出異常。純粹是你的選擇。 – GarethL 2013-02-22 16:41:33

回答

1

之間的兩個foreach行,添加$found = false;。在if的內部,添加$found = true;在兩個「最終foreach」}之間,根據需要添加if(!$found) throw.....;

protected function _render_templates_array($templates, array $template_name){ 
    foreach($template_name as $type=>$name){ 
     $found = false; 
     foreach($templates as $template=>$path){ 
      if(file_exists($path . $name . '.phtml')){ 
       require_once($path . $name . '.phtml'); 
       $found = true; 
      } 
     } 
     if(!$found) throw new .......; 
    } 

    return; 
}