smarty中使用fckeditor
网上最常用的做法是先在php中调用fckeditor类的createHtml方法生成一段html,直接assign给一个samrty变量即可
这里我通过smarty的插件机制,可以更方便的在smarty中集成fckeditor,在smarty的plugin目录中新建文件function.fck.php
内容如下
<?php
function smarty_function_fck($params, &$smarty)
{
if(!isset($params['InstanceName']) || empty($params['InstanceName']))
{
$smarty->trigger_error(‘fckeditor: required parameter “InstanceName” missing’);
}static $base_arguments = array();
static $config_arguments = array();if(!count($base_arguments))
$init = TRUE;
else
$init = FALSE;if(isset($params['BasePath']))
{
$base_arguments['BasePath'] = $params['BasePath'];
}
else if(empty($base_arguments['BasePath']))
{//这里设置默认的fck所在的目录,相对于要使用fck的程序的目录
$base_arguments['BasePath'] = ‘../plugins/fckeditor/’;
}$base_arguments['InstanceName'] = $params['InstanceName'];
if(isset($params['Value'])) $base_arguments['Value'] = $params['Value'];
if(isset($params['Width'])) $base_arguments['Width'] = $params['Width'];
if(isset($params['Height'])) $base_arguments['Height'] = $params['Height'];
if(isset($params['ToolbarSet'])) $base_arguments['ToolbarSet'] = $params['ToolbarSet'];
if(isset($params['CheckBrowser'])) $base_arguments['CheckBrowser'] = $params['CheckBrowser'];
if(isset($params['DisplayErrors'])) $base_arguments['DisplayErrors'] = $params['DisplayErrors'];// Use all other parameters for the config array (replace if needed)
$other_arguments = array_diff_assoc($params, $base_arguments);
$config_arguments = array_merge($config_arguments, $other_arguments);$out = ”;
if($init)
{
$out .= ‘<script type=”text/javascript” src=”‘ . $base_arguments['BasePath'] . ‘fckeditor.js”></script>’;
}$out .= “\n<script type=\”text/javascript\”>\n”;
$out .= “var oFCKeditor = new FCKeditor(‘” . $base_arguments['InstanceName'] . “‘);\n”;foreach($base_arguments as $key => $value)
{
if(!is_bool($value))
{
// Fix newlines, javascript cannot handle multiple line strings very well.
$value = ‘”‘ . preg_replace(“/[\r\n]+/”, ‘” + $0″‘, addslashes($value)) . ‘”‘;
}
$out .= “oFCKeditor.$key = $value; “;
}foreach($config_arguments as $key => $value)
{
if(!is_bool($value))
{
$value = ‘”‘ . preg_replace(“/[\r\n]+/”, ‘” + $0″‘, addslashes($value)) . ‘”‘;
}
$out .= “oFCKeditor.Config[\"$key\"] = $value; “;
}$out .= “\noFCKeditor.Create();\n”;
$out .= “</script>\n”;return $out;
}
?>
使用代码
{fck InstanceName=”body” BasePath=“../plugins/fckeditor/” Value=$news_info.body Width=”100%” Height=”400″}
可以自定义参数 ToolbarSet-使用的工具栏, BasePath-fck相对于当前脚本的目录,InstanceName-要赋予的$_POST变量名, Value-默认值等
最近评论