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.

General Questions /

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

using built in page controls in "Content" area?


Go to End
Reply


20 Posts   3384 Views

Avatar
janulka

Community Member, 80 Posts

4 February 2011 at 2:26am

Hello,
Is it possible to somehow use built in page controls in Content area?
I would like to use e.g. $Now.Year or $Title or $CurrentMember.FirstName inside the Content without having to make templates for this..
Thanks

Avatar
ttyl

Community Member, 114 Posts

8 February 2011 at 8:43am

Do you mean inside of the $Content output itself?

Avatar
janulka

Community Member, 80 Posts

8 February 2011 at 9:17am

yes..

Avatar
swaiba

Forum Moderator, 1798 Posts

8 February 2011 at 11:02am

how about...
put something like $MyContent instead of $Content in your Pages.ss
create a function like...

function MyContent() {
   $vd = new ViewableData();
   $t = SSViewer::fromString('<% control CurrentPage %>'.$this->Content.'<% end_control %>');
   $ad = new ArrayData(array('CurrentPage' => $this));
   return $vd->customise($ad)->renderWith($t);
}

...it's just a guess, p[robably overcomplicated, but I think it'll work

Avatar
Ryan M.

Community Member, 309 Posts

9 February 2011 at 12:13pm

UserForms requires the $UserDefinedForm variable in the Content area of the form page, so that seems to be an example of it working somehow. If I were you, I'd start by looking under the hood of the UserForms module.

Avatar
swaiba

Forum Moderator, 1798 Posts

9 February 2011 at 9:46pm

I'd disagree Ryan - I've looked at that before and it does a straight str_ireplace for the "$UserDefinedForm" with the userform - not very helpful to access the full power of the standard page controls for merging things like $Now

Avatar
theoldlr

Community Member, 103 Posts

11 February 2011 at 11:47am

swaiba, can you help me understand your example a little better? I'd like to use this idea for something a little different. Let's say, for example, I have a sub template file already made to display what I want. which would make your example more like this:

function MyContent() {
$vd = new ViewableData();
$t = SSViewer::getTemplateFile('mysite/include/SubTemplate.ss');
$ad = new ArrayData(array('CurrentPage' => $this));
return $vd->customise($ad)->renderWith($t);
}

What would you put into the WYSIWYG to make it render this? "$MyContent"? Wouldnt that just display "$MyContent" with the rest of what is in your HTMLEditorField rather than call the function?

Thanks!

Avatar
swaiba

Forum Moderator, 1798 Posts

11 February 2011 at 10:48pm

sure...

you see I think you could get away with...
the function would go in your Page_Controller...

function MyContent() {
   $t = SSViewer::fromString($this->Content);
   return $this->customise()->renderWith($t);
}

and the $MyContent would go into templates/Pages.ss replacing $Content - this mean where the template would get the "Content" and put it ont he page it calls the MyContent function and this in turn takes the "Content" and treats it as a template. because we are in "Page" context it should merge.

My previous example did a little more to create it's own merging/rendering process, wrap the "Content" in a control and then pass the page data in... I did this becasue it seemed wrong to render within itself...

function MyContent() {
$vd = new ViewableData();
$t = SSViewer::fromString('<% control CurrentPage %>'.$this->Content.'<% end_control %>');
$ad = new ArrayData(array('CurrentPage' => $this));
return $vd->customise($ad)->renderWith($t);
}

Go to Top