🏷️ Section Tags
What are Section Tags?
StoneJS provides special section tags that let you organize your component code into logical blocks with different execution behaviors. These tags use a distinctive <%%tag> syntax that won't conflict with EJS.
Quick Reference
| Tag | Execution | Primary Use |
|---|---|---|
<%%doc> |
Never (removed) | Documentation |
<%%once> |
Once per component load | Static initialization |
<%%init> |
Every request (start) | Request setup |
<%%js> |
Inline (during body) | JavaScript code blocks |
<%%cleanup> |
Every request (end) | Resource cleanup |
1. <%%doc> Documentation Tag
Documentation blocks are completely removed during preprocessing. Use them for internal notes, explanations, and team documentation.
Syntax:
- Document component behavior and API
- Explain complex algorithms
- Add team notes and TODOs
- Provide usage examples
2. <%%once> One-Time Initialization
Code in <%%once> blocks runs only once when the component is first loaded, not on every request. Perfect for expensive operations that produce static results.
Syntax:
Example:
- Loading configuration files
- Compiling regular expressions
- Setting module-level constants
- Expensive computations with static results
- Initializing shared resources
3. <%%init> Request Initialization
Code in <%%init> blocks runs at the beginning of every request, before any output is generated.
Syntax:
Example:
- Loading request-specific data
- Validating request parameters
- Setting up variables used throughout component
- Starting performance timers
- Initializing request logging
4. <%%js> Inline JavaScript Blocks
The <%%js> tag is a convenient alternative to standard EJS <% %> tags. It's converted directly to EJS code blocks during preprocessing.
Syntax:
Converts to:
Example:
- Alternative syntax that matches other section tags
- Processing data within the component body
- Computing values for display
- Any inline JavaScript that doesn't fit in init/once/cleanup
Note: <%%js> is functionally identical to <% %>. Use whichever you prefer - both work exactly the same way!
5. <%%cleanup> Request Cleanup
Code in <%%cleanup> blocks runs at the end of every request, even if errors occur. Uses try/finally under the hood.
Syntax:
Example:
- Logging request metrics and timing
- Closing database connections
- Cleaning up temporary files
- Recording analytics
- Releasing locks and resources
6. Combining Multiple Tags
You can use all tags in the same component for complete control over execution flow:
7. 📚 Best Practices
When to Use Each Tag:
<%%doc> - Use liberally for documentation
- ✓ Explain complex component logic
- ✓ Document expected parameters
- ✓ Add team notes and warnings
- ✗ Don't use for code you might need later (use git history instead)
<%%once> - Use sparingly for truly static data
- ✓ Loading configuration files
- ✓ Compiling regular expressions
- ✓ Module-level constants
- ✗ Don't use for request-specific data
- ✗ Don't use for user-specific data
- ✗ Don't use for anything that needs to update without restarting
<%%init> - Use for request setup
- ✓ Loading request-specific data
- ✓ Validating parameters
- ✓ Setting up variables used throughout component
- ✓ Starting performance timers
- ✗ Don't use for output generation (put that in the main body)
<%%js> - Use for inline code blocks
- ✓ Processing data within the component body
- ✓ Computing values for display
- ✓ When you prefer StoneJS-style tag syntax
- ✓ Completely interchangeable with
<% %> - ✗ Not required - use
<% %>if you prefer
<%%cleanup> - Use for guaranteed cleanup
- ✓ Closing connections
- ✓ Logging metrics
- ✓ Releasing resources
- ✓ Recording analytics
- ✗ Don't rely on it for critical business logic
- ✗ Don't use for output (cleanup runs after output is sent)
8. ⚙️ Technical Details
Tag Syntax:
section tags use a distinctive <%%tagname> syntax to avoid conflicts with EJS:
- Opening:
<%%tagname> - Closing:
</tagname%%> - This syntax never conflicts with EJS (
<%and%>)
How It Works:
- Preprocessing: Components are processed before EJS compilation
- <%%doc> Removal: Completely stripped from output
- <%%js> Conversion: Converted to standard
<% %>EJS tags - <%%once> Cache: Uses global cache (global.__stoneOnceCache) to track execution
- <%%init> Placement: Moved to the top of the component
- <%%cleanup> Wrapper: Wrapped in try/finally to ensure execution
Execution Order:
- Component loaded from disk
- Preprocessor detects section tags
- Tags are extracted and transformed
- Component is reconstructed with proper order
- EJS compiles the preprocessed component
- On first request: <%%once> code runs
- On every request: <%%init> → main body → <%%cleanup>
Performance:
- Preprocessing happens only once (cached components stay preprocessed)
- <%%once> blocks add minimal overhead (simple cache check)
- <%%cleanup> uses try/finally (standard JavaScript, very efficient)
- No runtime performance impact after first load