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.

Template Questions /

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

function in other <% control %>


Go to End


3 Posts   2115 Views

Avatar
snaip

Community Member, 181 Posts

24 July 2009 at 12:14am

hi

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">
   	 $titlewrap($Title);
    </a></h2>
    <a href="$Link" title="Go to the $Title.XML page">
    <% control Zdjecie_glowne %>
    	<img class="" src="$URL" alt="" width="140" height="90" />
    <% end_control %>    	
    </a>
</div>
<% end_control %> 

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

Avatar
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.

Avatar
robinp

Community Member, 33 Posts

1 August 2009 at 12:00am

Hi

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

Cheers

Robin