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

Adding a variable to $content from CMS


Go to End


4 Posts   2030 Views

Avatar
mschiefmaker

Community Member, 187 Posts

4 September 2009 at 11:02am

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

Avatar
mschiefmaker

Community Member, 187 Posts

8 September 2009 at 9:59pm

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

Avatar
bummzack

Community Member, 904 Posts

8 September 2009 at 11:55pm

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.

Avatar
AdamJ

Community Member, 145 Posts

9 September 2009 at 4:09am