Skip to content

feat: add support for template variables in prompt files through vars parameter#98

Merged
FidelusAleksander merged 6 commits into
mainfrom
nunjucks-templating
Jul 11, 2025
Merged

feat: add support for template variables in prompt files through vars parameter#98
FidelusAleksander merged 6 commits into
mainfrom
nunjucks-templating

Conversation

@FidelusAleksander

Copy link
Copy Markdown
Owner

This PR adds dynamic prompt templating support using Nunjucks templates and YAML variables, making the action much more flexible and reusable across different contexts.

✨ New Features

Template Variables Support

  • New vars input parameter accepts YAML-formatted variables
  • Templates are processed for all prompt inputs: prompt, prompt-file, system-prompt, system-prompt-file
  • Full backward compatibility - existing workflows continue working without modification

Template Syntax

  • Variable substitution: {{ variable }}
  • Conditional logic: {% if condition %}...{% endif %}
  • Loops: {% for item in items %}...{% endfor %}
  • Filters: {{ text | upper }}, {{ items | join(', ') }}
  • Nested objects: {{ user.name }}, {{ config.database.host }}

📋 Examples

Basic Variable Substitution

- uses: FidelusAleksander/prompt-action@v1
  with:
    prompt: "You are a {{ language }} expert translator. Translate {{ text }} to {{ target_language }}"
    vars: |
      language: Spanish
      target_language: English
      text: "Hola mundo"

GitHub Workflow Context Integration

- uses: FidelusAleksander/prompt-action@v1
  with:
    prompt: |
      Generate a PR description for {{ repo_name }}.
      Branch: {{ branch }}
      Author: {{ author }}
      Changes: {{ files_count }} files
    vars: |
      repo_name: ${{ github.repository }}
      branch: ${{ github.head_ref }}
      author: ${{ github.actor }}
      files_count: ${{ steps.count-files.outputs.count }}

Template Files with Loops and Conditionals

# .github/prompts/code-review.md
You are a {{ language }} expert specializing in {{ framework }}.
{% if include_tests %}
Please include testing recommendations.
{% endif %}

Review focus areas:
{% for focus in review_focus %}
- {{ focus }}
{% endfor %}

Code: {{ code }}
- uses: FidelusAleksander/prompt-action@v1
  with:
    prompt-file: .github/prompts/code-review.md
    vars: |
      language: TypeScript
      framework: React
      include_tests: true
      review_focus:
        - performance
        - security
        - best practices
      code: ${{ steps.get-code.outputs.content }}

Fixes #77.

Copilot AI and others added 4 commits July 8, 2025 21:34
Co-authored-by: FidelusAleksander <63016446+FidelusAleksander@users.noreply.github.com>
Co-authored-by: FidelusAleksander <63016446+FidelusAleksander@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 11, 2025 18:52
@github-actions github-actions Bot added documentation Improvements or additions to documentation enhancement New feature or improvement maintenance Maintenance around the repository labels Jul 11, 2025

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds dynamic prompt templating support so users can supply a vars YAML block and have Nunjucks templates rendered for both prompt and system-prompt inputs while preserving backward compatibility.

  • Introduce processTemplate() helper using js-yaml and Nunjucks
  • Hook template processing into run() for prompt/prompt-file and system-prompt/system-prompt-file
  • Update metadata, dependencies, tests, and docs to cover the new vars input

Reviewed Changes

Copilot reviewed 10 out of 12 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/template.ts New processTemplate function parses YAML and renders via Nunjucks
src/main.ts Calls processTemplate on both prompt and system-prompt inputs
package.json Added js-yaml, nunjucks, and their type definitions
action.yml Declared optional vars input
README.md Documented templating feature with examples
tests/template.test.ts Unit tests covering variable substitution, loops, conditionals, filters, and errors
tests/main.test.ts Integration tests for templating in action run
tests/prompts/country-capital-prompt.md Sample prompt template for country-capital test
tests/country-capital-response.json JSON schema for the country-capital example
.github/workflows/test-action.yml Updated test workflow to exercise templating and schema validation
Comments suppressed due to low confidence (2)

README.md:110

  • The YAML snippet under "Basic Variable Substitution" is invalid: the prompt key should use a pipe (|) for a multi-line string rather than a quoted scalar on the next line. Update to:
prompt: |
  You are a {{ language }} expert translator. Translate the following text to {{ target_language }}: {{ text }}
    prompt:

tests/main.test.ts:642

  • There is no test covering system-prompt-file templating with vars. Consider adding a test that mocks system-prompt-file and verifies it is rendered with the provided variables.
    it('should process template with prompt file', async () => {

Comment thread src/template.ts
@FidelusAleksander FidelusAleksander removed documentation Improvements or additions to documentation maintenance Maintenance around the repository labels Jul 11, 2025
@FidelusAleksander FidelusAleksander merged commit dc910e0 into main Jul 11, 2025
5 checks passed
@FidelusAleksander FidelusAleksander deleted the nunjucks-templating branch July 11, 2025 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Nunjucks templating support with vars parameter

3 participants