Skip to content

Commit aca0644

Browse files
committed
Complete templates.md documentation
Replaced TODO sections with practical examples: - Templates via mx.ini: Configuration, available built-in templates - Python-based templates: Class structure, @template decorator, MxIniBoundTemplate for mx.ini integration Added code examples showing how to create custom templates.
1 parent b2e8539 commit aca0644

File tree

1 file changed

+65
-4
lines changed

1 file changed

+65
-4
lines changed

docs/source/templates.md

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,73 @@ Custom templates can be defined through the use of either pure Jinja2 templates
3939

4040
### Templates defined via mx.ini with Jinja2
4141

42-
```{todo}
43-
Contribute to this documentation!
42+
Some templates are configured and triggered via the `mx.ini` file. The most common example is test and coverage scripts.
43+
44+
**Configuration in mx.ini**:
45+
46+
```ini
47+
[settings]
48+
mxmake-templates =
49+
run-tests
50+
run-coverage
51+
52+
# Optional: Configure template-specific settings
53+
mxmake-test-path = src
54+
mxmake-source-path = src/mypackage
4455
```
4556

57+
**Available built-in templates**:
58+
- `run-tests` - Generate test runner script
59+
- `run-coverage` - Generate coverage script
60+
- `pip-conf` - Generate pip configuration
61+
- `plone-site` - Generate Plone site creation script
62+
63+
These templates are generated automatically when you run `make install` or `make mxfiles`.
64+
4665
### Templates based on Python code
4766

48-
```{todo}
49-
Contribute to this documentation!
67+
For advanced use cases, you can create templates using Python code. This provides full control over template generation and allows integration with the mxmake/mxdev environment.
68+
69+
**Basic template class structure**:
70+
71+
```python
72+
from mxmake.templates import template, Template
73+
74+
@template("my-template")
75+
class MyTemplate(Template):
76+
description: str = "My custom template"
77+
target_name: str = "output.txt"
78+
template_name: str = "my-template.j2"
79+
target_folder = Path("custom")
80+
81+
@property
82+
def template_variables(self) -> dict:
83+
return {
84+
"project_name": "myproject",
85+
"custom_value": "example"
86+
}
5087
```
88+
89+
**Key components**:
90+
- `@template("name")` - Register the template with a unique name
91+
- `target_name` - Filename of the generated file
92+
- `template_name` - Name of the Jinja2 template file in `src/mxmake/templates/`
93+
- `template_variables` - Dictionary of variables available in the template
94+
- `target_folder` - Directory where the file will be generated
95+
96+
**Using MxIniBoundTemplate** for mx.ini integration:
97+
98+
```python
99+
from mxmake.templates import template, MxIniBoundTemplate
100+
101+
@template("custom-conf")
102+
class CustomConf(MxIniBoundTemplate):
103+
# Access mx.ini settings via self.settings
104+
@property
105+
def template_variables(self) -> dict:
106+
return {
107+
"packages": self.settings.get("main-package", "")
108+
}
109+
```
110+
111+
For complete examples, see the [templates.py source code](https://github.com/mxstack/mxmake/blob/main/src/mxmake/templates.py).

0 commit comments

Comments
 (0)