wuhhh / template-genie
A Craft CMS plugin for scaffolding Twig templates based on CMS setup with field-aware variable definitions
Installs: 13
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 2
Type:craft-plugin
pkg:composer/wuhhh/template-genie
Requires
- php: ^8.2
- craftcms/cms: ^5.0.0
This package is auto-updated.
Last update: 2025-12-15 17:03:41 UTC
README
A Craft CMS plugin that scaffolds Twig templates and partials based on your CMS structure, with intelligent field-aware variable definitions and sensible defaults.
What It Does
Template Genie analyzes your Craft CMS setup and generates:
- Section templates - Entry point templates that delegate rendering to partials
- Entry type partials - Field-aware templates with proper variable assignments
- Matrix/Content Block partials - Nested element templates for complex fields
- Smart HTML output - Optional semantic HTML rendering for rapid prototyping
The plugin understands all 29 native Craft CMS field types (100% coverage) and generates appropriate variable assignments, null coalescing patterns, and helpful context comments.
Key Features
- Complete field support - All native Craft 5 fields including Content Block
- Smart variable assignment - Automatically handles
.one()vs.all()based on relation limits - Context-aware comments - Field constraints, options, and limits included inline
- Layout integration - Apply Twig layouts during generation
- Interactive mode - User-friendly prompts for configuration
- Selective generation - Generate templates only, partials only, or specific sections
- Safe overwrite handling - Prompts before overwriting existing files
Installation
This plugin is available via Packagist:
composer require wuhhh/template-genie
./craft plugin/install template-genie && ./craft plugin/enable template-genie
Basic Usage
Interactive Mode (Recommended)
Simply run the command with no options for an interactive setup wizard:
./craft template-genie/generate
The interactive mode will guide you through:
- Section selection
- Template vs partial generation
- Layout file selection
- Output style preferences
- Overwrite behavior
Command Line Options
Generate templates and partials for specific sections:
# Generate everything for specific sections ./craft template-genie/generate --sections=blog,news # Generate only section templates ./craft template-genie/generate --sections=blog --templates-only # Generate only partials ./craft template-genie/generate --sections=blog --partials-only # Force overwrite without prompts ./craft template-genie/generate --sections=blog -f # Generate all sections ./craft template-genie/generate --all
Advanced Options
# Apply a layout file during generation ./craft template-genie/generate --sections=blog --layout-file=_layouts/base.twig # Specify custom block name (default: 'content') ./craft template-genie/generate --sections=blog --layout-file=_layouts/base.twig --block-name=main # Generate with HTML output for rapid prototyping ./craft template-genie/generate --sections=blog --output-style=full --with-output # Wrap entry type partials in semantic elements ./craft template-genie/generate --sections=blog --wrap-entry-type
Output Styles
Template Genie supports different output styles:
Minimal (Default)
Generates only variable assignments with context comments:
{# Blog Post #} {% set title = entry.title ?? null %} {% set featuredImage = entry.featuredImage.one() ?? null %} {% set body = entry.body ?? null %}
Full
Includes semantic HTML rendering for rapid prototyping:
{# Blog Post #} {% set title = entry.title ?? null %} {% set featuredImage = entry.featuredImage.one() ?? null %} {% set body = entry.body ?? null %} <article> <h1>{{ title }}</h1> {% if featuredImage %} <img src="{{ featuredImage.url }}" alt="{{ featuredImage.title }}"> {% endif %} <div>{{ body }}</div> </article>
How It Works
Section Templates
For sections with defined template paths, Template Genie generates entry point templates that delegate to partials:
{# templates/blog/_entry.twig #} {{ entry.render() }}
Entry Type Partials
Partials are generated at _partials/entry/{entryTypeHandle}.twig with field-aware variables:
{# _partials/entry/blogPost.twig #} {# Blog Post #} {% set title = entry.title ?? null %} {% set author = entry.author.one() ?? null %} {% set categories = entry.categories.all() ?? [] %} {% set featuredImage = entry.featuredImage.one() ?? null %} {% set body = entry.body ?? null %} {% set publishDate = entry.publishDate ?? null %}
Smart Field Handling
The plugin detects field configuration and generates appropriate patterns:
Single vs Multi Relations:
{# Single relation (maxRelations = 1) #} {% set featuredImage = entry.featuredImage.one() ?? null %} {# Multi relation (maxRelations > 1 or null) #} {% set gallery = entry.gallery.all() ?? [] %}
Field Constraints:
{# Product description (160 char limit, no line breaks) #} {% set description = entry.description ?? null %} {# Gallery images (min 2, max 8 selections) #} {% set galleryImages = entry.galleryImages.all() ?? [] %}
Field Options:
{# Background color (options: aquamarine|turquoise|teal|whitesmoke) #} {% set backgroundColor = entry.backgroundColor.value ?? null %}
Field Support
Template Genie supports all 29 native Craft CMS field types with intelligent handling:
- Addresses, Assets, Button Group, Categories, Checkboxes
- Color, Content Block, Country, Date, Dropdown
- Email, Entries, Icon, JSON, Lightswitch
- Link, Matrix, Money, Multi-select, Number
- Plain Text, Radio Buttons, Range, Table, Tags
- Time, Users
Each field type receives appropriate:
- Variable assignment pattern
- Null coalescing behavior
- Context comments with constraints
- Optional HTML rendering
Tips
- Use
.render()- Generated partials work automatically with Craft's render delegation - Matrix fields - Matrix block partials are auto-generated, use
{{ block.render() }} - Layouts - Apply layouts during generation to wrap templates in your site structure
- Force mode - Use
-fflag in CI/CD pipelines to skip prompts - Sections only - Skip sections without template paths (valid for Channel sections)
Requirements
- Craft CMS ^5.0.0
- PHP ^8.2
License
Proprietary - Commercial plugin for Craft CMS
Support
- Documentation: https://github.com/wuhhh/template-genie
- Issues: https://github.com/wuhhh/template-genie/issues