[SMARTY] FCKeditor and Smarty

Chris Hubbard <[email protected]>
Newsgroups gmane.comp.php.smarty.general
Message-ID <[email protected]>
A bit ago, someone asked about FCKeditor and Smarty.  I spent some time 
on it, and here's one implementation.

PHP file, edit.php:
<?php
require_once ("../config/config.php");
require_once ("../libs/FCKeditor/fckeditor.php") ;

if (array_key_exists("FCKeditor1", $_POST))
{
//    echo "<pre>";
//    print_r($_POST);
//    echo "</pre>";
     $bytes = file_put_contents("../templates/right_col.tpl", 
$_POST['FCKeditor1']);
}

$sBasePath = "../libs/FCKeditor/";

$right_col = file_get_contents("../templates/right_col.tpl");
$oFCKeditor = new FCKeditor('FCKeditor1') ;
$oFCKeditor->BasePath   = $sBasePath ;
$oFCKeditor->Value      = $right_col;
$oFCKeditor->Height = '400';

$tpl->assign("data", $oFCKeditor->CreateHtml()) ;
$tpl->display("edit.tpl");
?>

Comments about edit.php
1.  the config.php include contains the standard code to instantiate 
smarty with $tpl as the object reference
2.  I put FCKeditor folder in ../libs, which isn't a web browsable 
location.  For this example, it doesn't really matter where it goes, 
for security, it does matter.
3.  Make sure you get the $sBasePath right.  If you don't it won't work 
at all.
4.  The standard FCKeditor examples use $oFCKeditor->Create().  But 
that method echos the FCKeditor stuff.  What we want it the FCKeditor 
stuff returned so we can assign it to Smarty.  Luckly FCKeditor has the 
CreateHtml() method which is exactly what is needed.
5.  This example uses the PHP5 file_get_content and file_put_content() 
functions.  If you're running PHP4.x either get the PHPCompat library, 
or use fopen, fgets, fclose, and fopen, fputs, fclose instead.

Template file, edit.tpl:
<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" 
xml:lang="en_US">

<head>
   <title> </title>

</head>

<body>
<form action="edit.php" method="post">
{$data}
<br>
<input type="submit" value="Submit">
</form>

</body>
</html>

Comments about edit.tpl
1.  um...  nothing very interesting here.

Note, this example illustrates how to get FCKeditor working with 
Smarty.  In real life you would have all kinds of security measures in 
place to prevent people doing bad things.

If there's enough interest to create an actual Smarty custom function, 
I will, but it's easy enough as it is.
Chris
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.