I've wanted to do this lots of times but haven't figured out a way to do it. Suppose you have a small form that you want to add in a bunch of places around the site or a variable you can establish somewhere then want to use around the site. The only was I can see to call these is through the ss templates which works great but I want to be able to add them into the cms editor whenever I want as I would then have way more control over what was being put on the page. Someone else must have wanted this or possibly found a way to do it?
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.
Well, there is this example (could work for any form I suppose):
http://doc.silverstripe.org/recipes:customising-content-in-your-templates?s=paypal
(don't know how long it will remain there, since the documentation is in the process of being renewed...)
thanks for the quick reply
weird. I looked at that example on saturday morning at home and it seemed perfect but then I looked again this morning to apply it in my project and the documentation was gone. strange timing. I looked around the forums and pieced some things together. I got it working to replace a variable but coudn't get it to work with a form. Other people of the forums mentioned this is not how MVC works but I'm not trying to edit the code on the View mearly show the end results there.
I think the problem with the form is that it is dynamic and works at a level below the templates. So the form has to be constructed and made into an html element first. It has be pointed out many times that you can put it in the the .ss file easily but then you are forced to always have the form in the same place. Anyway would be nice to do so if anyone else has any tips would be much appreciated.
function Content() {
$replace = "";
$replace .= str_replace('$ProjectPlannerContactForm', $this->ProjectPlannerContactForm(), $this->Content);
//$replace .= str_replace('$Pictures', $this->Pictures(), $this->Content);
return $replace;
}
public function ProjectPlannerContactForm() {
return new ProjectPlannerContactForm($this,'ProjectPlannerContactForm');
// return "alpha omega";
}
Thanks
Will
You should use shortcodes for doing this sort of thing in 2.4 +
An example is available on ssbits - http://www.ssbits.com/tutorials/2010/2-4-using-short-codes-to-embed-a-youtube-video/.
thanks for the help but couldn't get this to work. I tried contactForm extends Form then pulling in that
public function ProjectPlannerContactForm() {
return new ProjectPlannerContactForm($this,'ProjectPlannerContactForm');
}
but getting the problem it doens't recognize $this
[Notice] Undefined variable: this
I then tried to build the form straight in the controller which should be really simple but that couldn't recognize $this either. Surely $this is just the parent controller so should be easy to extract? I've been trying different routes at this for a few hours and haven't been able to get any further so thought i'd post again.
Thanks
Will
If $this is undefined then it's likely that you're calling that function statically which means there is no instance of '$this'.
isn't that the problem though. because it is being called from the shortcodes engine it is static and there is no access from that level to the page controller, thus eliminating the chance of running a form. I may well be wrong but thats what I'm seeing at the moment, once you are at the view level you can't call back to the model?