SupeSite後臺添加新聞增加【預覽】功能
實現目的:爲了添加新聞時候更方便!
實現原因:添加新聞時候可能有些樣式會出現錯誤,生成靜態頁之後再去看的時候會比較浪費時間。
實現步驟:
1.在後臺找到admin/tpl/spacenews.hml
2.在<div class="buttons">
加一行:
<input type="button" value="預覽一下" style="color:red;font-size:16px">
</div>
3.在當前文件底部用ajax方法方法跳轉
<script language="javascript" type="text/javascript">
function yulan(){
//分別獲得你想要的值 去表單中相對應的id值
var subject = jq("#sub").val();
var newsauthor =jq("#new").val();
var newsfrom = jq("#from").val()
//獲得編輯器中的值注意
et.save();
var message = jq("[name^=me]").val();
jq.ajax({
url:"admincp.php?action=控制器&type=$type&op=方法",//傳到的控制器和方法中
data:"subject="+sub+"&newsr="+news+"&from="+from+"&me="+me,
type:"post",
dataType:"html",
success:function(info){
var a = window.open("html/yulan/yulan"+info+".htm");//返回值並彈出新窗口中預覽
}
})
}
</script>
4.在對應的控制器中寫相對應的方法即可(注意:因爲用的是$_GET['po=方法名']所以儘量在模版對應的控制器中寫)
elseif ($_GET['op'] == 'yulan'){
//判讀需要的目錄是否存在
if(!is_dir(S_ROOT.'./html/yulan')){
mkdir(S_ROOT.'./html/yulan',0777);
}
//獲得當前用戶的UID方便區別模版生成時的名字
$filenameuid=$_SGLOBAL['supe_uid'];
//獲得分類catid
$catid=$_POST['catid'];
$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('categories')." WHERE catid='$catid'");
//遍歷之後得到 分類首頁模版 的 tpl值
$thecat = $_SGLOBAL['db']->fetch_array($query);
//判斷 分類首頁模版 的 tpl不爲空 &&(和)檢查文件是否存在 (針對於資訊分類----預覽功能)
if(!empty($thecat['viewtpl']) && file_exists(S_ROOT.'./templates/'.$_SCONFIG['template'].'/'.$thecat['viewtpl'].'.html.php')) { //分類名字
$tplname = $thecat['viewtpl'];
}else {
//判斷頻道菜單名字 頻道類型 頻道tpl(分類)(和)檢查文件是否存在 (針對於資訊管理----預覽頻道)
if(!empty($channels['menus'][$thecat['type']]['viewtpl']) && file_exists(S_ROOT.'./templates/'.$_SCONFIG['template'].'/'.$channels['menus'][$thecat['type']]['viewtpl'].'.html.php')) {
//分類名字
$tplname = $channels['menus'][$thecat['type']]['viewtpl'];
}else {
//分類名字
$tplname = 'news_view';
}
}
//分別賦值到前臺的模版中
$channelname = $channels['menus'][$channel]['name'];
$news[subject] = shtmlspecialchars(trim($_POST['subject']));
$news[newsfrom] = shtmlspecialchars(trim($_POST['newsfrom']));
$news[newsauthor] = shtmlspecialchars(trim($_POST['newsauthor']));
$news[message] = stripcslashes($_POST['message']);
ob_clean();
include template($tplname);
$yl = ob_get_contents();
file_put_contents("html/yulan/yulan$filenameuid.htm",$yl);
ob_clean();
echo $filenameuid;
exit;
}