Rewriting
Node Builders
Node builders are utility factories to help create new Blade constructs in your rewriters. Their main purpose is to reduce the amount of manual string concatenation you need to perform.
#Element Builder
Use the element builder to construct HTML elements with attributes, children, and various closing styles.
The following example creates a div with an id and a class:
<?php
use Forte\Rewriting\Builders\Builder;
echo Builder::element('div')
->id('myDiv')
->addClass('mt-4');
produces:
<div id="myDiv" class="mt-4"></div>
You may also specify that the element should be either self-closing or a void element:
<?php
use Forte\Rewriting\Builders\Builder;
// <myElement />
echo Builder::element('myElement')
->selfClosing();
// <myElement>
echo Builder::element('myElement')
->void();
#Attributes
Use attr to set a standard key-value attribute:
<?php
use Forte\Rewriting\Builders\Builder;
echo Builder::element('div')
->attr('data-role', 'container');
outputs:
<div data-role="container"></div>
For boolean attributes (no value), use boolAttr:
<?php
use Forte\Rewriting\Builders\Builder;
echo Builder::element('div')
->boolAttr('hidden');
renders as:
<div hidden></div>
Blade dynamic attributes (:name="expr" syntax) are created with bladeAttr:
<?php
use Forte\Rewriting\Builders\Builder;
echo Builder::element('div')
->bladeAttr('class', '$classes');
produces:
<div :class="$classes"></div>
When an element needs multiple attributes that share the same stripped name (e.g., both a static class and a bound :class), use appendAttr to add attributes without deduplication. Unlike attr, which overwrites any existing attribute with the same name, appendAttr always appends:
<?php
use Forte\Rewriting\Builders\Builder;
echo Builder::element('div')
->attr('class', 'container')
->appendAttr(':class', '$dynamicClasses');
produces:
<div class="container" :class="$dynamicClasses"></div>
Shorthand methods id and class set those attributes directly. Use addClass to append to existing classes rather than replacing them:
<?php
use Forte\Rewriting\Builders\Builder;
echo Builder::element('div')
->class('container mx-auto')
->addClass('p-4');
outputs:
<div class="container mx-auto p-4"></div>
#Populating Children
Use children to set the element's child nodes. You may pass any combination of builders and raw strings:
<?php
use Forte\Rewriting\Builders\Builder;
echo Builder::element('ul')
->children(
Builder::element('li')->text('First'),
Builder::element('li')->text('Second'),
);
renders as:
<ul><li>First</li><li>Second</li></ul>
For a single text child, use the text shorthand:
<?php
use Forte\Rewriting\Builders\Builder;
echo Builder::element('p')
->text('Hello, world!');
produces <p>Hello, world!</p>.
To add children after existing ones, use append:
<?php
use Forte\Rewriting\Builders\Builder;
echo Builder::element('div')
->text('Hello')
->append(Builder::element('span')->text(' world'));
outputs:
<div>Hello<span> world</span></div>
#Directive Builder
Use the directive builder to create Blade directive nodes like @include, @if, or @csrf:
<?php
use Forte\Rewriting\Builders\Builder;
// @include('partials.header')
echo Builder::directive('include', "('partials.header')");
// @if($show)
echo Builder::directive('if', '($show)');
// @csrf
echo Builder::directive('csrf');
These produce @include('partials.header'), @if($show), and @csrf respectively.
When inserting a directive adjacent to other content, safeDirective ensures proper spacing:
<?php
use Forte\Rewriting\Builders\Builder;
echo Builder::safeDirective('if', '($condition)');
To create a modified copy of an existing DirectiveNode:
<?php
use Forte\Rewriting\Builders\Builder;
$modified = Builder::directiveFrom($existingDirective, '($newArgs)');
#Text Builder
Create a plain text node with Builder::text:
<?php
use Forte\Rewriting\Builders\Builder;
echo Builder::text('Hello, world!');
This produces the literal text Hello, world!.
#Echo/Interpolation Builders
Blade interpolation nodes come in three variants (escaped, raw, and triple). Each has a dedicated builder:
<?php
use Forte\Rewriting\Builders\Builder;
// {{ $name }}
echo Builder::echo('$name');
// {!! $html !!}
echo Builder::rawEcho('$html');
// {{{ $value }}}
echo Builder::tripleEcho('$value');
#PHP Builders
Create raw PHP tag nodes for <?php ?> and <?= ?> syntax:
<?php
use Forte\Rewriting\Builders\Builder;
// <?php echo "hello"; ...
echo Builder::phpTag('echo "hello";');
// <?=$variable ...
echo Builder::phpEchoTag('$variable');
These produce <?php echo "hello"; ?> and <?=$variable ?> respectively.
Both methods accept an optional second argument to omit the closing ?> tag:
<?php
use Forte\Rewriting\Builders\Builder;
// <?php echo "hello";
echo Builder::phpTag('echo "hello";', hasClose: false);
#Comment Builders
Build HTML or Blade comments:
<?php
use Forte\Rewriting\Builders\Builder;
// <!-- This is a comment -->
echo Builder::comment('This is a comment');
// {{-- TODO: fix this --}}
echo Builder::bladeComment('TODO: fix this');
#Raw Text Builders
When you need to insert arbitrary source text without any transformation, use raw:
<?php
use Forte\Rewriting\Builders\Builder;
echo Builder::raw('<custom-element attr="val">content</custom-element>');
outputs:
<custom-element attr="val">content</custom-element>
When mixing strings and builders, normalize converts strings to RawBuilder instances while passing builders through unchanged. Use normalizeAll for arrays:
<?php
use Forte\Rewriting\Builders\Builder;
// RawBuilder
$node = Builder::normalize('some text');
// TextBuilder (unchanged)
$node = Builder::normalize(Builder::text('hi'));
// array<NodeBuilder>
$nodes = Builder::normalizeAll(['some text', Builder::text('hi')]);