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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

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

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

$Pos in image url


Go to End


6 Posts   3782 Views

Avatar
darkhouse

Community Member, 12 Posts

9 July 2008 at 2:59pm

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!

Avatar
Willr

Forum Moderator, 5523 Posts

9 July 2008 at 4:47pm

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

Avatar
darkhouse

Community Member, 12 Posts

9 July 2008 at 11:47pm

That worked perfect! Thanks so much.

Avatar
darkhouse

Community Member, 12 Posts

10 July 2008 at 12:21am

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

Avatar
Sam

Administrator, 690 Posts

14 July 2008 at 5:07pm

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

Avatar
darkhouse

Community Member, 12 Posts

16 July 2008 at 3:28pm

This worked perfectly! Thanks so much.