Simple Template Engine in Python
Template engines are powerful tools for generating dynamic content by substituting variables within a template string. This challenge asks you to implement a basic template engine in Python, allowing you to replace placeholders in a template with provided values. This is useful for generating configuration files, HTML pages, or any other text-based output where data needs to be dynamically inserted.
Problem Description
You are tasked with creating a TemplateEngine class that can render a template string with a set of variables. The template string will contain placeholders in the format {{variable_name}}. The engine should replace these placeholders with the corresponding values from a provided dictionary. If a placeholder is not found in the dictionary, it should be replaced with an empty string.
Key Requirements:
- The
TemplateEngineclass should have anrendermethod. - The
rendermethod should take two arguments: a template string and a dictionary of variables. - The
rendermethod should return the rendered string with all placeholders replaced. - The engine should handle cases where a placeholder is not found in the dictionary gracefully (by replacing it with an empty string).
- The engine should handle multiple occurrences of the same variable.
- The engine should not modify the original template string.
Expected Behavior:
The render method should perform a simple string replacement based on the provided dictionary. It should only replace placeholders in the specified format {{variable_name}}. Other text within the template string should remain unchanged.
Edge Cases to Consider:
- Empty template string.
- Empty dictionary.
- Placeholders with invalid variable names (e.g.,
{{or}}). These should be treated as literal text and not replaced. - Nested curly braces (e.g.,
{{{{variable}}}}). These should be treated as literal text. - Placeholders with whitespace around the variable name (e.g.,
{{ variable }}).
Examples
Example 1:
Input: template = "Hello, {{name}}! Welcome to {{city}}.", variables = {"name": "Alice", "city": "New York"}
Output: "Hello, Alice! Welcome to New York."
Explanation: The placeholders `{{name}}` and `{{city}}` are replaced with their corresponding values from the `variables` dictionary.
Example 2:
Input: template = "The value is {{value}}.", variables = {"other_value": 123}
Output: "The value is ."
Explanation: The placeholder `{{value}}` is not found in the `variables` dictionary, so it is replaced with an empty string.
Example 3:
Input: template = "This is a test with {{var}} and {{var}} again.", variables = {"var": "test"}
Output: "This is a test with test and test again."
Explanation: The placeholder `{{var}}` appears twice and is replaced with the same value "test" in both instances.
Example 4:
Input: template = "Literal {{ brace.", variables = {"name": "Bob"}
Output: "Literal {{ brace."
Explanation: The placeholder `{{ brace.` is not a valid format and is treated as literal text.
Constraints
- The template string can be of any length.
- The dictionary of variables can be of any length.
- Variable names within the placeholders should consist of alphanumeric characters and underscores.
- The rendering process should be reasonably efficient for templates of moderate size (up to a few thousand characters). While extreme optimization isn't required, avoid excessively inefficient string manipulation.
Notes
Consider using Python's string manipulation methods (e.g., replace, split, regular expressions) to implement the template engine. A regular expression might be helpful for identifying and replacing the placeholders. Remember to handle edge cases carefully to ensure the engine behaves as expected in all scenarios. Focus on clarity and readability in your code. Don't overcomplicate the solution; a simple and correct implementation is preferred.