Feature: Smarty template PHP cache
Problem Statement
The current implementation of the Smarty template engine in Hazaar compiles templates into PHP in memory and executes them using eval()
. While this approach is efficient for small templates (such as emails or text content), it is suboptimal for larger HTML templates used in the Hazaar view system. Executing large templates via eval()
introduces unnecessary overhead and security concerns. A more efficient caching mechanism is needed to store compiled PHP templates on disk and execute them using PHP’s include
function.
Who will benefit?
- Developers using the Hazaar view system for rendering HTML templates.
- Applications requiring improved performance and reduced memory usage when processing large templates.
- Systems that generate large numbers of templates and need an efficient caching strategy.
Benefits and risks
Benefits
- Improves performance by avoiding
eval()
for large templates. - Reduces memory consumption by executing precompiled templates from disk.
- Enhances security by limiting the use of
eval()
to smaller, transient templates. - Provides a structured caching mechanism for compiled templates.
Risks
- Additional logic is required to determine when to use
eval()
versusinclude
. - Disk I/O overhead may need to be managed efficiently for frequent template updates.
- Proper cache invalidation must be implemented to prevent stale templates from being used.
Proposed Solution
- Modify the Smarty template engine to support disk caching for compiled PHP templates.
- Implement logic to:
- Automatically store compiled PHP templates on disk when the template is loaded via
loadFromFile()
. - Continue using
eval()
for templates loaded vialoadFromString()
.
- Automatically store compiled PHP templates on disk when the template is loaded via
- Use PHP’s
include
function to execute cached templates instead ofeval()
. - Implement a cache invalidation mechanism to ensure templates are recompiled when changes are detected.
- Provide configuration options to control caching behavior.
Priority/Severity
-
High (This will bring a huge increase in performance/productivity/usability/legislative cover) -
Medium (This will bring a good increase in performance/productivity/usability) -
Low (anything else e.g., trivial, minor improvements)