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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

How might I best allow CMS edited content to include a little bit of data from my database?


Go to End


3 Posts   1542 Views

Avatar
tobych

Community Member, 97 Posts

20 March 2009 at 2:04pm

On this page:

http://www.multicriteria-mapping.org/download-mc-mapper/

I have this text:

To start the process, click on _this link_ and save the file.

I want the URL behind _this link_, and perhaps a couple of other things on the page, to come from variables in my page type. Obviously I'd normally have the above text in a template. But there's a lot of text on this page, that gets edited using the CMS, and just this one bit of information that needs to come from the database (via a method DownloadLink on the page class, say).

Should I just suck it up and move some of the page into a template, or is there a way? I'm normally a purist, but...

Technically, it would be straightforward. I can overload whatever method I need on the page class, and replace the string as needed. But because this is a) probably not normal or purist and b) possibly controversial but c) maybe pragmatic, I was wondering what I might do.

Toby

Avatar
Sam

Administrator, 690 Posts

20 March 2009 at 3:40pm

We've done this kind of thing in the past by creating a Content() method on relevant Page sub-class.

A little method with the relevant str_replace commands would do the trick. Here's a trivial example:

function Content() {
  return srt_replace('$MyLink','the-link',$this->Content);
}

The Content() method is called by the '$Content' string in your templates, but crucially, it's not called when the page is being edited inside the CMS.

So - it seems like you mostly knew that this was possible but were wondering if it was acceptable code. I think the answer is "yes". :-)

Avatar
tobych

Community Member, 97 Posts

21 March 2009 at 5:07am

Edited: 21/03/2009 5:08am

Sam, thanks. You're right: I knew it was possible, but was wondering how acceptable it was.

The following is not a question that needs answering. Just ponderances. I'm okay for now. Thanks.

So, I imagine others will find this thread useful. I also wonder whether something like this might become a standard feature of SilverStripe. Or a new module. Specifically, whether the CMS (the WYSIWYG editor, in particular) could cover this functionality, so editors could use a drop down menu or something to include "little bits of information" from the database like this. I'm sure other CMS/Frameworks have this sort of thing.

Maybe having something in the WYSIWYG editor unnecessary because it's so easy to do in PHP, and all users need to know is a little syntax, like $ThisLink or [[ThisLink]] or whatever. But of course, to help them get it right, if there's only a few specific variables they might include, the page class could perhaps have a method that provides the CMS with allowable snippets/variables it can use:

static $snippets = array('ThisLink');

...

function Snippet($snippetName) {
  // assume the snippetName is "ThisLink" :-)
  return "this is my snippet"
}

Or just rely on method names, as the .ss templates do.

If anyone likes this idea, I'm in interested in developing it. Meanwhile I'm gonna stop right here.

Toby