Twig Templates
WPBits Block Studio supports Twig templates for dynamic block rendering. Twig is a PHP template engine that offers security features, caching, and an easy-to-read syntax.
Why Twig?
Section titled “Why Twig?”Twig provides several advantages over plain PHP templates:
- Security — Automatic escaping prevents XSS attacks
- Readability — Clean, simple syntax
- Caching — Compiled templates are cached for performance
- Extensibility — Filters and functions for common tasks
Basic Twig Template
Section titled “Basic Twig Template”A simple block template in Twig:
{# block-title.twig #}<div class="feature-block"> <h2>{{ attributes.title }}</h2> <p>{{ attributes.description }}</p></div>Output Escaping
Section titled “Output Escaping”Twig automatically escapes output to prevent XSS:
{# This is safe - content is escaped #}{{ attributes.title }}
{# For raw HTML (use carefully) #}{{ attributes.content | raw }}Template Syntax
Section titled “Template Syntax”Variables
Section titled “Variables”Access block attributes with dot notation:
{{ attributes.title }}{{ attributes.image.url }}{{ attributes.settings.size }}Conditionals
Section titled “Conditionals”{% if attributes.show_icon %} <span class="icon">{{ attributes.icon }}</span>{% endif %}{% for item in attributes.items %} <li>{{ item.name }}</li>{% endfor %}Filters
Section titled “Filters”Transform output with filters:
{# Uppercase #}{{ attributes.title | upper }}
{# Date formatting #}{{ attributes.date | date('F j, Y') }}
{# Truncate #}{{ attributes.text | truncate(100) }}WPBits Block Studio Twig Features
Section titled “WPBits Block Studio Twig Features”WPBits Block Studio’s Twig implementation includes:
Security Hardening
Section titled “Security Hardening”- All output is escaped by default
- Sandboxed environment
- Restricted functions
Built-in Filters
Section titled “Built-in Filters”| Filter | Description | Example |
|---|---|---|
upper | Uppercase | {{ text | upper }} |
lower | Lowercase | {{ text | lower }} |
trim | Remove whitespace | {{ text | trim }} |
date | Format date | {{ date | date('Y-m-d') }} |
esc_html | Escape HTML | {{ html | esc_html }} |
esc_attr | Escape attribute | {{ attr | esc_attr }} |
Built-in Functions
Section titled “Built-in Functions”| Function | Description |
|---|---|
asset() | Get asset URL |
theme() | Get theme URL |
Template Files
Section titled “Template Files”WPBits Block Studio generates these template files:
block-title.twig (Primary)
Section titled “block-title.twig (Primary)”Twig template for primary rendering:
<div class="my-block"> <h2>{{ attributes.title }}</h2></div>preview.twig (Optional)
Section titled “preview.twig (Optional)”Template for editor preview (if different from frontend):
<div class="my-block preview"> <h2>{{ attributes.title }}</h2></div>Creating Custom Templates
Section titled “Creating Custom Templates”Step 1: Enable Twig Rendering
Section titled “Step 1: Enable Twig Rendering”- In block editor, go to Template tab
- Select Twig as render mode
- Choose template type
Step 2: Write Template
Section titled “Step 2: Write Template”Create your template using Twig syntax:
<div class="testimonial-block"> {% if attributes.quote %} <blockquote>{{ attributes.quote }}</blockquote> {% endif %}
{% if attributes.author %} <cite>{{ attributes.author }}</cite> {% endif %}
{% if attributes.rating %} <div class="rating"> {% for i in 1..attributes.rating %} ★ {% endfor %} </div> {% endif %}</div>Step 3: Preview
Section titled “Step 3: Preview”WPBits Block Studio shows a live preview as you edit the template.
Troubleshooting
Section titled “Troubleshooting”Template Not Rendering
Section titled “Template Not Rendering”- Check file permissions
- Verify Twig files are in correct location
- Enable WP Debug to see errors
Escaping Issues
Section titled “Escaping Issues”Use | raw only when absolutely necessary, and never with user input:
{# Safe - user input is escaped #}{{ attributes.user_content }}
{# Dangerous - only use with trusted content #}{{ trusted_html_content | raw }}Next Steps
Section titled “Next Steps”- Custom Render PHP — Advanced rendering with PHP
- Export Templates — Deploy templates with your blocks