Index: trunk/xpressme_integration_kit/include/xpress_block_render.php
===================================================================
--- trunk/xpressme_integration_kit/include/xpress_block_render.php	(revision 462)
+++ trunk/xpressme_integration_kit/include/xpress_block_render.php	(revision 463)
@@ -52,4 +52,18 @@
 			}
 			return $block_id;
+	}
+
+	function get_block_mid($mydirname)
+	{
+			$xoopsDB =& Database::getInstance();
+			$modules_tbl = $xoopsDB->prefix('modules');	
+
+			$sql = "SELECT mid FROM $modules_tbl WHERE dirname = '$mydirname'";
+			$result =  $xoopsDB->query($sql, 0, 0);
+			if ($xoopsDB->getRowsNum($result)  > 0){
+				$row = $xoopsDB->fetchArray($result);
+				$mid = $row['mid'];
+			}
+			return $mid;
 	}
 
@@ -190,12 +204,43 @@
 		$option = explode('|' , $block['options']);
 		
-		if (isset($option[1]))
+		if (isset($option[1])) {
 			$templates_file = $option[1];
-		else
+		} else {
 			$templates_file = 'db:'.$mydirname. '_' . str_replace(".php", ".html", $block_function_name);
+		}
+		
 		$tpl =& new XoopsTpl() ;
+		$tpl->template_dir = $xoops_config->module_path . '/templates';
+		if (!$tpl->template_exists($templates_file)){
+			$src_file_path = $xoops_config->module_path . '/templates/' .$mydirname. '_' . str_replace(".php", ".html", $block_function_name);
+			add_xpress_tpl($mydirname,$templates_file,$src_file_path);
+		}
 		$tpl->assign( 'block' , $block ) ;
 		$ret['content'] = $tpl->fetch( $templates_file ) ;
 		return $ret ;
+	}
+	
+	function add_xpress_tpl($mydirname,$templates='',$src_file_path){
+		global $wpdb,$xoops_config , $xoops_db;
+		
+		$mid = get_block_mid($mydirname);
+
+		$temp_parm = explode(':' , $templates);
+		if (empty($temp_parm[1])) {
+			$filename=$temp_parm[0];
+			$type = 'db';
+		} else  {
+			$filename=$temp_parm[1];
+			$type = $temp_parm[0];
+		}
+		$temp_file_path = $xoops_config->module_path . '/templates/'. $filename;
+		$pattern = '^' . $mydirname . '_';
+		if (preg_match('/' . $pattern . '/' , $filename, $match)){ // file prefix check
+			if (!file_exists($temp_file_path)){		// Repetition check
+				if (file_exists($src_file_path)){	// source file check
+					$rcd = copy($src_file_path, $temp_file_path);
+				}
+			}
+		}
 	}
 	
@@ -250,4 +295,5 @@
 	{
 		global $xoops_db,$xoops_config;
+		
 		$mid = get_xpress_modid();
 		$sql = "SELECT bid,options,func_file FROM " . get_xoops_prefix() . "newblocks WHERE mid = $mid AND visible = 1";
@@ -294,6 +340,5 @@
 			return $xu_row['conf_value'];
 		}
-	}
-	
+	}	
 }	
 ?>
