Skip to content

Registering And Using Components

Shea Lewis edited this page Jun 24, 2015 · 1 revision

Every theme can have their own set of theme components. Below you will find instructions on both creating and utilizing components from within your view files.

Registering a Component

To register components for your theme, you'll need to create a components.php file at the root of your theme's directory. This file will be autoloaded once a theme is set as the active theme.

Creating a component is easy through the use of the Component::register() method; simply return your desired output through a callback closure:

\Component::register('pageheader', function($title) {
    return "<h1 class=\"page-header\">{$title}</h1>";
});

You may also return a view partial:

\Component::register('modal', function($title, $content) {
    return \Theme::view('partials.components.modal', compact('title', 'content'));
});

You may register as many components within this file as you need or desire.

Note: While theme components are extremely powerful, keep in mind that they are meant more for creating simple and reusable UI elements. So don't get too carried away with too much logic!

Usage

Once you've registered your components, usage from within your view files is extremely simple:

Blade:

@component_pageheader('My Awesome Page Header')

Twig:

{{ component_pageheader('My Awesome Page Header') }}

Any parameters you define to be utilized by your component may be passed through; this includes all data types (strings, integers, booleans, arrays, etc.)

Clone this wiki locally