README
¶
Miya Engine Feature Examples
This directory contains comprehensive, organized examples of all Miya Engine features. Each feature group has its own folder with a template file and a corresponding Go program that demonstrates the features.
Directory Structure
examples/features/
├── inheritance/ - Template inheritance (extends, blocks, super)
├── control-structures/ - Control flow (if, for, set, with)
├── filters/ - All 73+ built-in filters
├── macros-includes/ - Macros, includes, and imports
├── comprehensions/ - List and dictionary comprehensions
├── advanced/ - Filter blocks, do statements, whitespace control
├── tests-operators/ - Test expressions and operators
└── global-functions/ - Global functions (range, dict, cycler, etc.)
Quick Start
Each example folder contains:
- template.html or child.html - Template demonstrating the features
- main.go - Go program to run the example
- Additional support files (base templates, includes, etc.)
Running an Example
# Navigate to any feature folder
cd examples/features/inheritance
# Run the Go program
go run main.go
Feature Groups
1. Template Inheritance (inheritance/)
Features Demonstrated:
- Template extension with
{% extends %} - Block definition and override
- Super calls with
{{ super() }} - Multi-level inheritance
- Block name resolution
- Nested block content
Files:
base.html- Base template with blockschild.html- Child template extending basemain.go- Demonstration program
Run:
cd inheritance && go run main.go
2. Control Structures (control-structures/)
Features Demonstrated:
- Conditionals: if/elif/else, inline ternary
- Loops: for loops with all loop variables (index, first, last, etc.)
- Conditional iteration:
{% for item in items if condition %} - Variable assignment:
{% set %} - Scoping:
{% with %} - Dictionary iteration:
{% for key, value in dict %} - Nested loops and conditions
Files:
template.html- Control structures showcasemain.go- Demonstration program
Run:
cd control-structures && go run main.go
3. Filters (filters/)
Features Demonstrated: All 73+ built-in filters organized by category:
- String (16+): upper, lower, capitalize, title, trim, replace, truncate, center, wordcount, split, startswith, endswith, contains, slugify, indent, wordwrap
- Collection (15+): first, last, length, join, sort, reverse, unique, slice, batch, map, select, reject, selectattr, rejectattr, groupby
- Numeric (10): abs, round, int, float, sum, min, max, pow, ceil, floor
- HTML/Security (7+): escape, safe, striptags, urlencode, urlize, forceescape
- Utility (8+): default, format, tojson, filesizeformat, dictsort, attr, pprint
- Filter chaining:
{{ value|filter1|filter2|filter3 }} - Filters with arguments
- Filters in conditionals and loops
Files:
template.html- Complete filter showcasemain.go- Demonstration program
Run:
cd filters && go run main.go
4. Macros & Includes (macros-includes/)
Features Demonstrated:
-
Macros:
- Basic macro definition
- Default parameters
- Macros with logic
- Call blocks with
{% call %} - Caller function
{{ caller() }} - Nested macro calls
-
Imports:
- Namespace import:
{% import "file.html" as name %} - Selective import:
{% from "file.html" import macro1, macro2 %}
- Namespace import:
-
Includes:
- Template inclusion:
{% include "template.html" %} - Automatic context passing
- Template inclusion:
Files:
macros.html- Macro libraryincludes/header.html- Header includeincludes/footer.html- Footer includetemplate.html- Main template using macros and includesmain.go- Demonstration program
Run:
cd macros-includes && go run main.go
5. Comprehensions (comprehensions/)
Features Demonstrated:
-
List comprehensions:
- Basic:
[x * 2 for x in numbers] - With conditions:
[x for x in items if x.active] - With filters:
[name|upper for name in names] - Nested comprehensions
- Basic:
-
Dictionary comprehensions:
- Basic:
{user.id: user.name for user in users} - With conditions:
{k: v for k, v in dict if condition} - Transform keys/values
- Basic:
-
Integration:
- In conditionals
- In loops
- With additional filters
- Complex expressions
Files:
template.html- Comprehensions showcasemain.go- Demonstration program
Run:
cd comprehensions && go run main.go
6. Advanced Features (advanced/)
Features Demonstrated:
-
Filter Blocks:
- Single filter:
{% filter upper %}...{% endfilter %} - Chained filters:
{% filter trim|upper|replace(...) %} - Nested filter blocks
- Single filter:
-
Do Statements:
- Execute without output:
{% do expression %} - With filters:
{% do value|filter %}
- Execute without output:
-
Whitespace Control:
- Left strip:
{%- statement %} - Right strip:
{% statement -%} - Both sides:
{%- statement -%}
- Left strip:
-
Raw Blocks:
- Escape template syntax:
{% raw %}...{% endraw %}
- Escape template syntax:
-
Autoescape:
- Control HTML escaping
- Safe and escape filters
-
Environment Configuration:
- AutoEscape, StrictUndefined, TrimBlocks, LstripBlocks
Files:
template.html- Advanced features showcasemain.go- Demonstration program
Run:
cd advanced && go run main.go
7. Tests & Operators (tests-operators/)
Features Demonstrated:
-
Operators (19):
- Arithmetic: +, -, *, /, //, %, **, ~
- Comparison: ==, !=, <, <=, >, >=
- Logical: and, or, not
- Membership: in, not in
-
Tests (26+):
- Type tests: defined, undefined, none, boolean, string, number, integer, float
- Container tests: sequence, mapping, iterable, callable
- Numeric tests: even, odd, divisibleby
- String tests: lower, upper, startswith, endswith, match, alpha, alnum
- Comparison tests: equalto, sameas, in, contains
- Negated tests: All tests support
is not
Files:
template.html- Tests and operators showcasemain.go- Demonstration program
Run:
cd tests-operators && go run main.go
8. Global Functions (global-functions/)
Features Demonstrated:
- range() - Number sequences
- dict() - Dictionary constructor
- cycler() - Cycle through values
- joiner() - Smart joining
- namespace() - Mutable container for loops
- lipsum() - Lorem ipsum generator
- zip() - Combine sequences
- enumerate() - Iterate with index
- url_for() - URL generation
Files:
template.html- Global functions showcasemain.go- Demonstration program
Run:
cd global-functions && go run main.go
Feature Coverage
Complete Feature List
| Category | Features | Count | Status |
|---|---|---|---|
| Template Inheritance | extends, blocks, super | 3 | 100% |
| Control Structures | if/elif/else, for, set, with | 4+ | 100% |
| Filters | String, Collection, Numeric, HTML, Utility | 73+ | 100% |
| Macros & Includes | macros, call, import, include | 4 | 90% |
| Comprehensions | List, Dict | 2 | 100% |
| Advanced Features | Filter blocks, do, whitespace, raw, autoescape | 5 | 100% |
| Tests | Type, Container, Numeric, String, Comparison | 26+ | 95% |
| Operators | Arithmetic, Comparison, Logical, Membership | 19 | 100% |
| Global Functions | range, dict, cycler, joiner, etc. | 9 | 100% |
Overall Compatibility: 95.4% with Jinja2
Learning Path
For Beginners
- Start with control-structures - Learn basic flow control
- Move to filters - Understand data transformation
- Try template inheritance - Learn template organization
For Intermediate Users
- Explore macros-includes - Reusable components
- Study comprehensions - Advanced data processing
- Review tests-operators - Complex conditions
For Advanced Users
- Master advanced - Filter blocks, whitespace control
- Utilize global-functions - Helper functions
- Build real applications combining all features
Common Patterns
Pattern 1: Reusable Layout
base.html (inheritance/)
└── page.html extends base
└── Override blocks as needed
Pattern 2: Component Library
macros.html (macros-includes/)
└── Define reusable UI components
└── Import in templates
Pattern 3: Data Processing
Use comprehensions + filters for data transformation
{{ [user.name|upper for user in users if user.active]|join(", ") }}
Tips
- Start Simple: Run each example as-is before modifying
- Read Comments: Template files contain inline documentation
- Check Output: Compare expected vs actual output
- Experiment: Modify context data in main.go to see different results
- Combine Features: Most features work together seamlessly
Troubleshooting
Template Not Found
- Ensure you're in the correct directory when running
- Loader looks for templates relative to current directory
Import Errors
- Check that
github.com/zipreport/miyais installed - Run
go mod tidyif needed
Rendering Errors
- Check the error message for line numbers
- Verify your template syntax
- Ensure all variables in context are defined
Additional Resources
- Main README:
../../README.md- Project overview - Documentation:
../../docs/- Detailed feature documentation - Other Examples:
../../examples/go/- More complex examples
Contributing
Want to add more examples? Feel free to:
- Create a new feature folder
- Add template and Go program
- Update this README
- Submit a pull request
License
Copyright 2024 Joao Pinheiro
MIT License - see the LICENSE file in the project root for details.