Feature: Smarty template PHP cache #319

Closed
opened 2025-02-28 22:10:53 +00:00 by jamie · 5 comments
jamie commented 2025-02-28 22:10:53 +00:00 (Migrated from git.hazaar.io)

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() versus include.
  • 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

  1. Modify the Smarty template engine to support disk caching for compiled PHP templates.
  2. Implement logic to:
    • Automatically store compiled PHP templates on disk when the template is loaded via loadFromFile().
    • Continue using eval() for templates loaded via loadFromString().
  3. Use PHP’s include function to execute cached templates instead of eval().
  4. Implement a cache invalidation mechanism to ensure templates are recompiled when changes are detected.
  5. 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)
## 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()` versus `include`. - 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 1. Modify the Smarty template engine to support disk caching for compiled PHP templates. 2. Implement logic to: - Automatically store compiled PHP templates on disk when the template is loaded via `loadFromFile()`. - Continue using `eval()` for templates loaded via `loadFromString()`. 3. Use PHP’s `include` function to execute cached templates instead of `eval()`. 4. Implement a cache invalidation mechanism to ensure templates are recompiled when changes are detected. 5. Provide configuration options to control caching behavior. ## Priority/Severity - [x] 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)
jamie commented 2025-02-28 22:10:54 +00:00 (Migrated from git.hazaar.io)

assigned to @jamie

assigned to @jamie
jamie commented 2025-02-28 22:56:03 +00:00 (Migrated from git.hazaar.io)

created branch 319-feature-smarty-template-php-cache to address this issue

created branch [`319-feature-smarty-template-php-cache`](/hazaar/framework/-/compare/master...319-feature-smarty-template-php-cache) to address this issue
jamie commented 2025-02-28 22:56:09 +00:00 (Migrated from git.hazaar.io)

mentioned in merge request !200

mentioned in merge request !200
jamie commented 2025-03-04 01:54:15 +00:00 (Migrated from git.hazaar.io)

mentioned in commit 0bd11eda0d

mentioned in commit 0bd11eda0defd116c9136a155d050a3d1e06ca86
jamie commented 2025-03-04 03:01:11 +00:00 (Migrated from git.hazaar.io)

mentioned in merge request !201

mentioned in merge request !201
jamie (Migrated from git.hazaar.io) closed this issue 2025-03-04 03:01:55 +00:00
jamie self-assigned this 2025-09-04 01:14:39 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: hazaar/framework#319
No description provided.