Creating Page Components
Page components (blocks) usually have two main parts:
- The component class, which defines what settings the component has and renders it
- View files, which are used to display the component's output
Creating Components
Textstem provides an Artisan command to generate new components. Run the following command to make a new Card component:
php artisan textstem:make:pagecomponent Card
This generates a component class:
app/Wrangler/Components/Card.php
and copies the package's sample views into resources/views/wrangler/components/card/ (default.blade.php, preview.blade.php and default.json). Use --template= to start from a different folder of the package's component views.
Components in app/Wrangler/Components/ are found automatically: they appear in the page builder's block picker, and the AI assistant can use them, without any registration. A component type is the class name in kebab case (card); a host-app class wins over a package class of the same name.
The generated class looks like this:
<?php
namespace App\Wrangler\Components;
use Illuminate\Http\Request;
use Medialight\Textstem\Models\WranglerPage;
use Medialight\Textstem\Wrangler\ActiveComponent;
class Card extends ActiveComponent
{
public static string $description = 'A short summary of what this component does.';
public static string $version = '1.0.0';
public $config;
public array $editSetup = [
[
'name' => 'template',
'controltype' => 'select-template',
'value' => 'default',
],
[
'name' => 'title',
'controltype' => 'text',
'value' => '',
'label' => 'Title',
],
];
public function render(Request $request, WranglerPage $wranglerPage)
{
$view = $this->getView();
$data = $this->data();
// add any extra stuff the view needs to $data
return view($view, $data)->render();
}
public function preview()
{
$view = $this->getPreview();
$data = $this->data();
return view($view, $data)->render();
}
}
All components implement the Medialight\Textstem\Contracts\WranglerComponent contract. Extending ActiveComponent is the simplest way to do this.
Class properties
| Property | Use |
|---|---|
$description |
Shown in the block picker, and to the AI assistant |
$label |
The name editors see. When empty, it is made from the class name |
$version |
Informational |
$preview_with |
What the page builder's list view shows for each block (see Previewing) |
$editSetup |
The block's settings form (see below) |
A static isAvailable() method that returns false hides the component from the block picker, for example when a feature it needs is turned off.
Editor Setup
$editSetup is an array of fields that define the component's settings and build its form in the page builder. Each field has a control type, a label, a default value and so on. Some control types need more parameters; a select, for example, needs options.
The following properties define a field:
name- passed to templates as a variable name, so it should be a single word with no spaces, starting with a letter or underscore. If it is missing, the array key is usedlabel- shown to editors. If it is missing, one is made from the namevalue- the default valuecontroltype- the input editors use (text,selectand so on)instructions- help text shown under the fieldoptions- choices forselectandradiostackcontrolssize- the column width of the input group (1 to 4)
See Editor Setup for the control types.
Rendering Components
The render and preview methods return HTML. render is for the public page; preview is for admin views such as the page builder.
getView() returns the dot-path of the Blade view to use: the view named by the block's template setting if there is one, otherwise default, all within resources/views/wrangler/components/{type}/. If none exists, the package's "missing" view is used. getPreview() uses preview.blade.php when the folder has one, and getView() otherwise.
data() collects the data for the view. It merges the block's settings with the scaffolding the renderer adds, so each field is a plain variable in the view ($title), and also available as $data['title'].
The base class's render() already does all of this, and shows an error block instead of failing the whole page when the view throws, so you only need to override it to add extra data. The base preview() returns nothing, so the generated class overrides it.
Note - the preview method has no access to the request or the current page. Some components are dynamic and need the request or page data to work. In these cases preview() should return a placeholder. The Card component doesn't need either, so it returns the same view as render().
The view for the Card might look like this:
<div class="overflow-hidden bg-white shadow sm:rounded-lg my-4">
<div class="px-4 py-5 sm:p-6">
@if ($title)
<h4 class="h4 mt-0">{{ $title }}</h4>
@endif
{!! $content ?? '' !!}
</div>
</div>
The styles and structure of the output are usually written for each site or theme (the example uses Tailwind CSS).
More views
Any other .blade.php file in the component's folder becomes an option of its select-template field, so adding card/wide.blade.php lets editors choose "wide". Files named preview or starting with _ are left out.
An icon.svg in the folder is used as the component's icon in the block picker and the block editor.
React rendering
With React rendering on, public pages call build() instead of render(). build() returns data rather than HTML; by default it returns data().
Previewing
In the page builder's list view, each block shows a short "list preview". By default this is the component's name, its chosen template (unless it is default) and the block's title. To show something more useful, set a static $preview_with property:
public static string $preview_with = '{{genre}} / {{style}}';
The names in double braces are replaced with the block's current values.
Blocks can also be previewed in the site's own look, in iframes. See Component Previews.
See also: Page Components, in the User Guide, for how editors use components in the page builder.