FCKeditor(現已更名為 CKEditor)曾是最流行的線上富文本編輯器。本文說明如何在 Smarty 模板專案中正確引入並使用 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 = "";
$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');
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,模板直接輸出變數即可。
BasePath 路徑是否正確指向 FCKeditor 目錄,確認瀏覽器控制台無 404 錯誤。method="post",且 PHP 以 $_POST['Content'] 接收(對應編輯器名稱)。