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.

Customising the CMS /

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

Using $ Template Variables in CMS Content area ?


Go to End


7 Posts   1953 Views

Avatar
pinkp

Community Member, 182 Posts

2 May 2013 at 12:39am

I want to use template variables in the CMS. The same way UserDefinedForms allows you to place $UserDefinedForm in the Content HTML editor field.

This may have been covered but I cant find the correct example.

I want to be able to add a new tab to my page such as "My Custom Code" and the information on this tab to then be placed at the users choice in their content using $MyCustomCode.
I would also like to do this with any extra fields I create in the Site Config.

I found this code in the UserDefinedForms but not sure how to translate it to my situation, or if this is all you need etc.

/**
	 * Using $UserDefinedForm in the Content area of the page shows
	 * where the form should be rendered into. If it does not exist
	 * then default back to $Form.
	 *
	 * @return array
	 */
	public function index() {
		if($this->Content && $form = $this->Form()) {
			$hasLocation = stristr($this->Content, '$UserDefinedForm');
			if($hasLocation) {
				$content = str_ireplace('$UserDefinedForm', $form->forTemplate(), $this->Content);
				return array(
					'Content' => DBField::create_field('HTMLText', $content),
					'Form' => ""
				);
			}
		}

		return array(
			'Content' => DBField::create_field('HTMLText', $this->Content),
			'Form' => $this->Form()
		);
	}

thanks

Avatar
Willr

Forum Moderator, 5523 Posts

3 May 2013 at 9:01pm

You can do like userforms (exactly as you found - str_replace()) or use short codes http://www.ssbits.com/tutorials/2010/2-4-using-short-codes-to-embed-a-youtube-video/

Avatar
pinkp

Community Member, 182 Posts

3 May 2013 at 10:37pm

Thanks Willr, just a couple more questions, could you please show me how to change it for my variable $MyVariable

This has nothing to default back to like in the example, and what do I replace Form with. Your advice would be much appreciated.

Avatar
Willr

Forum Moderator, 5523 Posts

3 May 2013 at 10:44pm

Sorry, not sure I follow your problem with the code you posted? Say you want to do as UserForms does but replace $MyVariable simply remove the Form specific stuff and update the $MyVariable.

public function index() {
$value = "My AWESOME Value";

if($this->Content) {
$hasLocation = stristr($this->Content, '$MyVariable');

if($hasLocation) {
$content = str_ireplace('$MyVariable', $value, $this->Content);
return array(
'Content' => DBField::create_field('HTMLText', $content),
);
}
}

return array(
'Content' => DBField::create_field('HTMLText', $this->Content),
);
}

Avatar
pinkp

Community Member, 182 Posts

3 May 2013 at 10:57pm

The problem was I just wasn't sure how to strip it :)
OK so I added that to the Page.php and now where ever I place $MyVariable it shows "My AWSEOME Value"

But what I want it to return is the content of a field in the CMS. So usually I show the content of a field in the template with $MyVariableContent, but I want the user to fill in the field. "this is the users content" then in the content area they place $MyVariable and this shows "this is the users content" not the $value content My AWSOME Value...

hope that makes sense, thanks for your patience.

Avatar
Willr

Forum Moderator, 5523 Posts

3 May 2013 at 11:12pm

Then replace $value = "My AWESOME Value"; with $value = $this->Field;

Where Field is the name of your database field.

Avatar
pinkp

Community Member, 182 Posts

3 May 2013 at 11:29pm

Exactly what I needed, thanks so much, that will be so useful. Cheers