Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » $Pos in image url

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 2408 Views
  • darkhouse
    Avatar
    Community Member
    12 Posts

    $Pos in image url Link to this post

    Hello, I'm pretty new to SS, but I'm loving it.

    I'm trying to loop through the children of a page but I want to display some sequential images, like

    <img src="number$Pos.jpg" />

    but that doesn't work. It ends up just doing

    <img src="number.jpg" />

    Any ideas? Thanks!

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: $Pos in image url Link to this post

    because number$Pos is 1 string you might need to try and wrap the $Pos in a {} so it would be

    number{$Pos}

    Try that and see if it works

  • darkhouse
    Avatar
    Community Member
    12 Posts

    Re: $Pos in image url Link to this post

    That worked perfect! Thanks so much.

  • darkhouse
    Avatar
    Community Member
    12 Posts

    Re: $Pos in image url Link to this post

    One more thing. I want to only get the first 4 children, how can I do that? I tried this:

    <% control ChildrenOf(page-url) %>
    <% if $Pos < 5 %>
    ....
    <% end_if %>
    <% end_control %>

    but that didn't work. Just got a blank screen, which probably means there was a php error but they're turned off.

    Thanks

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: $Pos in image url Link to this post

    Yeah the template language was kept simple so that logic would be necessarily kept out of the templates.

    The best way to solve this is to create another small data handler method on your controller:

    function MyList() {
    $parent = DataObject::get_one('SiteTree', "URLSegment = 'my-page'");
    return DataObject::get("SiteTree", "ParentID = $parent->ID", "", "", "4");
    }

    Then you can just use <% control MyList %> in your template.

    The advantage of this approach is that you can focus on layout in your template file, and the exact details by which data is selected in your MyList() function. If you wanted to give template designers the flexibility to decide how many pages, to show, you could have an argument to the MyList function

    function MyList($limit = 4) {
    $parent = DataObject::get_one('SiteTree', "URLSegment = 'my-page'");
    return DataObject::get("SiteTree", "ParentID = $parent->ID", "", "", $limit);
    }

    Then you can just use <% control MyList(4) %> in your template. Since we've set a default value for the $limit argument, you could also use <% control MyList %>.

  • darkhouse
    Avatar
    Community Member
    12 Posts

    Re: $Pos in image url Link to this post

    This worked perfectly! Thanks so much.

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