<NME> loader.php
<BEF> <?php
define('APP_TIME_START', microtime(true));
define('IPHP_PATH', dirname(__FILE__));
include_once(dirname(__FILE__) . '/functions.php');

mb_internal_encoding("UTF-8");

$AUTOLOAD_PATH =  array(
	dirname(__FILE__) . '/framework',
	APP_PATH . '/models',
	APP_PATH . '/classes',
);

spl_autoload_register(function ($cls){
	global $AUTOLOAD_PATH;
	foreach($AUTOLOAD_PATH as $dir){
		$file = $dir . '/' . $cls . '.php';
		if(file_exists($file)){
			require_once($file);
			return;
		}
	}
	// 有很多代码会使用 class_exists(), 需要和它们兼容, 所以不能在这里 throw
#	if(!class_exists($cls, false)){
#		throw new Exception("Class $cls not found!");
#	}
});

// init application
App::init();

	return array($base, $controller, $action);
}

function find_layout_file(){
	$layout = 'layout';
	$path = base_path();
	if(App::$controller->layout === false){
		return false;
	}
	if(App::$controller->layout){
		$layout = App::$controller->layout;
	}
	foreach(App::$controller->view_path as $view_path){
		$ps = explode('/', $path);
		while(1){
			$base = join('/', $ps);
			$file = APP_PATH . "/$view_path/$base/$layout.tpl.php";
			if(file_exists($file)){
				Logger::trace("Layout: $file");
				return $file;
			}
			if(!$ps){
				break;
			}
			array_pop($ps);
		}
	}
	return false;
}

function find_view_file(){
	foreach(include_paths() as $path){
		// 由 Controller 指定模板的名字
		if(App::$controller->action && App::$controller->action != 'index'){
				$file = $dir . "/$action.tpl.php";
			}
			if(file_exists($file)){
				return $file;
			}
		}
	}
	return false;
}



<MSG> render view before layout

<DFF> @@ -107,34 +107,7 @@ function route(){
 	return array($base, $controller, $action);
 }
 
-function find_layout_file(){
-	$layout = 'layout';
-	$path = base_path();
-	if(App::$controller->layout === false){
-		return false;
-	}
-	if(App::$controller->layout){
-		$layout = App::$controller->layout;
-	}
-	foreach(App::$controller->view_path as $view_path){
-		$ps = explode('/', $path);
-		while(1){
-			$base = join('/', $ps);
-			$file = APP_PATH . "/$view_path/$base/$layout.tpl.php";
-			if(file_exists($file)){
-				Logger::trace("Layout: $file");
-				return $file;
-			}
-			if(!$ps){
-				break;
-			}
-			array_pop($ps);
-		}
-	}
-	return false;
-}
-
-function find_view_file(){
+function find_view(){
 	foreach(include_paths() as $path){
 		// 由 Controller 指定模板的名字
 		if(App::$controller->action && App::$controller->action != 'index'){
@@ -151,11 +124,56 @@ function find_view_file(){
 				$file = $dir . "/$action.tpl.php";
 			}
 			if(file_exists($file)){
-				return $file;
+				return array($view_path, $file);
 			}
 		}
 	}
 	return false;
 }
 
+function find_layout_file($view_path){
+	$layout = 'layout';
+	$path = base_path();
+	if(App::$controller->layout === false){
+		return false;
+	}
+	if(App::$controller->layout){
+		$layout = App::$controller->layout;
+	}
+	
+	$ps = explode('/', $path);
+	while(1){
+		$base = join('/', $ps);
+		$file = APP_PATH . "/$view_path/$base/$layout.tpl.php";
+		if(file_exists($file)){
+			return $file;
+		}
+		if(!$ps){
+			break;
+		}
+		array_pop($ps);
+	}
+	return false;
+}
 
+function find_view_and_layout(){
+	$view = find_view();
+	$view_file = false;
+	$layout_file = false;
+	if($view){
+		$view_file = $view[1];
+		$layout_file = find_layout_file($view[0]);
+		if($layout_file){
+			array_unshift(App::$controller->view_path, $view[0]);
+		}
+	}
+	if(!$layout_file){
+		foreach(App::$controller->view_path as $view_path){
+			$layout_file = find_layout_file($view[0]);
+			if($layout_file){
+				break;
+			}
+		}
+	}
+	return array($view_file, $layout_file);
+}

render view before layout