SilverStripe

Run function within article content

Sometimes you need to execute a function within a page article, and the easiest way is to write your functionality in your Controller (ie Page.php, Page_Controller class) and drop in a unique string within the article. In the demo below I have created a unique string '$DemoFunction' to write in my article content, which will be replaced by the content of the function DemoFunction();

In the Page_Controller class (mysite/code/Page.php)

function Content() {
// get current content
$newContent = $this->Content;

// do replacements
if( substr_count($newContent, '$DemoFunction') > 0 )
   $newContent = str_replace('$DemoFunction', $this->DemoFunction(), $newContent); 

return $newContent;
}