Custom Components
SQLPage’s component system is extensible - you can create custom components using Handlebars templates to add any UI element not covered by the built-in components.Understanding SQLPage Components
Every SQLPage component is a Handlebars template that transforms SQL query results into HTML. When you write:- Looks for
sqlpage/templates/text.handlebars - Passes your query results to the template
- Renders HTML from the template
- Streams it to the browser
Creating Your First Custom Component
Step 1: Create the Template File
Create a file insqlpage/templates/ with your component name:
Step 2: Write the Handlebars Template
Here’s a simple example:Step 3: Use Your Component
Handlebars Basics
Variables
Access query result columns as variables:Conditionals
Loops
For components that display multiple rows:Component Structure
Top-Level Properties
The first row of your query provides top-level properties:The each_row Helper
{{#each_row}} iterates over all rows except the first (which contains top-level properties):
Real-World Example: Dual-List Component
Here’s a complete example of a custom component for selecting items from two lists:Built-in Handlebars Helpers
SQLPage provides several custom helpers:icon_img
Render icons from Tabler Icons:markdown
Render Markdown content:{{{ to avoid escaping HTML.
default
Provide fallback values:eq, ne, gt, lt, gte, lte
Comparison operators:static_path
Get the correct path for static assets:app_config
Access configuration values:@csp_nonce
For inline scripts to comply with Content Security Policy:Styling Custom Components
Using Bootstrap Classes
SQLPage includes Bootstrap 5, so you can use its utility classes:Custom CSS
Add custom styles insqlpage/sqlpage.css:
Advanced Techniques
Processing JSON Data
Delayed Template Execution
The{{#delay}} helper defers content rendering:
Component State
Use@component_index to generate unique IDs for multiple instances:
Overriding Built-in Components
You can override any built-in component by creating a file with the same name:Best Practices
1. Keep Templates Simple
Complex logic belongs in SQL, not templates:2. Use Semantic HTML
3. Make Components Accessible
4. Handle Missing Data
5. Security: Escape User Input
Use double braces{{}} to escape HTML by default:
Testing Custom Components
Preview During Development
Set environment to development for live reloading:Test with Sample Data
Create a test page:Inspecting Built-in Components
Learn by example - all built-in component templates are in the SQLPage source:- Simple components:
code.handlebars,text.handlebars - List-based:
list.handlebars,table.handlebars - Complex forms:
form.handlebars - Interactive:
chart.handlebars,map.handlebars
Examples
Explore custom component examples:- Dual-list selector:
examples/custom form component/ - Custom charts:
examples/charts, computations and custom components/