php替换:
$path = \Yii::getAlias('@xxx').'/views/site/notice-detail.php';
$content = file_get_contents($path); //引入模板
//获取要替换的值进行字符串的替换
$content = str_replace('{top_title}',$title,$content);
$content = str_replace('{title}',$title,$content);
.......
$dir = $path ."/html/";
if (!file_exists($dir)) {
mkdir($dir);
}
$filename=$dir.'/'.$filename; //这里就不判断html是否存在了,因为写入会覆盖
$result = file_put_contents($filename,$content);//写入内容到对应静态文件中
大概就是先通过file_get_contents获取模板页的内容,然后通过str_replace进行标签的替换,替换完成之后,通过file_pu_contents写入到新文件即可。生成的html,我们可以生成多个html,让前端根据不同的页面去访问不同的html即可。
三、生成预览
生成html之后,一般来说是需要预览给工作人员看看的,毕竟人家也不懂技术,不知道到底生成的是啥
1、使用dialog打开窗口
静态页:
//这是我们要打开的窗口,先隐藏 <div id="dialog-form-record" style="display:none;"> <div id="Content_record"> </div> </div>
JS定义底部按钮:
var arrButton = {
"Release": {
'text': '按钮名称',
'priority': 'secondary',
'class': 'btn btn-success',
'id':'',
'click':点击事件
},
"Cancel": {
'text': 'Cancel', //取消按钮
'priority': 'secondary',
"id":'xxx',
'click': function () {
dialogRecord.dialog( "close" );
}
}
};
//定义宽高
dialogRecord = $( "#dialog-form-record" ).dialog({
autoOpen: false,
height: 800,
width: 1400,
modal: true,
buttons:arrButton,
close: function() {
$( "#Content_record" ).html("");
dialogRecord.dialog( "close" );
}
});
JS打开窗口:
//定义标题和窗口大小
$( "#dialog-form-record" ).dialog( "option", "title", "Preview Html" );
$( "#dialog-form-record" ).dialog({
modal: true,
height: 800,
width: 1400
});
2、使用iframe引入刚才生成的静态文件
(1)js引入iframe
//加个时间戳,防止缓存 for (i in response.) iframe += "<iframe class='news_iframe' id='iframe"+ i +"' src='"+ response.url.url +"/news/html/"+ response.zh[i] + "?timestamp= " + new Date().getTime() + "'></iframe>"; } //把iframe写入到html $( "#en_content" ).html(iframe);
3、注意:
(1)iframe的src里面不能有空格之类的东西
(2)iframe去除边框
(3)iframe加载速度慢,所以加个onload事件,当iframe加载完之后再显示
