Jump to:

3435 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » problem with execute function in tempalte

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

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

    problem with execute function in tempalte Link to this post

    hi

    i have a function:

       function gwiazdki() {
          $szare = 5 - $this->Trudnosc;
          for($i=1; $i<=$this->Trudnosc; $i++) {
           echo "<img src='../../themes/tutorial/images/gwiazdka_zolta.jpg' width='14' height='13' alt='trudnosc' class='trudnosc' />";
           }
          for($i=1; $i<=$szare; $i++) {
           echo "<img src='../../themes/tutorial/images/gwiazdka_szara.jpg' width='14' height='13' alt='trudnosc' class='trudnosc' />";
           }       
       }

    $Trudnosc is a number: 0,1,2,3,4 or 5

    and in template i put
    $gwiazdki

    but function runs on top of the site before <html> tag, why ?

    <img src='../../themes/tutorial/images/gwiazdka_zolta.jpg' width='14' height='13' alt='trudnosc' class='trudnosc' /><img src='../../themes/tutorial/images/gwiazdka_zolta.jpg' width='14' height='13' alt='trudnosc' class='trudnosc' /><img src='../../themes/tutorial/images/gwiazdka_szara.jpg' width='14' height='13' alt='trudnosc' class='trudnosc' /><img src='../../themes/tutorial/images/gwiazdka_szara.jpg' width='14' height='13' alt='trudnosc' class='trudnosc' /><img src='../../themes/tutorial/images/gwiazdka_szara.jpg' width='14' height='13' alt='trudnosc' class='trudnosc' /><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" >
    <head>

  • joshy
    Avatar
    Community Member
    57 Posts

    Re: problem with execute function in tempalte Link to this post

    Rather than echoing it, you need to build it into an ArrayData object and return it. You can then loop through it in the template using <% control gwiazdki %>.

    Google for how to build into ArrayData and if you get stuck I'll give you more of a hand

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: problem with execute function in tempalte Link to this post

    i tried like here http://silverstripe.org/all-other-modules/show/261496#post261496

       function gwiazdki() {
          $szare = 5 - $this->Trudnosc;
          $data = new ArrayData();

          for($i=1; $i<=$this->Trudnosc; $i++) {
              $data->push(new ArrayData(array("zolta" => "<img src='../../themes/tutorial/images/gwiazdka_zolta.jpg' width='14' height='13' alt='trudnosc' class='trudnosc' />")));
          }
          return $data;       
       }

    but i get an error
    [Warning] Missing argument 1 for ArrayData::__construct()

    so i tried this:

       function gwiazdki() {
          $szare = 5 - $this->Trudnosc;

          for($i=1; $i<=$this->Trudnosc;; $i++) {
          $a = array("zolta" => "<img src='../../themes/tutorial/images/gwiazdka_zolta.jpg' width='14' height='13' alt='trudnosc' class='trudnosc' />");
          }
          return new ArrayData($a);       
       }

    but it returns only one image

    EDIT:
    need to change
    $data = new ArrayData(); to
    $data = new DataObjectSet();

    works

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: problem with execute function in tempalte Link to this post

    Yeah, $a will be overwritten for each iteration. You were on the right track with the first one, but multiple ArrayData items should be loaded into a DataObjectSet. Try:

    function gwiazdki() {
       $szare = 5 - $this->Trudnosc;
       $data = new DataObjectSet();

       for($i=1; $i<=$this->Trudnosc; $i++) {
          $data->push(new ArrayData(array("zolta" => "<img src='../../themes/tutorial/images/gwiazdka_zolta.jpg' width='14' height='13' alt='trudnosc' class='trudnosc' />")));
       }
       return $data;
    }

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: problem with execute function in tempalte Link to this post

    yea it works now

    1153 Views
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.