How to put text right next to a SilverStripe template placeholder
Posted March 6th, 2010 in SilverStripe
I needed to put some text directly next to a placeholder in a SilverStripe template and couldn't find the answer in the online documentation or the SilverStripe book, but perhaps I missed it. In any case, I worked it out from a user comment so show here how to do it.
SilverStripe Template Placeholders
SilverStripe template placholders take the form $Placeholder. There are a number of pre-defined placeholder variables and you can define your own by creating a method in your appropriate page controller class.
Adding text directly next to a placeholder
The reason I needed to do this was that I wanted to initialise a Javascript function at the end of the page's content based on the class name of the page using the format ClassNameInit(), but doing $ClassNameInit() would be looking for a function named ClassNameInit instead of what I was trying to achive.
The way to achieve this is with curlies like so:
{$ClassName}Init
If the class name was HomePage, then this would end up looking like this in the HTML source:
HomePageInit
The full chunk of Javascript with correct escaping looks like this in the template:
<script type="text/javascript">
if(window.{$ClassName}Init) {
{$ClassName}Init();
}
</script>
and this in how it appears in the rendered page, when the class name is HomePage:
<script type="text/javascript">
if(window.HomeNameInit) {
HomePageInit();
}
</script>
As a final note, because I couldn't work out how to do this at the time I needed it, I ended up naming the Javascript functions Init$ClassName instead.
Related posts:
- How to check if a Javascript function exists (Saturday, July 5th 2008)

Comments
blog comments powered by Disqus