21289 Posts in 5733 Topics by 2602 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1163 Views |
-
Adding a variable to $content from CMS

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
-
Re: Adding a variable to $content from CMS

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
-
Re: Adding a variable to $content from CMS

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.
-
Re: Adding a variable to $content from CMS

9 September 2009 at 4:09am
You may also want to take a look at this: http://doc.silverstripe.com/doku.php?id=recipes:customising-content-in-your-templates
| 1163 Views | ||
|
Page:
1
|
Go to Top |


