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.

General Questions /

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

can i execute function with variable in template ?


Go to End
Reply


10 Posts   3061 Views

Avatar
snaip

Community Member, 180 Posts

18 September 2009 at 1:50am

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

Avatar
Sean

Forum Moderator, 922 Posts

18 September 2009 at 3:27pm

That should work - is there any error produced?

Avatar
Willr

Forum Moderator, 5513 Posts

18 September 2009 at 4:02pm

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.

Avatar
snaip

Community Member, 180 Posts

3 October 2009 at 3:42am

Edited: 03/10/2009 3:43am

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>

Avatar
Nivanka

Community Member, 400 Posts

3 October 2009 at 4:31am

try without using quotes

example :

$zajawka(Zajawka);

Avatar
snaip

Community Member, 180 Posts

3 October 2009 at 11:35am

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)
:(

Avatar
Nivanka

Community Member, 400 Posts

3 October 2009 at 3:04pm

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

Avatar
snaip

Community Member, 180 Posts

3 October 2009 at 10:23pm

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;
   }

}

Go to Top