Jump to:

23459 Posts in 18906 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » How might I best allow CMS edited content to include a little bit of data from my database?

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

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

Page: 1
Go to End
Author Topic: 947 Views
  • tobych
    Avatar
    Community Member
    97 Posts

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

    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

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: How might I best allow CMS edited content to include a little bit of data from my database? Link to this post

    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".

  • tobych
    Avatar
    Community Member
    97 Posts

    Re: How might I best allow CMS edited content to include a little bit of data from my database? Link to this post

    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

    947 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.