# Dynamic Documents



Once you have [specified a document as dynamic](/configuration/resources/documents#static_and_dynamic_documents_configuration), you can upload a template that Socotra will inject with data when it renders the document. Dynamic documents pass through stages as the platform generates instances, including the following:

* `dataReady`: all the data to be made available to the document template has been persisted, including any additional data or metadata specified by the [Document Data Snapshot Plugin](/configuration/plugins/document-data-snapshot).
* `ready`: the document has been rendered and can be downloaded from the platform.

Template Languages [#template-languages]

Dynamic document templates can be written in Liquid or Velocity templating languages. Socotra offers [several dedicated endpoints](/api/resources/document-resources#document_template_creation_and_update_endpoints) to create new dynamic document templates or to update existing ones.

Velocity Example [#velocity-example]

Suppose you wish to include a summary document on every issued quote that includes the name of the product. Here's a basic template:

```text
Summary for Product $data.productName

Lorem ipsum...
```

<Callout>
  You can provide HTML markup in your templates, including references to external resources like stylesheets and images. Examples in this guide are deliberately concise.
</Callout>

After you <ApiLink name="createVelocityDocumentTemplate">upload the template with a name</ApiLink> in an applicable resource group configuration and create a quote, you'll see the document appear as a <ApiLink name="DocumentInstanceResponse" /> in the list returned by <ApiLink name="fetchDocumentsForQuote" />. The `renderingData` property will contain a map of the data object available to the template. The `documentInstanceState` property changes as the document passes through the rendering cycle, with the successful `ready` state indicating that the rendered document can be downloaded from the platform. Rendered documents can be retrieved with the <ApiLink name="fetchDocumentResource" /> endpoint.

Data in `renderingData` can be referenced from the `data` object in the Velocity template. For example, given `renderingData` like the following:

```json
{
	"groupLocator": "01J8JXETQSQHCH2W62BHXEKFR1",
	"underwritingStatus": "none",
	"currency": "USD",
	"accountLocator": "01J8GC55Y1VYVTKSW1FY85NR53",
	"durationBasis": "months",
	"billingLevel": "inherit",
	"productName": "Commercial"
}
```

You could reference any of the values in the template with `$data.productName`, `$data.currency`, `$data.startTime`, and so on. Similarly, you can use any of the standard Velocity statements to traverse the data tree and iterate over elements. For instance, if you had an array of `insureds` in `renderingData`, each of which had `lastName` and `location` data fields, you could display an HTML summary list of insured last names and locations like this:

```text
<ul>
#foreach ($insured in $data.insureds)
    <li>${insured.data.lastName}: $insured.data.location</li>
#end
</ul>
```

Controlling Template Data [#controlling-template-data]

The [Document Data Snapshot](/configuration/plugins/document-data-snapshot) plugin allows you to modify or augment both the data and metadata available to document templates.

Document Snippets [#document-snippets]

You can define reusable document snippets for inclusion in your templates, using the typical syntax for Liquid or Velocity. For example, after configuring and uploading snippets, you may include them in Velocity templates with the `#parse` or `#include` directives. You can also include snippets in [bootstrap configuration](/configuration/general-topics/bootstrap), just as you can with other document templates.

Document snippets can be included in the [bootstrap configuration](/configuration/general-topics/bootstrap), and managed in a fashion similar to other document resources. In document templates, you refer to snippets for inclusion by static name, using the usual syntax for such references (e.g. in Velocity, the `#parse` or `#include` directives), and rely upon the platform's resource selection facilities to include the correct instance of the snippet.

Snippets Example (Velocity) [#snippets-example-velocity]

Suppose you would like to include a footer snippet in a document template, and decide that the static name of the snippet will be `footer`. First, update your main document template to include the `footer` with either `#include` or `#parse`, like this:

```text
#parse( "footer" )
```

Next, update your top-level <ApiLink name="ConfigurationRef">configuration</ApiLink> to define the snippet:

```json
{
	// ...,
	"templateSnippets": {
		"footer": {
			"selectionTimeBasis": "policyStartTime"
		}
	}
	// ...,
}
```

Then include the snippet name in the `templateSnippets` array in the requisite <ApiLink name="DocumentConfigRef" />:

```json
{
	// ...,
	"templateSnippets": ["footer"]
	// ...,
}
```

After deploying, you can upload instances of your snippet. There are snippet-specific upload endpoints for both <ApiLink name="uploadVelocity">Velocity</ApiLink> and <ApiLink name="uploadLiquid">Liquid</ApiLink>. After uploading instances, assign them to appropriate resource groups so that the expected instance is included in the document when the document is rendered.

Custom Fonts [#custom-fonts]

You may include custom fonts in document templates intended for PDF rendering. Custom fonts are managed as a resource.

In order to use a custom font, declare a static name for the font in the `customFonts` array in the top-level <ApiLink name="ConfigurationRef">configuration</ApiLink>. For any documents that require the custom font, add the static name to the <ApiLink name="DocumentConfigRef" /> `customFonts` array.

After deploying the configuration, you can upload the font as a resource with the <ApiLink name="addFont" /> endpoint, in TTF or OTF format. Then you can add the font to one or more resource groups.

<Callout>
  The `selectionTimeBasis` for custom fonts is always `now`.
</Callout>

Templates can reference custom fonts just as they would in any other context. For example, a Velocity template could specify CSS styling like this:

```css
.custom {
	font-family: 'Faculty Glyphic', sans-serif;
	font-weight: 400;
	font-style: normal;
}
```

If the "Faculty Glyphic" font was configured as specified, then the custom font will render as expected. If the font cannot be found, the platform will default to rendering with standard fonts.

Troubleshooting and Template Development [#troubleshooting-and-template-development]

Socotra enforces Velocity's `strict` rendering option.

Embedding SVGs in document templates is not supported.

Streamlining the rendering feedback loop [#streamlining-the-rendering-feedback-loop]

You can use the <ApiLink name="renderDocument">ad-hoc document rendering endpoint</ApiLink> to see how a template would be rendered against a given reference item, such as a quote, policy, or term. This can help with troubleshooting and template refinement since you don't have to conduct any actual transactions to induce document rendering for testing purposes.
