Jump to:

23491 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » can i execute function with variable in template ?

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

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

    can i execute function with variable in template ? Link to this post

    hi

    is there any way to do something like that:

    class Page_Controller extends ContentController {

    function abc($x) {
    $doSet = DataObject::get(
    $obj = '$x',
    $filter = "",
    $sort = "RAND()",
    $join = "",
    $limit = $random
    );
    return $doSet;
    }

    }

    class PageOne_Controller extends Page_Controller {

    }

    class PageTwo_Controller extends Page_Controller {

    }

    ang now in template PageOne.ss return 'abc' function with 'x' like:

    $abc(PageOne);

    and in PageTwo.ss

    $abc(PageTwo);

    i had some problem with execute function in these way in templates so i ask you

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: can i execute function with variable in template ? Link to this post

    That should work - is there any error produced?

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: can i execute function with variable in template ? Link to this post

    You have a line $limit = $random and $random is not defined anywhere else. That should be changed eg $limit = "10".

    You can also not pass variables into functions so if you are trying to do something like $abc($ClassName) this won't work.

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: can i execute function with variable in template ? Link to this post

    Sean it doesnt work

    this is my code

    class OkolicaMapaPage_Controller extends StronaPage_Controller {

       function zajawka($zajawka) {
          $x = str_replace("\n","<br />",$zajawka);
          return $x;
       }

    }

    $zajawka('$Zajawka'); making error:

    Parse error: syntax error, unexpected T_STRING, expecting ')' in C:\Windows\Temp\silverstripe-cacheC--serwer-strony-nd\.cacheC..serwer.strony.nd.themes.noclegi-dobczyce.templates.Layout.OkolicaMapaPage.ss on line 31

    <script type='text/javascript'>
    var mapa;
    var geo;
    function mapaStart() {

       if(GBrowserIsCompatible()) {
          mapa = new GMap2(document.getElementById("mapa"));
          mapa.setUIToDefault();   
          mapa.enableScrollWheelZoom();
          mapa.setCenter(new GLatLng(49.97065584042108,19.90997314453125),9);

          geo = new GClientGeocoder();
             
          <% control OkolicaPage %>
             var adres = '$Title';
             $zajawka($Zajawka);
             //var zajawka = '$Zajawka;

             geo.getLatLng(adres,function(punkt) {
                var marker = new GMarker(punkt,{title: adres});
                 //marker.opis = adres;
                 mapa.addOverlay(marker);       
                       

                GEvent.addListener(marker,"click",function() {
                //marker.openInfoWindowHtml(marker.opis);
                 });
                 
                 return marker;
             }); <<<<<<<<<<<<<< THIS IS LINE 31 <<<<<<<<<<<<<<<

          <% end_control %>            
       }
    }

    </script>

    <h2>
    <% control OkolicaHolder_tytul %>
       $Title:
    <% end_control %>
    </h2>

    <ul class="reg_menu">
       <li class="jasna">
          <% control OkolicaMapaPage_tytul %>
             <a href="$Link" title="$Title">Mapa</a>
          <% end_control %>
       </li>
       <li class="ciemna">
          <% control OkolicaHolder_tytul %>
             <a href="$Link" title="$Title">Lista miejscowoÅ›ci</a>
          <% end_control %>   
       </li>
    </ul>

    <div id="box_jasny">
       <div class="btop"></div>
       <div class="bcont">
          <div id="mapa" class="okolica"></div>
       </div>
       <div class="bbot"></div>
    </div>
    <div class="clear"></div>

  • Nivanka
    Avatar
    Community Member
    395 Posts

    Re: can i execute function with variable in template ? Link to this post

    try without using quotes

    example :

    $zajawka(Zajawka);

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: can i execute function with variable in template ? Link to this post

    no it is not this

    function replace_str($y) {
       $x = str_replace("e","eee",$y);
       echo $x;
    }

    $replace_str(aea); => aeeea

    $text = "aea";
    $replace_str($text) => ERROR
    $replace_str(text) => teeext

    but i must use this function in this way $replace_str($text)

  • Nivanka
    Avatar
    Community Member
    395 Posts

    Re: can i execute function with variable in template ? Link to this post

    are you using this to do some sting replace of a field of the dataobject?

    if that case you can have a code like this on your controller;

    function replace_str() {
    $x = str_replace("e","eee",$this->y);
    echo $x;
    }

    Make sure $this->y is the field;

    then call it from the template like $replace_str

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: can i execute function with variable in template ? Link to this post

    ok it works now but i cant use it in other <% control %>

    OkolicaMapaPage.ss

    <% control OkolicaPage %>
       $replace_str;
    <% end_control %>

    class OkolicaMapaPage_Controller extends Page_Controller {

       function replace_str() {
          $x = str_replace("e","eeee",$this->Title);
          echo $x;
       }

    }

    class Page_Controller extends ContentController {

       function OkolicaPage() {
          $obiekty = DataObject::get(
             $callerClass = 'OkolicaPage',
             $filter = '',
             $sort = '',
             $join = '',
             $limit = ''
          );
          return $obiekty;
       }

    }

    2885 Views
Page: 1 2
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.