Jump to:

23007 Posts in 11866 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Adding a variable to $content from CMS

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: 1320 Views
  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Adding a variable to $content from CMS Link to this post

    This has got to be a really simple question but is it possible to reference a variable in the content field of the CMS?

    I have a page with a section of content which the end user wants to be able to modify regularly. My problem is that within this text there is a phrase which will be defined by a variable from the URL link to this page. Can an enduser include a variable reference i.e. $variable within a CMS form?

    I have tried to do it but the editor just sees it as text. Whats the format to tell it, it's a variable?

    Thanks

    MM

  • mschiefmaker
    Avatar
    Community Member
    187 Posts

    Re: Adding a variable to $content from CMS Link to this post

    Does anyone know the answer to this? I can think of way to work around it but I feel there must be a way to do it.

    I am trying to reference a variable from the URL

    http://www.blah.co.nz/SilverStripe/hints-and-tips?mf=counselling tips

    The within the body of the content I want to be able to reference the variable mf

    Thanks

    Catherine

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Adding a variable to $content from CMS Link to this post

    Hi

    Should be quite simple to accomplish. Place something like this in your Page_Controller

    public function Content(){
       $content = parent::__get('Content');
       $variable = isset($_GET['mf']) ? htmlentities($_GET['mf']) : 'undefined';
       return str_replace('$variable', $variable, $content);
    }

    In the CMS content section you can then place a "variable" called $variable anywhere in the Content area and it will be replaced by the mf query string variable (or undefined if mf isn't set).
    You should be extra careful with things like this though. Anybody could inject code into your site, by setting mf to any desired value... in my example I use the htmlentities function to encode tags to entities, but you might want to further restrict the allowed input to prevent misuse of your site.

    Hope that helps.

  • AdamJ
    Avatar
    Community Member
    145 Posts
    1320 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.