Google Closure Soy Templates for Python

20121221 · abandoned · verified Fri Apr 17

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the API for compiling and rendering Soy templates using `closure-soy`. Note that this library acts as a wrapper around the Java-based Soy compiler. You must have Java installed, provide the path to the Soy compiler JAR, and point to your `.soy` template files for this code to function correctly.

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}")

view raw JSON →