模板替换函数二合一

ct-mobile-first
ycc 5 years ago
parent 4423a77023
commit 8ced108d40

@ -490,8 +490,8 @@ class Information extends CI_Controller
$update_info_log = $this->update_cache($ic_url, true); $update_info_log = $this->update_cache($ic_url, true);
} else if (strcasecmp($site_code, "ch") == 0 && !empty($auto_update_cache)) { } else if (strcasecmp($site_code, "ch") == 0 && !empty($auto_update_cache)) {
//读取模板生成PC和移动优先文件 //读取模板生成PC和移动优先文件
$this->make_mobile_html($information); $this->make_www_cache('mobile',$information);
$this->make_pc_html($information); $this->make_www_cache('pc',$information);
} else if (strcasecmp($site_code, "cht") == 0 && !empty($auto_update_cache)) { } else if (strcasecmp($site_code, "cht") == 0 && !empty($auto_update_cache)) {
$update_info_log = $this->update_cache($ic_url); $update_info_log = $this->update_cache($ic_url);
} else if (strcasecmp($site_code, "ah") == 0 && !empty($auto_update_cache)) { } else if (strcasecmp($site_code, "ah") == 0 && !empty($auto_update_cache)) {
@ -603,99 +603,45 @@ class Information extends CI_Controller
return false; return false;
} }
//生成静态文件 $template='mobile' or 'pc' //生成静态文件 $device='mobile' or 'pc'
function make_www_cache($template,$information){ function make_www_cache($device, $information)
{
$data = array(); $data = array();
switch ($template){ switch ($device) {
case 'mobile': case 'mobile':
//获取移动优先的模板,如果有的话 //获取移动优先的模板,如果有的话
$mobile_first_template_path = 'mobile_first/' . $this->config->item('site_code'); $template_path = 'mobile_first/' . $this->config->item('site_code');
if (is_file(APPPATH . 'views/' . $mobile_first_template_path . EXT)) { if (is_file(APPPATH . 'views/' . $template_path . EXT)) {
$mobile_template = $this->load->view($mobile_first_template_path, '', TRUE); $template = $this->load->view($template_path, '', TRUE);
$html_path_ext = '-mobile.htm';
} else { } else {
return false; return false;
} }
break; break;
case 'pc': case 'pc':
//获取PC的模板如果有的话
$template_path = 'mobile_first/' . $this->config->item('site_code');
if (is_file(APPPATH . 'views/' . $template_path . '-pc' . EXT)) {
$template = $this->load->view($template_path . '-pc', '', TRUE);
$html_path_ext = '-pc.htm';
} else {
return false;
}
break; break;
default: default:
} }
}
function make_mobile_html($information)
{
$data = array();
//获取移动优先的模板,如果有的话
$mobile_first_template_path = 'mobile_first/' . $this->config->item('site_code');
if (is_file(APPPATH . 'views/' . $mobile_first_template_path . EXT)) {
$mobile_template = $this->load->view($mobile_first_template_path, '', TRUE);
} else {
return false;
}
$mobile_template = str_replace('<!--@TITLE@-->', $this->input->post('ic_seo_title'), $mobile_template);
$mobile_template = str_replace('<!--@DESCRIPTION@-->', $this->input->post('ic_seo_description'), $mobile_template);
$mobile_template = str_replace('<!--@KEYWORDS@-->', $this->input->post('ic_seo_keywords'), $mobile_template);
$mobile_template = str_replace('<!--@CANONICAL@-->', $this->input->post('site_url') . $this->input->post('ic_url'), $mobile_template);
//非产品页面
if (empty(get_meta($information->ic_id, 'meta_product_code'))) {
//H1标题样式
$mobile_template_H1 = $this->call_mobile_template_H1($mobile_first_template_path, $this->input->post('is_path'), $this->input->post('ic_author'), $this->input->post('ic_title'), $this->input->post('ic_photo'));//H1模板替换
$mobile_template = str_replace('<!--@HEAD_1@-->', $mobile_template_H1, $mobile_template);
//信息推荐
$template_NEXT = $this->call_mobile_template_NEXT($mobile_first_template_path, $this->input->post('is_id'));
$mobile_template = str_replace('<!--@ARTICLENEXT@-->', $template_NEXT, $mobile_template);
$mobile_template = str_replace('<!--@ADDTHIS-WIDGET@-->', '<script async src="https://s7.addthis.com/js/300/addthis_widget.js#pubid=ra-52170b0a4a301edc"></script>', $mobile_template);
}
//额外样式
$meta_addon_css = get_meta($information->ic_id, 'meta_addon_css');
if (!empty($meta_addon_css)) {
$mobile_template = str_replace('<!--@ADDON-CSS@-->', '<link href="' . $meta_addon_css . '" rel="stylesheet">', $mobile_template);
}
$mobile_template = str_replace('<!--@CUSTOM-CONENT@-->', $this->input->post('ic_content'), $mobile_template);
//Google广告代码
if (!empty(get_meta($information->ic_id, 'meta_google_ad_content'))) {
$mobile_template = str_replace('<!--@Match-Content-GOOGLE@-->', '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-4231674166786366" data-ad-slot="1447610161"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script>', $mobile_template);
}
if (!empty(get_meta($information->ic_id, 'meta_google_ad_article'))) {
$mobile_template = str_replace('<!--@IN-ARTICLE-GOOGLE@-->', '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-4231674166786366" data-ad-slot="5442695494"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script>', $mobile_template);
}
//社媒分享图片
if (!empty($information->ic_photo)) {
$full_ic_photo = $this->config->item('site_image_url') . $information->ic_photo;
$mobile_template = str_replace('<!--@OG:IMAGE@-->', '<meta property="og:image" content="$full_ic_photo"><meta property="og:image:secure_url" content="$full_ic_photo"><meta property="og:image:width" content="800"><meta property="og:image:height" content="450">', $mobile_template);
}
// /travelguide/chinese-zodiac/monthly-fortune-for-dog.htm
$mobile_html = $this->config->item('cache')[$this->config->item('site_code')]['cache_path'] . $this->input->post('ic_url');
$mobile_html = str_replace("\\", "/", $mobile_html);
if (substr($mobile_html, -1, 1) == '/') {
$mobile_html = $mobile_html . 'index.htm';
}
$mobile_html .= '-mobile.htm';
create_folder_by_path(dirname($mobile_html));
file_put_contents($mobile_html, $mobile_template, LOCK_EX);
}
function make_pc_html($information) //替换模板中的标签
{ $template = str_replace('<!--@TITLE@-->', $information->ic_seo_title, $template);
$data = array(); $template = str_replace('<!--@DESCRIPTION@-->', $information->ic_seo_description, $template);
//获取PC的模板如果有的话 $template = str_replace('<!--@KEYWORDS@-->', $information->ic_seo_keywords, $template);
$template_path = 'mobile_first/' . $this->config->item('site_code'); $template = str_replace('<!--@CANONICAL@-->', $this->config->item('site_url') . $information->ic_url, $template);
if (is_file(APPPATH . 'views/' . $template_path . '-pc' . EXT)) {
$template = $this->load->view($template_path . '-pc', '', TRUE);
} else {
return false;
}
$template = str_replace('<!--@TITLE@-->', $this->input->post('ic_seo_title'), $template);
$template = str_replace('<!--@DESCRIPTION@-->', $this->input->post('ic_seo_description'), $template);
$template = str_replace('<!--@KEYWORDS@-->', $this->input->post('ic_seo_keywords'), $template);
$template = str_replace('<!--@CANONICAL@-->', $this->input->post('site_url') . $this->input->post('ic_url'), $template);
//非产品页面 //非产品页面
if (empty(get_meta($information->ic_id, 'meta_product_code'))) { if (empty(get_meta($information->ic_id, 'meta_product_code'))) {
$template_H1 = $this->call_mobile_template_H1($template_path, $this->input->post('is_path'), $this->input->post('ic_author'), $this->input->post('ic_title'), $this->input->post('ic_photo'));//H1模板替换 $template_H1 = $this->call_mobile_template_H1($template_path, $information->is_path, $information->ic_author, $information->ic_title, $information->ic_photo);//H1模板替换
$template = str_replace('<!--@HEAD_1@-->', $template_H1, $template); $template = str_replace('<!--@HEAD_1@-->', $template_H1, $template);
//信息推荐 //信息推荐
$template_NEXT = $this->call_mobile_template_NEXT($template_path, $this->input->post('is_id')); $template_NEXT = $this->call_mobile_template_NEXT($template_path, $information->is_id);
$template = str_replace('<!--@ARTICLENEXT@-->', $template_NEXT, $template); $template = str_replace('<!--@ARTICLENEXT@-->', $template_NEXT, $template);
$template = str_replace('<!--@ADDTHIS-WIDGET@-->', '<script async src="https://s7.addthis.com/js/300/addthis_widget.js#pubid=ra-52170b0a4a301edc"></script>', $template); $template = str_replace('<!--@ADDTHIS-WIDGET@-->', '<script async src="https://s7.addthis.com/js/300/addthis_widget.js#pubid=ra-52170b0a4a301edc"></script>', $template);
} }
@ -704,7 +650,7 @@ class Information extends CI_Controller
if (!empty($meta_addon_css)) { if (!empty($meta_addon_css)) {
$template = str_replace('<!--@ADDON-CSS@-->', '<link href="' . $meta_addon_css . '" rel="stylesheet">', $template); $template = str_replace('<!--@ADDON-CSS@-->', '<link href="' . $meta_addon_css . '" rel="stylesheet">', $template);
} }
$template = str_replace('<!--@CUSTOM-CONENT@-->', $this->input->post('ic_content'), $template); $template = str_replace('<!--@CUSTOM-CONENT@-->', $information->ic_content, $template);
//Google广告代码 //Google广告代码
if (!empty(get_meta($information->ic_id, 'meta_google_ad_content'))) { if (!empty(get_meta($information->ic_id, 'meta_google_ad_content'))) {
$template = str_replace('<!--@Match-Content-GOOGLE@-->', '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-4231674166786366" data-ad-slot="1447610161"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script>', $template); $template = str_replace('<!--@Match-Content-GOOGLE@-->', '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-4231674166786366" data-ad-slot="1447610161"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script>', $template);
@ -718,12 +664,12 @@ class Information extends CI_Controller
$template = str_replace('<!--@OG:IMAGE@-->', '<meta property="og:image" content="' . $full_ic_photo . '"><meta property="og:image:secure_url" content="' . $full_ic_photo . '"><meta property="og:image:width" content="800"><meta property="og:image:height" content="450">', $template); $template = str_replace('<!--@OG:IMAGE@-->', '<meta property="og:image" content="' . $full_ic_photo . '"><meta property="og:image:secure_url" content="' . $full_ic_photo . '"><meta property="og:image:width" content="800"><meta property="og:image:height" content="450">', $template);
} }
// /travelguide/chinese-zodiac/monthly-fortune-for-dog.htm // /travelguide/chinese-zodiac/monthly-fortune-for-dog.htm
$html_path = $this->config->item('cache')[$this->config->item('site_code')]['cache_path'] . $this->input->post('ic_url'); $html_path = $this->config->item('cache')[$this->config->item('site_code')]['cache_path'] . $information->ic_url;
$html_path = str_replace("\\", "/", $html_path); $html_path = str_replace("\\", "/", $html_path);
if (substr($html_path, -1, 1) == '/') { if (substr($html_path, -1, 1) == '/') {
$html_path = $html_path . 'index.htm'; $html_path = $html_path . 'index.htm';
} }
$html_path .= '-pc.htm'; $html_path.=$html_path_ext;
create_folder_by_path(dirname($html_path)); create_folder_by_path(dirname($html_path));
file_put_contents($html_path, $template, LOCK_EX); file_put_contents($html_path, $template, LOCK_EX);
} }
@ -786,7 +732,7 @@ class Information extends CI_Controller
); );
$data['info_topthings'] = $this->Information_model->Detail($random_array[rand(0, count($random_array) - 1)]); $data['info_topthings'] = $this->Information_model->Detail($random_array[rand(0, count($random_array) - 1)]);
if (!empty($data['info_topthings'])) { if (!empty($data['info_topthings'])) {
$data['info_topthings_root'] =$this->Information_model->get_detail_by_path($data['info_topthings']->is_path, 1); $data['info_topthings_root'] = $this->Information_model->get_detail_by_path($data['info_topthings']->is_path, 1);
$data['info_topthings']->ic_photo = $this->set_photo_content($data['info_topthings']->ic_photo, $data['info_topthings']->ic_content); $data['info_topthings']->ic_photo = $this->set_photo_content($data['info_topthings']->ic_photo, $data['info_topthings']->ic_content);
} }
//文化板块的页面,随机挑选一篇 //文化板块的页面,随机挑选一篇

Loading…
Cancel
Save