Parsing
Parser Options
ParserOptions controls how Forte parses templates, including which directives are recognized, which component prefixes are registered, and which extensions are active. You pass options as the second argument to Forte::parse().
#Creating Options
Use ParserOptions::defaults() to get a fully configured instance with Forte's built-in directive definitions and the default component prefixes (x-, livewire:, flux:):
<?php
use Forte\Components\ComponentManager;
use Forte\Parser\Directives\Directives;
use Forte\Parser\ParserOptions;
$options = ParserOptions::defaults();
$options->getDirectives(); // Directives
$options->getComponentManager(); // ComponentManager
$options->hasExtensions(); // false
Use ParserOptions::make() for a minimal starting point. Directives and component prefixes are resolved lazily at parse time. If you do not explicitly replace them, make() falls back to the same defaults as defaults(). To start with a truly empty registry, pass your own Directives or ComponentManager instances:
<?php
use Forte\Parser\ParserOptions;
$options = ParserOptions::make();
$options->hasExtensions(); // false
#Configuring Components
The withComponentPrefix method registers an additional component prefix on the existing manager. This is the most common way to add a custom prefix:
<?php
use Forte\Facades\Forte;
use Forte\Parser\ParserOptions;
$options = ParserOptions::defaults()
->withComponentPrefix('my-');
$doc = Forte::parse('<my-widget />', $options);
$component = $doc->findComponentByName('my-widget');
$component->getPrefix(); // "my-"
To replace the component manager entirely, use components:
<?php
use Forte\Components\ComponentManager;
use Forte\Parser\ParserOptions;
$manager = new ComponentManager();
$manager->register('custom:');
$options = ParserOptions::defaults()->components($manager);
$options
->getComponentManager()
->getPrefixes(); // ["x-", "livewire:", "flux:", "custom:"]
#Configuring Directives
By default, Forte only recognizes directives from its built-in registry and those available within the current Laravel application.
The withAllDirectives method tells the parser to treat any @word pattern as a directive, even if it's not in the registry:
<?php
use Forte\Facades\Forte;
use Forte\Parser\ParserOptions;
$options = ParserOptions::defaults()->withAllDirectives();
$doc = Forte::parse('@customThing("arg")', $options);
$doc->directives
->first()
->nameText(); // "customthing"
You can also pass a pre-configured Directives instance with directives:
<?php
use Forte\Parser\Directives\Directives;
use Forte\Parser\ParserOptions;
$directives = Directives::withDefaults();
$directives->setAcceptAll(true);
$options = ParserOptions::defaults()
->directives($directives);
#Syncing with Laravel
When running inside a Laravel application, syncLaravelDirectives reads the directives registered on Laravel's BladeCompiler and adds them to Forte's registry automatically:
<?php
use Forte\Parser\ParserOptions;
$options = ParserOptions::defaults()
->syncLaravelDirectives();
This is useful when your application (or a package) registers custom Blade directives via Blade::directive() and you want Forte to recognize them without enabling accept-all mode.
#Merging Options
Use merge to combine two option sets. The merged options inherit component prefixes and extensions from both, and accept-all or sync flags from either:
<?php
use Forte\Parser\ParserOptions;
$a = ParserOptions::defaults()->withComponentPrefix('a-');
$b = ParserOptions::make()->withComponentPrefix('b-');
$a->merge($b);
$prefixes = $a->getComponentManager()->getPrefixes();
// Contains both "a-" and "b-"
#Passing Options to Parse
Pass your configured options as the second argument to Forte::parse() or Forte::parseFile():
<?php
use Forte\Facades\Forte;
use Forte\Parser\ParserOptions;
$options = ParserOptions::defaults()
->withComponentPrefix('ui-')
->withAllDirectives();
$doc = Forte::parse('<ui-button>Click</ui-button> @customDirective', $options);
$doc->findComponentByName('ui-button')->getPrefix(); // "ui-"
$doc->directives->first()->nameText(); // "customdirective"
#See also
- Components and Slots: Registering custom component prefixes
- Directives Registry: How Forte discovers and configures directives
- Documents: The Document class returned by
Forte::parse()