Generating an Ordered List of Items

Problem

You want an ordered list of items in your Blade template.

Solution

Use the HTML::ol() method.

You simply pass the method a list of items. If you pass an associative array, only the array values are used.

{{ HTML::ol(array('a', 'b', 'c')) }}

This produces a simple list.

<ol>
  <li>a</li>
  <li>b</li>
  <li>c</li>
</ol>

If any of the elements are arrays, then a sub-list is generated.

// PHP code to generate $list
$list = array(
  'one',
  'two',
  array(
    'sub-one',
    'sub-two',
  ),
);
return View::make('thebladeview', array('list' => $list));

If the Blade template has the following:

{{ HTML::ol($list) }}

Then the following would be output.

<ol>
  <li>one</li>
  <li>two</li>
  <li><ol>
    <li>sub-one</li>
    <li>sub-two</li>
  </ol></li>
</ol>

If you want the sub-list to have a title, then have the array representing the sub-list have a key.

// PHP code to generate $list
$list = array(
  'one',
  'two',
  'three' => array(
    'sub-one',
    'sub-two',
  ),
);
return View::make('thebladeview', array('list' => $list));

If the Blade template has the following:

{{ HTML::ol($list) }}

Then the following would be output.

<ol>
  <li>one</li>
  <li>two</li>
  <li>three
    <ol>
      <li>sub-one</li>
      <li>sub-two</li>
    </ol>
  </li>
</ol>

If you want attributes to apply to the list, use an array as the second argument to HTML::ol().

{{ HTML::ol(array('a', 'b'), array('class' => 'mylist')) }}

Now the list has a class attribute.

<ol class="mylist">
  <li>a</li>
  <li>b</li>
</ol>

Discussion

A couple of notes.

  1. Any HTML entities in the list values are escaped.
  2. The second argument, $attributes, will only apply to the top level list. If you have sublists, they won't contain any attributes.
comments powered by Disqus