Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions

Adding dynamic content (module?) while in the editor


Reply

2 Posts   1003 Views

Avatar
Jeff Vogt

17 April 2009 at 6:44pm Community Member, 1 Post

Say I have a function in mysite/code/Page.php:

helloWorld() {
return 'hello world';
}

If I'm editing a template, it's as easy as typing $helloWorld wherever I want the output of my function. What I want to do is be able to do this from SilverStripe Admin when I (or a user) is editing a page.

How would I do this without tearing SS apart? Is this not proper separation of MVC?

Avatar
Ben Gribaudo

18 April 2009 at 1:19am Community Member, 181 Posts

Hi Jeff,

If I understand correctly, you want to be able to type "$HelloWorld" in the TinyMCE editor and then have that string replaced with the value of HelloWorld() when the page is rendered. Is that correct?

When SS renders a page for display, it calls the Content() method on the page's class. You can override this method, adding the behavior you desire.

Example (in your page's class):

   public function Content() {
      return str_replace('$HelloWorld', $this->helloWorld(), $this->Content);
   }

Hope this helps,
Ben