SilverStripe

Mambot through SilverStripe page content

Sometimes it is just not worthwhile creating a new page type just for one tiny piece of functionality. Or perhaps you want the in-page functionality protected from accidental client tanpering? Either way, you can run functionality quite easily from within the page content with this crafty search and replace.

Page.php (inside the controller, ie Page_Controller class)

/* === MAMBOT TO ALLOW FUNCTION RUNNING FROM WITHIN ARTICLES === */
    
    function Content() {        
    
        // get current content
        $newContent = $this->Content;
        
        // if our pre-definined flag is in the content
        if( substr_count($newContent, '$TextInsideContent') > 0 ){
            // replace the flag with the content of our function
            $newContent = str_replace('$TextInsideContent', $this->CustomFunction(), $newContent);
        };
        
        return $newContent;        
    }