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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Template Questions /

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

function in other <% control %>

Go to End

3 Posts   1824 Views


Community Member, 181 Posts

24 July 2009 at 12:14am


why i can't use function inside <% control %> ?

for example i have:

 function Wycieczki_sg() {

    $doSet = DataObject::get(
      $callerClass = "ItWycieczkaPage",
      $filter = "Widocznosc = 'Stale'",
      $sort = "",
      $join = "",
      $limit = "3" 
    return $doSet;      

   function titlewrap($str) {
      	$new_title = wordwrap($str, 23, "<br />\n");
      	return $new_title;  	

in template:

<% control Wycieczki_sg %>	 
<div class="tour" onclick="location.href='$Link'">	    	
    <h2><a href="$Link" title="Go to the $Title.XML page">
    <a href="$Link" title="Go to the $Title.XML page">
    <% control Zdjecie_glowne %>
    	<img class="" src="$URL" alt="" width="140" height="90" />
    <% end_control %>    	
<% end_control %> 

but $titlewrap($Title); doesn't work

Nathan Cox

Community Member, 99 Posts

31 July 2009 at 5:18pm

I'm seeing two issues here:
1) WHen you're inside the Wycieczki_sg control everything is interpreted in the context of that object (the "ItWycieczkaPage" in this case). So what it's trying to do is call the titlewrap() method of ItWycieczkaPage. To call a method on the "outer" page you need to use $Top.titlewrap()

2) As far as I know you can't pass a $Variable as a function argument from the templates, only strings. So $titlewrap($Title) isn't going to work.

I'm guessing what you need to do is put a method like this in ItWycieczkaPage:

function WrappedTitle() {
	return wordwrap($this->Title, 23, "<br />\n"); 

Disclaimer: I haven't actually tested this.


Community Member, 33 Posts

1 August 2009 at 12:00am


Nathon is right you cann't as far as I know you cann't pass variables in templates. To access titlewrap you actually use $Top.titlewrap