Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Passing multiple variables to a function via control?

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

Page: 1
Go to End
Author Topic: 2848 Views
  • Tama
    Avatar
    Community Member
    119 Posts

    Passing multiple variables to a function via control? Link to this post

    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: http://www.silverstripe.org/general-questions/show/269733?start=0#post269972 (thanks Mo) and have altered it slightly:

    function RSSFeedIn($feed_url = 'http://www.eventfinder.co.nz/feed/events/nelson-tasman/whatson/upcoming.rss', $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);
    $feed->init();
    if($items = $feed->get_items(0, $num_items)) {
    foreach($items as $item) {

    // Cast the Date
    $date = new Date('Date');
    $date->setValue($item->get_date());

    // Cast the Title
    $title = new Text('Title');
    $title->setValue($item->get_title());

    // Cast the description and strip
    $desc = new Text('Description');
    $desc->setValue(strip_tags($item->get_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>
       <ul>
       <% control RSSFeedIn %>
          <li>
             <a href="$Link">$Date - $Title</a>
          </li>
       <% end_control %>
       </ul>

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

    <% control RSSFeedIn('http://www.eventfinder.co.nz/feed/events/nelson-tasman/whatson/upcoming.rss',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

    Tama

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Passing multiple variables to a function via control? Link to this post

    Have you tried without the quotes around the url?

  • Tama
    Avatar
    Community Member
    119 Posts

    Re: Passing multiple variables to a function via control? Link to this post

    Hi Dio5, thanks for your reply. Unfortunately double quotes doesn't work either. From my understanding it's because of what is written here: http://doc.silverstripe.org/doku.php?id=templates - under "Template Syntax / Variables":

    $Property
    $Property(param)
    $Property.SubProperty

    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.

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Passing multiple variables to a function via control? Link to this post

    Hi,

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

  • Tama
    Avatar
    Community Member
    119 Posts

    Re: Passing multiple variables to a function via control? Link to this post

    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.

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Passing multiple variables to a function via control? Link to this post

    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 ).

  • Tama
    Avatar
    Community Member
    119 Posts

    Re: Passing multiple variables to a function via control? Link to this post

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

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


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

  • Tama
    Avatar
    Community Member
    119 Posts

    Re: Passing multiple variables to a function via control? Link to this post

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

    ...so I'm converting my URLs to hex using;

    bin2hex($url)


    which outputs something like this:

    http://www.eventfinder.co.nz/feed/events/nelson-tasman/whatson/upcoming.rss -> 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...

    2848 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.