|
11 | 11 | - [Adding Sections](#adding-sections) |
12 | 12 | - [Defining Fields](#defining-fields) |
13 | 13 | - [Array & Object Fields](#array-and-object-fields) |
| 14 | +- [Ephemeral Templates](#ephemeral-templates) |
| 15 | + - [Creating Ephemeral Templates](#creating-ephemeral-templates) |
| 16 | + - [Ephemeral Schema & Rendering](#ephemeral-schema-and-rendering) |
14 | 17 | - [JSON Schema Generation](#json-schema-generation) |
15 | 18 | - [Template Schemas](#template-schemas) |
16 | 19 | - [Section Schemas](#section-schemas) |
|
32 | 35 | <a name="introduction"></a> |
33 | 36 | ## Introduction |
34 | 37 |
|
35 | | -Schematic is a database-driven templating engine for Laravel that generates JSON Schema definitions from your templates. It is designed for use with LLM structured output APIs such as those provided by OpenAI and Anthropic, allowing you to define templates with typed fields and automatically produce valid JSON Schema for tool use and structured responses. |
| 38 | +Schematic is a templating engine for Laravel that generates JSON Schema definitions from your templates. It is designed for use with LLM structured output APIs such as those provided by OpenAI and Anthropic, allowing you to define templates with typed fields and automatically produce valid JSON Schema for tool use and structured responses. Templates can be persisted to the database or created as [ephemeral (in-memory) templates](#ephemeral-templates) for on-the-fly use without any database overhead. |
36 | 39 |
|
37 | 40 | <a name="installation"></a> |
38 | 41 | ## Installation |
@@ -195,6 +198,82 @@ TPL, |
195 | 198 | ); |
196 | 199 | ``` |
197 | 200 |
|
| 201 | +<a name="ephemeral-templates"></a> |
| 202 | +## Ephemeral Templates |
| 203 | + |
| 204 | +Ephemeral templates are in-memory templates that are **not persisted to the database**. They are useful for one-off or dynamic templates that you build at runtime — no migrations or database queries required. |
| 205 | + |
| 206 | +Ephemeral templates support the same core features as database-backed templates: sections, fields, JSON Schema generation, rendering, and previewing. |
| 207 | + |
| 208 | +<a name="creating-ephemeral-templates"></a> |
| 209 | +### Creating Ephemeral Templates |
| 210 | + |
| 211 | +Use the `ephemeral` method on the `Schematic` facade to create an in-memory template: |
| 212 | + |
| 213 | +```php |
| 214 | +use Yannelli\Schematic\Facades\Schematic; |
| 215 | + |
| 216 | +$template = Schematic::ephemeral( |
| 217 | + slug: 'intake-form', |
| 218 | + name: 'Patient Intake Form', |
| 219 | + description: 'A quick intake form built on the fly', |
| 220 | +); |
| 221 | + |
| 222 | +$template->addSection( |
| 223 | + slug: 'demographics', |
| 224 | + name: 'Demographics', |
| 225 | + content: '{{ patient_name }}, Age: {{ age }}', |
| 226 | + fields: [ |
| 227 | + ['name' => 'patient_name', 'type' => 'string', 'description' => 'Full name'], |
| 228 | + ['name' => 'age', 'type' => 'integer', 'description' => 'Patient age'], |
| 229 | + ], |
| 230 | + examples: ['patient_name' => 'Jane Doe', 'age' => 34], |
| 231 | +); |
| 232 | +``` |
| 233 | + |
| 234 | +You may also create ephemeral templates directly via the `EphemeralTemplate` class: |
| 235 | + |
| 236 | +```php |
| 237 | +use Yannelli\Schematic\Ephemeral\EphemeralTemplate; |
| 238 | + |
| 239 | +$template = EphemeralTemplate::make('quick-note', 'Quick Note'); |
| 240 | +$section = $template->addSection('body', 'Body', content: '{{ note }}'); |
| 241 | +$section->addField('note', 'string', 'The note content'); |
| 242 | +``` |
| 243 | + |
| 244 | +Sections on ephemeral templates support the same fluent methods as database-backed sections, including `addField`, `removeField`, `enable`, `disable`, and `setExamples`. All mutations happen in memory. |
| 245 | + |
| 246 | +<a name="ephemeral-schema-and-rendering"></a> |
| 247 | +### Ephemeral Schema & Rendering |
| 248 | + |
| 249 | +Ephemeral templates generate JSON Schema and render content exactly like their database-backed counterparts: |
| 250 | + |
| 251 | +```php |
| 252 | +// JSON Schema generation |
| 253 | +$schema = $template->toJsonSchema(); |
| 254 | +$doc = $template->toJsonSchemaDocument(); |
| 255 | +$sectionSchema = $template->sectionSchema('demographics'); |
| 256 | + |
| 257 | +// Rendering with data |
| 258 | +$output = $template->render([ |
| 259 | + 'demographics' => ['patient_name' => 'Alice Smith', 'age' => 28], |
| 260 | +]); |
| 261 | + |
| 262 | +// Preview using example data |
| 263 | +$preview = $template->preview(); |
| 264 | +``` |
| 265 | + |
| 266 | +Section management works identically — you can iterate, reorder, enable, and disable sections: |
| 267 | + |
| 268 | +```php |
| 269 | +$template->section('demographics')->disable(); |
| 270 | +$template->reorderSections(['body', 'demographics']); |
| 271 | + |
| 272 | +foreach ($template->iterateSections() as $section) { |
| 273 | + // Only enabled sections |
| 274 | +} |
| 275 | +``` |
| 276 | + |
198 | 277 | <a name="json-schema-generation"></a> |
199 | 278 | ## JSON Schema Generation |
200 | 279 |
|
|
0 commit comments