Skip to main content

Templates Package Cheatsheet

Template Creation

Template::make(string $pathOrDsn)
Template::fromDsn(string $dsn) // engine:path

Template::twig()
Template::blade()
Template::arrowpipe()
Template::forEngine(string $engine)

new Template(path: '', config: ?TemplateEngineConfig, driver: ?CanHandleTemplate)

Static Shortcuts

Template::text(string $pathOrDsn, array $variables): string
Template::messages(string $pathOrDsn, array $variables): Messages

Fluent API

withConfig(TemplateEngineConfig $config): self
withDriver(CanHandleTemplate $driver): self

get(string $path): self
withTemplate(string $path): self
withTemplateContent(string $content): self
from(string $content): self

with(array $values): self
withValues(array $values): self

Outputs

toText(): string
toMessages(): Messages
toMessageStore(): MessageStore
toArray(): array

Message Rendering

renderMessage(Message $message): Message    // render template variables in message text parts
renderMessages(Messages $messages): Messages // render template variables in all messages

Introspection

config(): TemplateEngineConfig
template(): string
params(): array
variables(): array
info(): TemplateInfo
validationErrors(): array

TemplateEngineConfig

TemplateEngineConfig::fromPreset(string $preset, ?string $basePath = null): self
TemplateEngineConfig::fromArray(array $config): self
TemplateEngineConfig::twig(string $resourcePath = '', string $cachePath = ''): self
TemplateEngineConfig::blade(string $resourcePath = '', string $cachePath = ''): self
TemplateEngineConfig::arrowpipe(string $resourcePath = '', string $cachePath = ''): self

Minimal Examples

$text = Template::forEngine('twig')
    ->get('prompts/demo-twig/hello')
    ->with(['name' => 'World'])
    ->toText();

$messages = Template::messages('twig:prompts/demo-twig/hello', ['name' => 'World']);