Jump to:

3445 Posts in 1031 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » function in other <% control %>

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

Page: 1
Go to End
Author Topic: 1514 Views
  • snaip
    Community Member
    180 Posts

    function in other <% control %> Link to this post


    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

    Re: function in other <% control %> Link to this post

    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.

  • robinp
    Community Member
    33 Posts

    Re: function in other <% control %> Link to this post


    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



Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.