/**
* 根据filename构建完整的模板路径,如果filename是以斜杠开头,就表示相对于webroot网站根目录的路径,否则就是相对于当前主执行脚本的路径
*/
static void builtin_template_get_fullpath(char * full_path, char * filename, MAIN_DATA * my_data)
{
if(filename[0] == '/') {
char * webroot = main_get_webroot();
int append_length = 0;
append_length += main_full_path_append(full_path, append_length, FULL_PATH_SIZE, webroot);
append_length += main_full_path_append(full_path, append_length, FULL_PATH_SIZE, filename);
full_path[append_length] = '\0';
}
else
builtin_make_fullpath(full_path, filename, my_data);
}
...........................................................
/**
* 解析partial模板语法时,会调用的回调函数
* 例如:
* {{> header.tpl}}
* 在渲染时,就会调用下面这个回调函数,将header.tpl子模板的内容读取并解析出来
*/
static int builtin_crustache__partial(ZL_EXP_VOID * VM_ARG, crustache_template **partial, char * partial_name, size_t name_size)
{
char full_path[FULL_PATH_SIZE];
if(name_size == 0)
return -1;
char tmp = partial_name[name_size];
char * api_name = "bltMustacheFileRender";
int file_size;
partial_name[name_size] = '\0';
MAIN_DATA * my_data = zenglApi_GetExtraData(VM_ARG, "my_data");
builtin_template_get_fullpath(full_path, partial_name, my_data);
partial_name[name_size] = tmp;
char * file_contents = builtin_get_file_content(VM_ARG, full_path, api_name, &file_size);
(*partial) = builtin_crustache_new_template(VM_ARG, file_contents, api_name, file_size, full_path);
zenglApi_FreeMem(VM_ARG, file_contents);
return 0;
}
...........................................................
/**
* bltMustacheFileRender模块函数,渲染mustache模板
* filename参数表示模板文件名(可以是相对于当前执行脚本的相对路径),可选的array参数表示需要在模板中渲染的数据(一个哈希数组)
* .........................................................
*/
ZL_EXP_VOID module_builtin_mustache_file_render(ZL_EXP_VOID * VM_ARG,ZL_EXP_INT argcount)
{
ZENGL_EXPORT_MOD_FUN_ARG arg = {ZL_EXP_FAT_NONE,{0}};
if(argcount < 1)
zenglApi_Exit(VM_ARG,"usage: bltMustacheFileRender(filename[, array])");
zenglApi_GetFunArg(VM_ARG,1,&arg); //得到第一个参数
if(arg.type != ZL_EXP_FAT_STR)
zenglApi_Exit(VM_ARG,"first argument filename of bltMustacheFileRender must be string");
char * filename = arg.val.str;
char full_path[FULL_PATH_SIZE];
MAIN_DATA * my_data = zenglApi_GetExtraData(VM_ARG, "my_data");
builtin_template_get_fullpath(full_path, filename, my_data);
int file_size;
char * api_name = "bltMustacheFileRender";
char * file_contents = builtin_get_file_content(VM_ARG, full_path, api_name, &file_size);
crustache_template *template = builtin_crustache_new_template(VM_ARG, file_contents, api_name, file_size, full_path);
.........................................................
}
|