Hone logo
Hone
Problems

Implement a Simple Template Engine in Python

This challenge involves building a basic template engine in Python. Template engines are crucial for dynamically generating text-based content, such as HTML pages, emails, or configuration files, by substituting placeholders within a static template with dynamic data.

Problem Description

You need to implement a Python class, TemplateEngine, that can process a template string and render it into a final output string. The template string will contain placeholders enclosed in double curly braces {{ }}. These placeholders represent variables that should be replaced with corresponding values provided during the rendering process.

Key Requirements:

  1. Initialization: The TemplateEngine class should be initialized with a template string.
  2. Rendering: A render method should be implemented, which takes a dictionary of variables as input. This method should iterate through the template, identify placeholders, and replace them with their corresponding values from the provided dictionary.
  3. Placeholder Syntax: Placeholders will always be in the format {{variable_name}}.
  4. Variable Substitution: If a variable name found in a placeholder exists as a key in the input dictionary, its corresponding value should be used for replacement.
  5. Unmatched Placeholders: If a placeholder's variable name is not found in the input dictionary, the placeholder should remain unchanged in the output.
  6. Data Types: The values in the dictionary can be of various basic Python types (strings, integers, floats, booleans). Ensure they are converted to strings for substitution.

Expected Behavior:

The render method should return a new string representing the fully rendered template. The original template string should not be modified.

Edge Cases to Consider:

  • Empty template string.
  • Template string with no placeholders.
  • Template string with only placeholders.
  • Dictionary with extra variables not present in the template.
  • Placeholders with the same variable name appearing multiple times.

Examples

Example 1:

Input Template: "Hello, {{name}}! You are {{age}} years old."
Input Variables: {"name": "Alice", "age": 30}
Output: "Hello, Alice! You are 30 years old."
Explanation: The placeholders `{{name}}` and `{{age}}` were replaced with "Alice" and "30" respectively.

Example 2:

Input Template: "Welcome to {{city}}, {{user}}!"
Input Variables: {"user": "Bob"}
Output: "Welcome to {{city}}, Bob!"
Explanation: `{{user}}` was replaced with "Bob". `{{city}}` was not found in the provided variables, so it remained unchanged.

Example 3:

Input Template: "The result is {{value1}} and {{value2}}."
Input Variables: {"value1": 100, "value2": True}
Output: "The result is 100 and True."
Explanation: Integer and boolean values are correctly converted to strings for substitution.

Example 4: (Edge Case - Empty Template)

Input Template: ""
Input Variables: {"name": "Charlie"}
Output: ""
Explanation: An empty template results in an empty output.

Constraints

  • The template string will consist of ASCII characters.
  • Variable names within placeholders will only contain alphanumeric characters and underscores (a-z, A-Z, 0-9, _).
  • The number of placeholders in a template will not exceed 1000.
  • The length of the template string will not exceed 10,000 characters.
  • The render method should aim for a reasonable time complexity, ideally linear with respect to the template length and the number of variables.

Notes

  • You do not need to handle complex logic within the template (e.g., loops, conditionals). This is a basic substitution engine.
  • Regular expressions can be a powerful tool for finding and replacing patterns in strings.
  • Consider how to efficiently find all occurrences of placeholders.
  • The problem statement specifies that unmatched placeholders should remain as they are. This is an important behavior to preserve.
Loading editor...
python