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:):

1<?php
2
3use Forte\Components\ComponentManager;
4use Forte\Parser\Directives\Directives;
5use Forte\Parser\ParserOptions;
6
7$options = ParserOptions::defaults();
8
9$options->getDirectives(); // Directives
10$options->getComponentManager(); // ComponentManager
11$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:

1<?php
2
3use Forte\Parser\ParserOptions;
4
5$options = ParserOptions::make();
6
7$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:

1<?php
2
3use Forte\Facades\Forte;
4use Forte\Parser\ParserOptions;
5
6$options = ParserOptions::defaults()
7 ->withComponentPrefix('my-');
8
9$doc = Forte::parse('<my-widget />', $options);
10
11$component = $doc->findComponentByName('my-widget');
12$component->getPrefix(); // "my-"

To replace the component manager entirely, use components:

1<?php
2
3use Forte\Components\ComponentManager;
4use Forte\Parser\ParserOptions;
5
6$manager = new ComponentManager();
7$manager->register('custom:');
8
9$options = ParserOptions::defaults()->components($manager);
10
11$options
12 ->getComponentManager()
13 ->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:

1<?php
2
3use Forte\Facades\Forte;
4use Forte\Parser\ParserOptions;
5
6$options = ParserOptions::defaults()->withAllDirectives();
7
8$doc = Forte::parse('@customThing("arg")', $options);
9
10$doc->directives
11 ->first()
12 ->nameText(); // "customthing"

You can also pass a pre-configured Directives instance with directives:

1<?php
2
3use Forte\Parser\Directives\Directives;
4use Forte\Parser\ParserOptions;
5
6$directives = Directives::withDefaults();
7$directives->setAcceptAll(true);
8
9$options = ParserOptions::defaults()
10 ->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:

1<?php
2
3use Forte\Parser\ParserOptions;
4
5$options = ParserOptions::defaults()
6 ->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:

1<?php
2
3use Forte\Parser\ParserOptions;
4
5$a = ParserOptions::defaults()->withComponentPrefix('a-');
6$b = ParserOptions::make()->withComponentPrefix('b-');
7
8$a->merge($b);
9
10$prefixes = $a->getComponentManager()->getPrefixes();
11// Contains both "a-" and "b-"

#Passing Options to Parse

Pass your configured options as the second argument to Forte::parse() or Forte::parseFile():

1<?php
2
3use Forte\Facades\Forte;
4use Forte\Parser\ParserOptions;
5
6$options = ParserOptions::defaults()
7 ->withComponentPrefix('ui-')
8 ->withAllDirectives();
9
10$doc = Forte::parse('<ui-button>Click</ui-button> @customDirective', $options);
11
12$doc->findComponentByName('ui-button')->getPrefix(); // "ui-"
13$doc->directives->first()->nameText(); // "customdirective"

#See also