Jump to:

23459 Posts in 18906 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Display $Pos as two figures e.g.. 01, 02, 03 etc?

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

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

Page: 1
Go to End
Author Topic: 693 Views
  • neilcreagh
    Avatar
    Community Member
    94 Posts

    Display $Pos as two figures e.g.. 01, 02, 03 etc? Link to this post

    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!

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: Display $Pos as two figures e.g.. 01, 02, 03 etc? Link to this post

    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.

  • neilcreagh
    Avatar
    Community Member
    94 Posts

    Re: Display $Pos as two figures e.g.. 01, 02, 03 etc? Link to this post

    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)

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: Display $Pos as two figures e.g.. 01, 02, 03 etc? Link to this post

    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.

  • neilcreagh
    Avatar
    Community Member
    94 Posts

    Re: Display $Pos as two figures e.g.. 01, 02, 03 etc? Link to this post

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

  • RichMcNabb
    Avatar
    Community Member
    34 Posts

    Re: Display $Pos as two figures e.g.. 01, 02, 03 etc? Link to this post

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

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