function render($tplName, $tplItem) { global $smarty; global $smartyConf; CSmarty::clearError(); // 这里,$data有三种数据格式,第一种是我定义的最原始的数据格式,第二种是有的同学在display里面进行了细分,加了extData,第三种是平台的数据格式 // 所以,在这里先进行一次处理 // $data = preprocess(CJSON::decode(file_get_contents($tplItem['data']))); $data = preprocess(json_decode(file_get_contents($tplItem['data']), TRUE)); $result = $smarty->do_render($data, $tplName); // 有错误发生 $errors = CSmarty::getError(); if (count($errors) > 0) { return $errors[0]; } return $result; }
/** * 通用的单条渲染插件,可渲染aladdin、result、ecom等目录下; * 不能渲染midpage、xml、data和baidu下的完整页面模板 * * @package plugins * @author jiachunxin@baidu.com */ function smarty_modifier_common_render($data, $type, $templateName, $curr_sort = false) { if ($GLOBALS['mulit_render_flag'] === 1) { $key = $data['multi_render_key']; if (empty($key)) { CLog::warning("empty_key!", 1); } return $key; } $logArr['smarty_modifier'] = "common_render"; $conf = CSmarty::getConf(); $language = $conf['language']; $platform = $conf['platform']; $logArr['template_type'] = $type; $logArr['template_name'] = $templateName; $logArr['language'] = $language; $logArr['platform'] = $platform; $logArr['StdStg'] = $data['StdStg']; // result目录下的模板不统计性能 $time_start = Volatile::microtime(true) * 1000; if ($type != $GLOBALS['STRUCT_TEMPLATE_TYPE']) { $log_key = $data['StdStg'] . '_' . $templateName; } else { CSmarty::addStructTplRenderNumCount(); } if (empty($type)) { CLog::warning("template type is empty", -1, $logArr, 1); return false; } if (empty($templateName)) { CLog::warning("template name is empty", -1, $logArr, 1); return false; } if (empty($data['resultData']['tplData']) || !is_array($data['resultData']['tplData'])) { CLog::warning("tplData is empty", -1, $logArr, 1); return false; } $data['resultData']['queryInfo'] = CSmarty::getQueryInfo(); $data['resultData']['templateConfig'] = CSmarty::getTemplateConfig(); if ($curr_sort === false) { $curr_sort = 0; } else { $curr_sort += 1; } $data['resultData']['extData']['curr_sort'] = $curr_sort; // page renderer $smarty = CSmarty::getInstance(array('language' => $language, 'type' => $type, 'platform' => $platform)); if (false === $smarty) { $status = -1; $errors = CSmarty::getError(); $errors_str = implode(";", $errors); $logArr[$type . '_smarty_error'] = $errors_str; CLog::warning("fail to get instance of smarty, type: {$type}", $status, $logArr, 1); return false; } // encrypt page's url backend_encrypt_url($data, $platform, $language, $type, $templateName); // render page $page = $smarty->do_render($data, $templateName, 1); if (empty($page)) { $status = -1; $errors = CSmarty::getError(); $errors_str = implode(";", $errors); CLog::warning("fail to render " . $type . " page, errors[{$errors_str}],info[{$log_key}]", $status, $logArr, 1); return false; } $tm_cost = round(Volatile::microtime(true) * 1000 - $time_start, 2); // result目录下的模板不统计性能 if ($type != $GLOBALS['STRUCT_TEMPLATE_TYPE']) { $GLOBALS['logArr']["aladdin_page_size_{$log_key}"] = strlen($page); $GLOBALS['logArr']["time_aladdin_{$log_key}"] = $tm_cost; $GLOBALS['time_count_temp']['time_aladdin_total'] += $tm_cost; $GLOBALS['time_count_temp']['render_aladdin_num']++; } else { CSmarty::addStructTplRenderTimeCount($tm_cost); // result模板也统计性能,合并打印 CSmarty::recordResultTplRenderTime($tm_cost); } return $page; }
/** * * * @file modifier.real.php * @package plugins * @author liyudong@baidu.com * @date 2011-11-03 10:47 */ function smarty_modifier_aladdin_render($data, $templateName, $curr_sort = false) { $time_start = Volatile::microtime(true) * 1000; $logArr['smarty_modifier'] = "modifier_aladdin_render"; /** * render config */ $conf = CSmarty::getConf(); /** * template type * @var string */ $type = VUI_TEMPLATE_ALADDIN_TEMPLATE_TYPE; $language = $conf['language']; $platform = $conf['platform']; $logArr['template_type'] = $type; $logArr['template_name'] = $templateName; $logArr['language'] = $language; $logArr['platform'] = $platform; $log_key = $data['StdStg'] . '_' . $templateName; /** * template type cannot be empty */ if (strlen($type) == 0) { CLog::warning("template type is empty", -1, $logArr, 1); return false; } /** * template name cannot be empty */ if (empty($templateName)) { CLog::warning("template name is empty", -1, $logArr, 1); return false; } /** * aladdin's tplData */ if (empty($data['resultData']['tplData']) || !is_array($data['resultData']['tplData'])) { CLog::warning("tplData is empty", -1, $logArr, 1); return false; } /** * set queryInfo */ $data['resultData']['queryInfo'] = CSmarty::getQueryInfo(); /** * set templateConfig */ $data['resultData']['templateConfig'] = CSmarty::getTemplateConfig(); /** * set result current sort of result page */ if ($curr_sort === false) { $curr_sort = 0; } else { $curr_sort += 1; } $data['resultData']['extData']['curr_sort'] = $curr_sort; /** * page renderer * @var Smarty */ $smarty = CSmarty::getInstance(array('language' => $language, 'type' => $type, 'platform' => $platform)); if (false === $smarty) { $status = -1; $errors = CSmarty::getError(); $errors_str = implode(";", $errors); $logArr['ala_smarty_error'] = $errors_str; CLog::warning("fail to get instance of smarty, type: {$type}", $status, $logArr, 1); return false; } /** * encrypt page's url */ backend_encrypt_url($data, $platform, $language, $type, $templateName); /** * render aladdin's page */ $page = $smarty->do_render($data, $templateName, 1); if ($page === false) { $status = -1; $errors = CSmarty::getError(); $errors_str = implode(";", $errors); CLog::warning("fail to render aladdin's page, errors[{$errors_str}]", $status, $logArr, 1); return false; } if (strlen($page) == 0) { $status = -1; CLog::warning("aladdin's page is empty", $status, $logArr, 1); return false; } $GLOBALS['logArr']["aladdin_page_size_{$log_key}"] = strlen($page); $GLOBALS['logArr']["time_aladdin_{$log_key}"] = round(Volatile::microtime(true) * 1000 - $time_start, 2); return $page; }
/** * do rendering * @param array [in|out] data * @param array [in|out] result * @param Object [in] smarty */ function render(&$data, &$result, $smarty, $render_header_flag = false) { /** * init render result */ $result['status'] = $GLOBALS['STATUS_CODE']['OK']; $result['msg'] = $GLOBALS['STATUS_MSG'][$result['status']]; //$result['page'] = ''; if ($render_header_flag) { $GLOBALS['mulit_render_flag'] = 0; $page = $smarty->do_render($data['uiData'], $data['uiControl']['templateName'], $data['uiControl']['templateSwitch'], $data['uiControl']['templateVersion']); } else { if ($data['uiControl']['singleItem'] == 1) { /** * tplt name from gss data */ $templateName = $data['uiData']['asResult']['item'][0]['dispData']['strategy']['tempName']; /** * tplt name from uiCrontrol */ if (empty($templateName)) { $templateName = $data['uiControl']['templateName']; } $data['uiData']['asResult']['item'][0]['dispData']['resultData']['queryInfo'] = CSmarty::getQueryInfo(); $data['uiData']['asResult']['item'][0]['dispData']['resultData']['templateConfig'] = CSmarty::getTemplateConfig(); $conf = CSmarty::getConf(); backend_encrypt_url($data['uiData']['asResult']['item'][0]['dispData'], $conf['platform'], $conf['language'], $conf['type'], $templateName); $page = $smarty->do_render($data['uiData']['asResult']['item'][0]['dispData'], $templateName, $data['uiControl']['templateSwitch'], $data['uiControl']['templateVersion']); } else { //并行渲染需要的数据,预处理时顺便就分类了,避免并行渲染时需要再循环遍历一次 $arrAlaData = array(); $arrAsData = array(); // HHVM直连状态下,直接关闭并行渲染 if (!empty($_ENV['HHVM'])) { $GLOBALS['globalConf']['FEATURE_LIST']['MULTI_RENDER']['TURN'] = 'OFF'; } # $arrMultiRenderConf = Util::getConf('/multi_render', 'MULTI_RENDER'); # $bolPreRenderFeature = (Util::getConf('/feature', 'FEATURE_LIST/PREDATA/TURN') === 'ON') ? true : false; # $bolMultiRenderFeature = (Util::getConf('/feature', 'FEATURE_LIST/MULTI_RENDER/TURN') === 'ON') ? true : false; $arrMultiRenderConf = $GLOBALS['globalConf']['MULTI_RENDER']; $bolPreRenderFeature = $GLOBALS['globalConf']['FEATURE_LIST']['PREDATA']['TURN'] === 'ON' ? true : false; $bolMultiRenderFeature = $GLOBALS['globalConf']['FEATURE_LIST']['MULTI_RENDER']['TURN'] === 'ON' ? true : false; //限定baidu模板 //给FE加参数确定是否有预处理 if ($data['uiControl']['platform'] == 'pc' && $data['uiControl']['templateName'] == 'baidu' && $bolPreRenderFeature) { $time_start = Volatile::microtime(true) * 1000; pre_render($data, $arrAlaData, $arrAsData); $data['uiData']['queryInfo']['multiRenderFlag'] = true; $GLOBALS['logArr']['time_pre_render'] = round(Volatile::microtime(true) * 1000 - $time_start, 2); } if ($bolMultiRenderFeature && $bolPreRenderFeature && $data['uiControl']['templateName'] == 'baidu' && $data['uiControl']['platform'] == 'pc' && (!empty($arrAlaData) || !empty($arrAsData))) { CSmarty::setQueryInfo($data['uiData']['queryInfo']); CSmarty::setEncKeyStr($data['uiData']['queryInfo']['encryptionKey']); $time_start = Volatile::microtime(true) * 1000; try { $page = multi_render($data, $smarty, $arrAlaData, $arrAsData, $arrMultiRenderConf); } catch (Exception $e) { $page = ""; if (isset($GLOBALS['multi_render_shmid'])) { $shmid = $GLOBALS['multi_render_shmid']; shmop_delete($shmid); shmop_close($shmid); } CLog::warning("milti error:" . $e->getMessage()); } $GLOBALS['logArr']['time_multi_render_total'] = round(Volatile::microtime(true) * 1000 - $time_start, 2); } if (empty($page)) { $GLOBALS['mulit_render_flag'] = 0; $page = $smarty->do_render($data['uiData'], $data['uiControl']['templateName'], $data['uiControl']['templateSwitch'], $data['uiControl']['templateVersion']); } } } $GLOBALS['logArr']['result_tpl_tm'] = @implode(',', CSmarty::getResultTplRenderTime()); if ($page === false) { $result['page'] = ''; $errors = CSmarty::getError(); $errors_str = implode(";", $errors); $status = $GLOBALS['STATUS_CODE']['RENDER_FAIL']; $result['status'] = $status; $result['msg'] = $GLOBALS['STATUS_MSG'][$status] . ", errors[{$errors_str}]"; CLog::warning($GLOBALS['STATUS_MSG'][$status] . ", errors[{$errors_str}]", $status); return $status; } else { $result['page'] .= $page; } return $GLOBALS['STATUS_CODE']['OK']; }
/** * * * @file modifier.render.php * @package plugins * @author fengfei02@baidu.com * @date 2012-10-29 10:47 */ function smarty_modifier_render($data, $templateName, $curr_sort = false) { $logArr['smarty_modifier'] = "modifier_render"; /** * render config */ $conf = CSmarty::getConf(); /** * template type * @var string */ $type = $GLOBALS['STRUCT_TEMPLATE_TYPE']; $language = $conf['language']; $platform = $conf['platform']; $logArr['template_name'] = $templateName; $logArr['template_type'] = $type; $logArr['language'] = $language; $logArr['platform'] = $platform; /** * template type cannot be empty * * 模板是否修改类型的目录? * */ if (strlen($type) == 0) { CLog::warning("template type is empty", -1, $logArr, 1); return false; } /** * template name cannot be empty */ if (empty($templateName)) { CLog::warning("template name is empty", -1, $logArr, 1); return false; } /** * ac's data */ if (empty($data) || !is_array($data) || empty($data['resultData'])) { CLog::warning("data or resultData is empty", -1, $logArr, 1); return false; } /** * set queryInfo,这个字段在baidu的global变量和函数有用,迁移时有用 */ $data['resultData']['queryInfo'] = CSmarty::getQueryInfo(); /** * set templateConfig,同queryInfo */ $data['resultData']['templateConfig'] = CSmarty::getTemplateConfig(); /** * set result current sort of result page,同queryInfo */ if ($curr_sort === false) { $curr_sort = 0; } else { $curr_sort += 1; } $data['resultData']['extData']['curr_sort'] = $curr_sort; /** * page renderer * @var Smarty */ $smarty = CSmarty::getInstance(array('language' => $language, 'type' => $type, 'platform' => $platform)); if (false === $smarty) { $status = -1; $errors = CSmarty::getError(); $errors_str = implode(";", $errors); $logArr['ac_smarty_error'] = $errors_str; CLog::warning("fail to get instance of smarty, type: {$type}", $status, $logArr, 1); return false; } /** * render ac's page */ $page = $smarty->do_render($data, $templateName, 1); if ($page === false) { $status = -1; $errors = CSmarty::getError(); $errors_str = implode(";", $errors); //CLog::warning("fail to render ac's page, errors[$errors_str]", $status, $logArr, 1); CLog::warning("fail to render ac's page", $status, $logArr, 1); return false; } if (strlen($page) == 0) { $status = -1; CLog::warning("ac's page is empty", $status, $logArr, 1); return false; } return $page; }