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.


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

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

$Pos in image url


6 Posts   2442 Views


9 July 2008 at 2:59pm Community Member, 12 Posts

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!


9 July 2008 at 4:47pm Forum Moderator, 5511 Posts

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


Try that and see if it works


9 July 2008 at 11:47pm Community Member, 12 Posts

That worked perfect! Thanks so much.


10 July 2008 at 12:21am Community Member, 12 Posts

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.



14 July 2008 at 5:07pm Administrator, 685 Posts

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


16 July 2008 at 3:28pm Community Member, 12 Posts

This worked perfectly! Thanks so much.