首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

Smarty 整合 FCKeditor 完整實作教學

     編輯:PHP 相關知識

Smarty 整合 FCKeditor 完整實作教學

FCKeditor(現已更名為 CKEditor)曾是最流行的線上富文本編輯器。本文說明如何在 Smarty 模板專案中正確引入並使用 FCKeditor,包含兩種常見整合方式與避坑指南。

方法一:直接輸出 HTML 字串(不推薦用於編輯現有資料)

require_once("conn.php");
require_once("class/Smarty.class.php");

$smarty = new Smarty();
$smarty->template_dir = "../templates";
$smarty->compile_dir  = "../templates_c";
$smarty->left_delimiter  = "<{";
$smarty->right_delimiter = "}>";

$editor = new FCKeditor("Content");
$editor->BasePath   = "../FCKeditor/";
$editor->ToolbarSet = "Basic";
$editor->Value      = "";
$FCKeditor = $editor->CreateHtml();

$smarty->assign('Title', "Rossy is here waiting for you");
$smarty->assign('FCKeditor', $FCKeditor);
$smarty->display('template.tpl');

缺點:若需編輯現有資料(回顯內容),直接將 HTML 字串 assign 給模板變數,FCKeditor 往往無法正確載入值,只會顯示空白編輯器。

方法二:傳遞物件引用(推薦)

require_once("conn.php");
require_once("class/Smarty.class.php");

$smarty = new Smarty();
$smarty->template_dir = "../templates";
$smarty->compile_dir  = "../templates_c";
$smarty->left_delimiter  = "<{";
$smarty->right_delimiter = "}>";

$editor = new FCKeditor("Content");
$editor->BasePath   = "../FCKeditor/";
$editor->ToolbarSet = "Basic";
$editor->Value      = "Here is a example of smarty and FCKeditor";

$smarty->assign('Title', "Rossy is here waiting for you");
$smarty->assign_by_ref("FCKeditor", $editor);  // 關鍵:傳遞物件引用
$smarty->display('template.tpl');

模板檔 template.tpl



example of smarty use fckeditor


Example

title: <{$Title}>

content:

<{$FCKeditor}>

{/* 直接輸出物件,Smarty 會自動呼叫 __toString() 或 CreateHtml() */}

關鍵差異解析

  • assign():傳遞值的副本,適合純字串/數字/陣列。
  • assign_by_ref():傳遞物件引用,模板輸出 <{$FCKeditor}> 時,會自動呼叫物件的 CreateHtml() 方法,確保編輯器正確渲染並帶入 Value 內容。

替代方案與現代建議

FCKeditor 已停止維護,現代專案建議改用 CKEditor 4/5、TinyMCE 或 Editor.md(Markdown 編輯器)。整合方式同理:在 PHP 實例化編輯器物件,設定參數後以 assign_by_ref 傳給 Smarty,模板直接輸出變數即可。

常見問題 FAQ

Q: 模板輸出後編輯器不顯示?
檢查 BasePath 路徑是否正確指向 FCKeditor 目錄,確認瀏覽器控制台無 404 錯誤。
Q: 表單提交後內容為空?
確認表單 method="post",且 PHP 以 $_POST['Content'] 接收(對應編輯器名稱)。
Q: 伺服器不支援 MSXML 導致 FCKeditor 失效?
此為 FCKeditor 舊版已知問題,請直接升級至 CKEditor 或改用其他編輯器。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved