You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: docs/source/templates.md
+65-4Lines changed: 65 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,12 +39,73 @@ Custom templates can be defined through the use of either pure Jinja2 templates
39
39
40
40
### Templates defined via mx.ini with Jinja2
41
41
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
44
55
```
45
56
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
+
46
65
### Templates based on Python code
47
66
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
+
classMyTemplate(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
+
deftemplate_variables(self) -> dict:
83
+
return {
84
+
"project_name": "myproject",
85
+
"custom_value": "example"
86
+
}
50
87
```
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
+
classCustomConf(MxIniBoundTemplate):
103
+
# Access mx.ini settings via self.settings
104
+
@property
105
+
deftemplate_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