LoaderInterface, so you can also supply your own.
FileLoader
The default. Searches configured directories using dot-notation.
NamespaceLoader
Adds namespace prefixes (
admin::dashboard) that map to separate directory trees.MemoryLoader
Stores templates in memory. Ideal for unit tests and generated content.
FileLoader
FileLoader is wired in automatically when you call ->paths() or Lexer::fromConfig(). You do not need to instantiate it directly for typical usage.
Dot-notation resolution
A dot in a template name is converted to a directory separator before the.lex extension is appended:
| Template name | Resolved path |
|---|---|
'home' | views/home.lex |
'layouts.app' | views/layouts/app.lex |
'admin.partials.nav' | views/admin/partials/nav.lex |
Cache key
FileLoader generates its cache key as md5(absolutePath . ':' . filemtime). This means the compiled cache is automatically invalidated when the source file changes — without reading its content.
Instantiating directly
You rarely need to create aFileLoader yourself, but it is available if you want to use it standalone with tooling:
NamespaceLoader
NamespaceLoader wraps a base FileLoader and adds namespace support. A namespace is a short alias (e.g. admin) that maps to a directory. Template names that contain :: are routed to the matching namespace loader; all other names fall through to the base loader.
Registering namespaces
Resolution rules
| Template name | Resolved path |
|---|---|
'admin::dashboard' | views/admin/dashboard.lex |
'admin::users.index' | views/admin/users/index.lex |
'mail::welcome' | views/mail/welcome.lex |
'home' | views/home.lex (falls through to base) |
Multiple directories per namespace
You can calladdNamespace() multiple times with the same prefix to register additional directories for that namespace. Lex searches them in registration order.
The namespace separator is always
:: and cannot be changed.MemoryLoader
MemoryLoader stores template source strings in a plain PHP array. It has no dependency on the filesystem, making it the best choice for unit tests and for templates generated at runtime.
Basic usage
Using MemoryLoader in tests
MemoryLoader is primarily used to supply template source strings to the compiler during testing. Access the raw source via load() or all() for assertions:
Other MemoryLoader methods
| Method | Description |
|---|---|
set(string $name, string $source): void | Store or replace an in-memory template. |
remove(string $name): void | Remove a stored template. |
all(): array | Return all stored templates as name => source. |
exists(string $name): bool | Check whether a template is registered. |
MemoryLoader::getPath() always returns null because in-memory templates have no filesystem path. The dependency graph does not track in-memory templates, so automatic cache invalidation is not applicable.