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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Display $Pos as two figures e.g.. 01, 02, 03 etc?


Go to End


9 Posts   2949 Views

Avatar
neilcreagh

Community Member, 136 Posts

21 March 2012 at 3:52am

Quick question: how could I get $Pos to display on my template as two figures eg. "01, 02, 03" instead of "1, 2, 3"?
Thanks!

Avatar
(deleted)

Community Member, 473 Posts

21 March 2012 at 8:23am

You'll need to define a custom method for this. Something like:

public function PaddedPos($startIndex = 1) {
	return sprintf('%02d', $this->Pos($startIndex));
}

%02d will pad an integer to two characters, if it is less than that, using zeros.

Avatar
neilcreagh

Community Member, 136 Posts

21 March 2012 at 11:05pm

Edited: 21/03/2012 11:05pm

Thanks for the reply Simon, however I think this is a bit more tricky because I'm already calling a function on my page.php and it's the $Pos of that function that I need to pad out with leading zeros... and I just can't get it to work.

function ArticleCategories() {
return DataObject::get("ArticleCategory", "ParentID=16", "", "", "");
}

Is there any way of doing this on the template eg. something like $Pos.Nice ?
Or, on the template is it possible to write in the leading zero with something like <% if Pos<10 %>0<% end_if %> (this doesn't work though)

Otherwise I might just resort to hard coding it on the template eg. <% if Pos=1 %>01<% else if Pos=2 %>02... (horribly long winded code)

Avatar
(deleted)

Community Member, 473 Posts

22 March 2012 at 10:40am

You'll either need to add the method to ArticleCategory or one of its parent classes (not the controller, though).

There's nothing like $Pos.Nice and you can't use inequalities in the template, just equalities.

Avatar
neilcreagh

Community Member, 136 Posts

27 March 2012 at 4:51am

Sorry, almost forgot to say thanks for the help Simon!

Avatar
RichMcNabb

Community Member, 34 Posts

15 May 2012 at 9:25am

I'm having trouble getting this to work - any chance you could post your code? Thanks :)

Avatar
akbortoli

Community Member, 2 Posts

27 January 2015 at 8:53am

Hello, does anyone have a solution for this? I'm struggling to get this to work.

Avatar
neilcreagh

Community Member, 136 Posts

27 January 2015 at 10:46pm

Sorry I can't help - I couldn't figure out how to successfully add the method Simon used above to my ArticleCategory function and I had already wasted too much time on it so I resorted to just hand coding my templates with eg. <% if Pos=1 %>01<% else if Pos=2 %>02 ...etc.

Go to Top