I ran into the problem where I wanted to allow for <%~ 'resource' %> and <%[Hello World]%> in content that was coming from the database. In this way, the content wasn't just being dropped into place with something like TLabel->Text=$content but passed through the prado parser and rendered the prado way. In other words i wanted a full wepage to be able to come from the database.
So, here is the class to implement that functionality...
class TDBContent extends TControl
{
var $dbid = null;
var $contentid = null;
// if before pre-render then the content id may be blank if it's dynamic
public function onPreRender($config) {
parent::onPreRender($config);
if($this->dbid == null)
throw new Exception("The DatabaseID of the database was not specified");
if($this->contentid == null)
throw new Exception("The ContentID was not specified");
if(empty($this->Application->Modules[$this->dbid]))
throw new Exception("There was no database at the id '{$this->dbid}'");
}
public function render($writer) {
$db = $this->Application->Modules[$this->dbid]->Database;
$cmd = $db->createCommand("SELECT content FROM page_content WHERE content_key=:key");
$cmd->bindParameter(":key",$this->contentid,PDO::PARAM_STR);
$pageText = $cmd->queryScalar();
$page = new TPage();
$tpl = new TTemplate($pageText, $this->Page->Template->ContextPath);
$page->setPage($this->getPage());
$page->setTemplate($tpl);
$page->setPagePath($this->contentid);
$page->run($writer);
}
public function getDatabaseId() {return $this->dbid;}
public function setDatabaseId($v) {$this->dbid = $v; }
public function getContentId() {return $this->contentid;}
public function setContentId($v) {$this->contentid = $v;}
}
The table I have for this class is about as simple as it gets
CREATE TABLE `page_content` (
`id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`content_key` VARCHAR( 128 ) NOT NULL ,
`content` LONGTEXT NOT NULL
) ENGINE = MYISAM ;
The explanation.This control doesn't have a template associated with it so it's not a subclass of TTemplateControl. This class is the short form of how prado generates a page. The TTemplate parses the content and the TPage renders it. The ContextPath is important so that assets from the correct directory get published. Run is how the page is actually created. Clearly you can't have any OnClick attributes, active control, or dynamic content beyond what's in that page in the database. Still, it is good for basic editable static pages.
I'm sure there are a number of improvements that can be made, such as I18N, caching, or an option for SafeHTML, but the class above is just a starting point. If you ever want prado to parse any of your content then you can use this example. If you make any improvements to this class I'd love to see them uploaded here.
I'm using it with FCKEditor. Users may create and edit a page with FCKEditor (and upload files) and still have some of the cool features of prado. This works particularly well when you have a set of TTemplateControls that your users can put in the html to create the page. Some may be: WebsiteLoginPanel, SearchBox, MainNavigationWithTopLevelPages and the like.
Enjoy!