Google Closure Soy Templates for Python
closure-soy provides a Python wrapper for Google Closure's Soy template compiler and renderer. It allows Python applications to compile .soy files to JavaScript and render Soy templates. The library is very old, with its last release being `20121221`, and is no longer maintained.
Common errors
-
ImportError: No module named closure_soy
cause The library is either not installed, or you are running a Python version (likely Python 3) with which it is incompatible.fixEnsure `pip install closure-soy` was successful. Verify you are running Python 2.x (e.g., `python --version`). If on Python 3, this library will not work; consider alternatives. -
SyntaxError: invalid syntax (on Python 3.x)
cause The `closure-soy` codebase contains Python 2.x specific syntax which is invalid in Python 3.x, leading to a `SyntaxError` during import or execution.fixThis library is not compatible with Python 3.x. Use a Python 2.x interpreter or choose a modern Python 3 compatible templating engine. -
FileNotFoundError: [Errno 2] No such file or directory: 'java'
cause The Python wrapper tries to invoke the `java` command to run the Soy compiler, but the Java Runtime Environment (JRE) is either not installed or not in your system's PATH.fixInstall Java (JRE) on your system and ensure the `java` executable is available in your system's PATH. On Linux/macOS, check with `java -version`. -
OSError: Cannot find the Soy compiler JAR file at the specified path.
cause The path provided for `soy_jar_path` when initializing `Renderer` or `CompileToJs` is incorrect, or the file does not exist.fixVerify that the `soy_jar_path` variable or environment variable points to the correct, existing Soy compiler JAR file.
Warnings
- breaking This library was developed for Python 2.x and is incompatible with Python 3. Attempting to use it on Python 3 will result in `ImportError` or `SyntaxError`.
- gotcha The `closure-soy` library is merely a Python wrapper for the Java-based Closure Soy Compiler. You must have a Java Runtime Environment (JRE) installed and available in your system's PATH, and you need to provide the file path to the Soy compiler JAR file for the library to function.
- deprecated The project is abandoned, with the last release in 2012 and no further maintenance. It's not suitable for new projects or modern Python environments.
Install
-
pip install closure-soy
Imports
- Renderer
from closure_soy import Renderer
- CompileToJs
from closure_soy.compiler import CompileToJs
Quickstart
import os
from closure_soy import Renderer
from closure_soy.compiler import CompileToJs
# NOTE: This library is a Python 2.x era wrapper for the Java-based Closure Soy Compiler.
# To run this code, you MUST have Java installed and available in your PATH,
# and you MUST provide the path to the Closure Soy Compiler JAR file.
# You will also need actual .soy template files.
# Placeholder for the Soy compiler JAR path (e.g., soy-compiler.jar)
SOY_JAR_PATH = os.environ.get('SOY_JAR_PATH', '/path/to/soy-compiler.jar')
# Placeholder for your .soy template files (e.g., ['path/to/my_templates.soy'])
TEMPLATE_FILES = [f for f in os.environ.get('SOY_TEMPLATE_FILES', '').split(',') if f]
if not TEMPLATE_FILES:
TEMPLATE_FILES = ['/path/to/example.soy'] # Replace with a valid .soy file path
# Example 1: Compiling Soy templates to JavaScript
try:
# output_dir = '/tmp/js_templates'
# os.makedirs(output_dir, exist_ok=True) # Python 3 syntax
# CompileToJs(SOY_JAR_PATH, TEMPLATE_FILES, output_dir)
# print(f"Soy templates compiled to {output_dir}")
print("To compile: Uncomment and set SOY_JAR_PATH, TEMPLATE_FILES, and output_dir.")
print("Example: CompileToJs(SOY_JAR_PATH, TEMPLATE_FILES, '/tmp/js_templates')")
except Exception as e:
print(f"Failed to compile templates (expected if setup is not complete): {e}")
# Example 2: Rendering a Soy template
try:
renderer = Renderer(SOY_JAR_PATH, TEMPLATE_FILES)
# Assuming 'my.template.name' is a template defined in one of TEMPLATE_FILES
# and 'data' is a dictionary matching the template's parameters.
# rendered_html = renderer.render('my.template.name', {'param': 'value'})
# print(f"Rendered HTML: {rendered_html}")
print("To render: Uncomment and set SOY_JAR_PATH, TEMPLATE_FILES. Provide a template name and data.")
print("Example: renderer.render('my.template.name', {'param': 'value'})")
except Exception as e:
print(f"Failed to render template (expected if setup is not complete): {e}")