Elements

Since different components of the view layer are often reused, li3 includes the common functionality of wrapping view templates inside of layouts and including small, re-usable view components called elements inside of views. You can also think of an element like a widget or other portion of the displayed content that would be reused across multiple pages.

Examples of Elements

  • site navigation
  • single post items
  • widgets i.e. upcoming events, blogroll.

Unless otherwise configured, elements are defined in app/views/elements. Inside this folder, the files you define will be tailored to display the output for your chosen element. As an example, the code below defines a product element. The file name is app/views/elements/product.html.php.

  1. <article class="product">
  2. <h1><?= $item->title ?></h1>
  3. <img src="<?= $item->cover ?>" />
  4. <div class="description">
  5. <?php echo $item->description ?>
  6. </div>
  7. <div class="price">
  8. $<?= $item->price_usd ?>
  9. </div>
  10. </article>

Displaying an Element

Displaying an element is accomplished via the View::render() method. In the following example we render a list of products using the product element we defined above.

  1. <div class="products">
  2. <?php foreach ($products as $item): ?>
  3. <?php echo $this->_view->render(
  4. ['element' => 'product'], compact('item')
  5. ) ?>
  6. <?php endforeach ?>
  7. </div>

A complete description of the render() method can be found in the li3 API documentation under View::render()