Skip to content

Whitespace Control

YAPL provides mechanisms to control whitespace in your templates, ensuring clean, properly formatted output. This is especially important for AI prompts where formatting can affect model performance.

Configure whitespace behavior when creating a YAPL instance:

const yapl = new NodeYAPL({
baseDir: "./prompts",
whitespace: {
trimBlocks: true, // Remove newlines after block tags (default: true)
lstripBlocks: true, // Remove leading whitespace before block tags (default: true)
dedentBlocks: true, // Remove common indentation from block content (default: true)
},
});

Removes newlines immediately following block tags:

<!-- Without trimBlocks -->
{% if condition %}
Content here
{% endif %}
More content
<!-- With trimBlocks (default) -->
{% if condition %}
Content here
{% endif %}
More content

Removes leading whitespace on lines that contain only block tags:

<!-- Template with indented tags -->
{% if condition %}
Content
{% endif %}
<!-- With lstripBlocks (default), leading spaces before tags are removed -->
Content

Removes common indentation from block content:

{% block content %}
This is indented content.
This line has the same indentation.
This line has extra indentation.
{% endblock %}
<!-- With dedentBlocks (default), output: -->
This is indented content.
This line has the same indentation.
This line has extra indentation.

Use - modifiers to control whitespace around individual tags:

{%- if condition -%} <!-- Remove whitespace on both sides -->
{% if condition -%} <!-- Remove whitespace on the right -->
{%- if condition %} <!-- Remove whitespace on the left -->
{{- variable -}} <!-- Remove whitespace on both sides -->
{{ variable -}} <!-- Remove whitespace on the right -->
{{- variable }} <!-- Remove whitespace on the left -->
{#- This comment strips surrounding whitespace -#}

Proper whitespace control ensures your YAPL templates produce clean, well-formatted output optimized for AI models and human readability.