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.

Template Questions

Passing multiple variables to a function via control?


8 Posts   2940 Views


5 October 2009 at 10:36am (Last edited: 5 October 2009 1:55pm), Community Member, 130 Posts

Hi there, I'm quite new to Silverstripe and really at the initial stage of poking around and getting a feel of how Sapphire hangs together.

So I've done a bit of the tutorials and started to play around in a development environment. I set myself the task of making a flexible RSS feed importer I can call from templates. I came across a nice bit of code incorporating SimplePie here: (thanks Mo) and have altered it slightly:

function RSSFeedIn($feed_url = '', $num_items = 10) {
$output = new DataObjectSet();

include_once(Director::getAbsFile(SAPPHIRE_DIR . '/thirdparty/simplepie/SimplePie.php'));

$t1 = microtime(true);
$feed = new SimplePie($feed_url, TEMP_FOLDER);
if($items = $feed->get_items(0, $num_items)) {
foreach($items as $item) {

// Cast the Date
$date = new Date('Date');

// Cast the Title
$title = new Text('Title');

// Cast the description and strip
$desc = new Text('Description');

$output->push(new ArrayData(array(
'Title' => $title,
'Date' => $date,
'Link' => $item->get_link(),
'Description' => $desc
return $output;

I can call this via the template like so:

   <h3>Upcoming Events</h3>
   <% control RSSFeedIn %>
         <a href="$Link">$Date - $Title</a>
   <% end_control %>

And it works but I expected to be able to call it like this:

<% control RSSFeedIn('',10) %>

Which doesn't work.

I've spent the past half hour searching the Silverstripe Forums/ documentation but haven't found anything which clearly explains what I need to do to make this work so I thought I'd put it out there.

Thanks in advance



13 October 2009 at 2:48am Community Member, 501 Posts

Have you tried without the quotes around the url?


13 October 2009 at 10:30am Community Member, 130 Posts

Hi Dio5, thanks for your reply. Unfortunately double quotes doesn't work either. From my understanding it's because of what is written here: - under "Template Syntax / Variables":


These variables will call a method/field on the object and insert the returned value as a string into the template.

* $Property will call $obj→Property() (or the field $obj→Property)
* $Property(param) will call $obj→Property(”param”)
* $Property.SubProperty will call $obj→Property()→SubProperty() (or field equivalents)

If a Variable returns a string, that string will be inserted into the template. If the variable returns an object, then the system will attempt to render the object through its forTemplate() method. If the forTemplate() method has not been defined, the system will return an error.

Note you also cannot past a variable into a variable $Property($Value) will not work.

Which means taking a more OOP approach to the original function I guess.


13 October 2009 at 11:23am Community Member, 501 Posts


I didn't mean double quotes, I meant no quotes at all.


13 October 2009 at 11:42am Community Member, 130 Posts

Whoops, sorry! It doesn't work without quotes either - I tried it with different combinations of quotes etc. before coming across the documentation.

I've spent far too long doing procedural PHP stuff so I'm just refreshing my brain with the object orientated approach as I believe (from the Templates documentation) my main problem is I'm trying to use procedural functions rather than OO methods - if that makes any sense.


13 October 2009 at 7:55pm Community Member, 501 Posts

Sure it makes sense.

But it's odd that this should not work, I remember well doing more or less the same thing (passing multiple variables in a template function), but that wasn't in a control though and not with urls containing slashes etc so maybe that is the problem.

If you look at the regex for controls ([A-Za-z0-9_-]+) used in SSViewer.php, this might explain it.

(Actually not sure what this has to do with OO or not, you are calling a method in the controller which seems like a normal thing to do to me :)).


14 October 2009 at 8:16am Community Member, 130 Posts

Hi Dio - you're bang on. It's fine if I put a normal string in:

   <% control RSSFeedIn(jobbies,10) %>
         <a href="$Link">$Date - $Title</a>
   <% end_control %>

So it must be in the regular expression in SSViewer. Will have a hack and see what I can come up with.


14 October 2009 at 10:23am Community Member, 130 Posts

OK, I admit it - after briefly scratching my head over the multiple reg_exps in SSViewer I decided to not mess with the core code and use a workaround... I'm converting my URLs to hex using;


which outputs something like this: -> 687474703a2f2f7777772e6576656e7466696e6465722e636f2e6e7a2f666565642f6576656e74732f6e656c736f6e2d7461736d616e2f77686174736f6e2f7570636f6d696e672e727373

Which can be easily used in the template without confusing SSViewer. I then convert the hex back to the URL using this line in the method:

$feed_url = pack('H*', $feed_url);

Yes it's ugly but it means I can specific the URL in the template... sorta...