Render roman numbers


I would like to render a numbered list of pages, but with roman numbers.
To get the numbers, I put

<?= $page->children()->listed()->num() ?>

and I get
1, 2, 3, 4…

is there a simple way to convert the numbers into roman numbers, like

I, II, III, IV… ?

Thank you!

You can convert easily via CSS: list-style-type: upper-roman

You probably mean nums() not num()? But that only gives you an array of numbers, not a list. What exactly do you want to achieve here?

What @ahmetbora suggested, only works for an ordered list (<ol>), so you would have to loop through your pages collection and generate such a list.

I made a link list like this:

  <?php foreach($page->children()->listed() as $child) : ?>
    <a href= "<?= $child->url() ?>">
      <span class="number"><?= $child->num() ?></span> 
      <?= $child->title() ?>
  <?php endforeach ?>

and I want a link list that looks like

I Link 1
II Link 2
III Link 3
IV Link 4

The CSS trick of @ahmetbora sadly didn’t work.

As @pixelijn mention, you should use <ol> instead of <ul>, should be work.

And then also remove this manual number

OK I put an <ol> tag instead of an <ul> and I got my roman numbers.
But there is still that ugly period after the number. Is there a way to get rid of that?

And since the numbers are outside of the list, they’re not part of the link anymore. The only way to avoid that is to wrap the <a> tag around the <li> object.

That would result in invalid HTML, though. See “Permitted content” on this page: <ol>: The Ordered List element - HTML: HyperText Markup Language | MDN

If you absolutely must have the number inside your link (why?), then back to your original idea with the span and use a function like this to convert:

Also maybe you can use unicode characters as standalone:

Thanks for your help, the script did the job!