Skip to main content

Template Service

Template service for rendering Jinja2 templates in spec-kit

Overview

This module provides the main template service implementation that coordinates all template operations by delegating to specialized service modules.

Classes

JinjaTemplateService

Jinja2-based template service implementation that delegates to specialized modules

Inherits from: TemplateService

Methods

compare_templates
compare_templates(self, project_path: pathlib.Path, new_template_dir: Optional[pathlib.Path] = None, selected_agents: Optional[List[str]] = None) -> specify_cli.services.template_service.models.TemplateDiff

Compare current project files with templates using comparator module

Parameters:

  • project_path (Path, required)
  • new_template_dir (Optional, optional)
  • selected_agents (Optional, optional)

Returns: TemplateDiff

discover_templates
discover_templates(self) -> List[specify_cli.models.template.GranularTemplate]

Discover templates using discovery module

Returns: List

discover_templates_by_category
discover_templates_by_category(self, category: str) -> List[specify_cli.models.template.GranularTemplate]

Filter templates by category using discovery module

Parameters:

  • category (str, required)

Returns: List

enhance_context_with_platform_info
enhance_context_with_platform_info(self, context: specify_cli.models.project.TemplateContext, platform_name: str) -> specify_cli.models.project.TemplateContext

Enhance template context with platform-specific information.

Parameters:

  • context (TemplateContext, required)
  • platform_name (str, required)

Returns: TemplateContext

get_template_variables
get_template_variables(self, template_path: pathlib.Path) -> List[str]

Extract variables used in template using validator module

Parameters:

  • template_path (Path, required)

Returns: List

load_template
load_template(self, template_name: str) -> specify_cli.models.template.GranularTemplate

Load individual template object

Parameters:

  • template_name (str, required)

Returns: GranularTemplate

load_template_package
load_template_package(self, ai_assistant: str, template_dir: pathlib.Path) -> bool

Load template package for specified AI assistant

Parameters:

  • ai_assistant (str, required)
  • template_dir (Path, required)

Returns: bool

load_templates_from_package_resources
load_templates_from_package_resources(self) -> bool

Load templates from package resources

Returns: bool

render_all_templates_from_mappings
render_all_templates_from_mappings(self, folder_mappings: List[specify_cli.services.template_service.models.TemplateFolderMapping], context: specify_cli.models.project.TemplateContext, verbose: bool = False) -> specify_cli.services.template_service.models.RenderResult

Render all templates based on dynamic folder mappings

Parameters:

  • folder_mappings (List, required)
  • context (TemplateContext, required)
  • verbose (bool, optional) - Default: False

Returns: RenderResult

render_project_templates
render_project_templates(self, context: specify_cli.models.project.TemplateContext, output_dir: pathlib.Path) -> List[specify_cli.models.project.TemplateFile]

Render all templates in the loaded package

Parameters:

  • context (TemplateContext, required)
  • output_dir (Path, required)

Returns: List

render_template
render_template(self, template_name: Union[str, specify_cli.models.template.GranularTemplate], context: Optional[specify_cli.models.project.TemplateContext]) -> str

Render a specific template with given context

Parameters:

  • template_name (Union, required)
  • context (Optional, required)

Returns: str

render_template_package
render_template_package(self, package: specify_cli.models.template.TemplatePackage, context: specify_cli.models.project.TemplateContext) -> List[specify_cli.services.template_service.models.TemplateRenderResult]

Render full template package

Parameters:

  • package (TemplatePackage, required)
  • context (TemplateContext, required)

Returns: List

render_templates
render_templates(self, templates_path: pathlib.Path, destination_path: pathlib.Path, ai_assistants: List[str], project_name: str, branch_pattern: str, selected_agents: Optional[List[str]] = None) -> specify_cli.services.template_service.models.RenderResult

Render templates from a given path for multiple AI assistants

Parameters:

  • templates_path (Path, required)
  • destination_path (Path, required)
  • ai_assistants (List, required)
  • project_name (str, required)
  • branch_pattern (str, required)
  • selected_agents (Optional, optional)

Returns: RenderResult

render_with_platform_context
render_with_platform_context(self, template: specify_cli.models.template.GranularTemplate, context: specify_cli.models.project.TemplateContext) -> str

Render template with platform-specific context variables

Parameters:

  • template (GranularTemplate, required)
  • context (TemplateContext, required)

Returns: str

set_custom_template_dir
set_custom_template_dir(self, template_dir: Optional[pathlib.Path]) -> bool

Set custom template directory

Parameters:

  • template_dir (Optional, required)

Returns: bool

validate_template_package
validate_template_package(self, package: specify_cli.models.template.TemplatePackage) -> bool

Validate template package

Parameters:

  • package (TemplatePackage, required)

Returns: bool

validate_template_syntax
validate_template_syntax(self, template_path: pathlib.Path) -> Tuple[bool, Optional[str]]

Validate template syntax using validator module

Parameters:

  • template_path (Path, required)

Returns: Tuple

Functions

get_template_service

get_template_service() -> specify_cli.services.template_service.template_service.JinjaTemplateService

Factory function to create a JinjaTemplateService instance

Returns: JinjaTemplateService