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
Copy file name to clipboardExpand all lines: docs/code-api/generator.md
+79-80Lines changed: 79 additions & 80 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,129 +7,128 @@ parent: API
7
7
8
8
# `Generator` class
9
9
10
-
This code provides a `Generator` class for parsing, generating, and manipulating PHP files. It handles various PHP constructs such as namespaces, classes, interfaces, traits, and more. Additionally, it can detect reserved keywords and make names safe for use in generated code.
10
+
{: .warning }
11
+
> This page documents the modern `Generator` class for parsing templates of any file format. For legacy PHP generation from JSON/YAML/NEON templates, see the `ClassicGenerator` class.
12
+
13
+
The `Generator` class provides functionality for parsing modern templates (any file format) and generating files. It uses [Pars'Em](https://github.com/matronator/parsem) for template parsing and variable substitution.
`Matronator\Mtrgen\FileObject` is the main data structure for the parsed templates. It contains all the information about the file that is needed for writing it to the disk.
30
+
`Matronator\Mtrgen\GenericFileObject` is the main data structure for parsed modern templates. It contains all the information about the file that is needed for writing it to the disk.
32
31
33
32
```php
34
-
class FileObject
35
-
{
36
-
public PhpFile $contents;
37
-
38
-
public string $filename;
33
+
namespace Matronator\Mtrgen;
39
34
40
-
public string $directory;
41
-
42
-
public ?string $entity = null;
35
+
class GenericFileObject
36
+
{
37
+
public string $contents; // The file contents as a string
38
+
public string $filename; // The output filename (including extension)
39
+
public string $directory; // The output directory path
43
40
44
-
public function __construct(string $directory, string $filename, PhpFile $contents, ?string $entity = null) {
45
-
$this->filename = $filename . '.php';
41
+
public function __construct(string $directory, string $filename, string $contents) {
42
+
$this->filename = $filename;
46
43
$this->contents = $contents;
47
44
$this->directory = $directory;
48
-
$this->entity = $entity;
49
45
}
50
46
}
51
47
```
52
48
53
-
### PhpFile
54
-
55
-
`Nette\PhpGenerator\PhpFile` is a class from the [Nette](https://nette.org)[PHP Generator library](https://github.com/nette/php-generator). It is used to represent a PHP file and its contents. It is used by the `Generator` class for generating the parsed templates.
56
-
57
-
Constants
58
-
---------
59
-
60
-
-`RESERVED_KEYWORDS`: An array of reserved keywords in PHP.
61
-
-`RESERVED_CONSTANTS`: An array of reserved constants in PHP.
Copy file name to clipboardExpand all lines: docs/index.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ title: Introduction
7
7
8
8

9
9
10
-
File generator engine that can generate PHPfiles from JSON/YAML/NEON templates.
10
+
File generator engine that can generate files of any format from templates. Templates can be any file format (JavaScript, PHP, TypeScript, Python, etc.) and will generate files in the same format. MTRGen also supports legacy JSON/YAML/NEON templates for generating PHP files.
vendor/bin/mtrgen gen -p my/folder/template.php.mtr
54
54
55
55
# Generate from the local store
56
56
vendor/bin/mtrgen generate TemplateName
57
57
58
58
# Save a template to the local store
59
-
vendor/bin/mtrgen save path/to/template.json
60
-
vendor/bin/mtrgen s path/to/template.json
59
+
vendor/bin/mtrgen save path/to/template.js.mtr
60
+
vendor/bin/mtrgen s path/to/template.php.mtr
61
61
62
62
# Optionally provide an alias to save the template under
63
-
vendor/bin/mtrgen save path/to/template.json --alias=NewName
63
+
vendor/bin/mtrgen save path/to/template.js.mtr --alias=NewName
64
64
65
65
# Remove a template from the local store
66
66
vendor/bin/mtrgen remove TemplateName
@@ -69,7 +69,7 @@ vendor/bin/mtrgen r TemplateName
69
69
70
70
## Acknowledgement
71
71
72
-
This project would not be possible without [Nette](https://nette.org)'s [`php-generator`](https://github.com/nette/php-generator) package, which is used for the final code generation itself to output the finished PHP file.
72
+
This project uses [Pars'Em](https://github.com/matronator/parsem) for template parsing and variable substitution. For legacy PHP file generation, it uses [Nette](https://nette.org)'s [`php-generator`](https://github.com/nette/php-generator) package.
If you just want to generate a file from a template in the online registry one-time only and don't want to add it to your local store, you can use the `use` command. This command works just like the `generate` command, except it takes an identifier as an argument and searches the online registry instead of your local store.
33
33
34
-
When it finds the template, it will prompt you to provide arguments for the template (if there are any) and generates the PHP file without saving the template in your local store. This is useful if you just want to use a template one time and know you won't be needing it in the future again.
34
+
When it finds the template, it will prompt you to provide arguments for the template (if there are any) and generates the file without saving the template in your local store. This is useful if you just want to use a template one time and know you won't be needing it in the future again. The generated file format depends on the template (modern templates can generate any format, legacy templates generate PHP files).
> You can publish templates of any format (modern templates) or legacy JSON/YAML/NEON templates. The template format doesn't matter - as long as it has a valid header, it can be published.
74
+
72
75
Alternatively if you want to publish a template you already have saved in your local store, just provide the template name instead of the `--path` option, like this:
Copy file name to clipboardExpand all lines: docs/templates/index.md
+39-7Lines changed: 39 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,10 +9,14 @@ has_children: true
9
9
10
10
## Introduction
11
11
12
-
Generator templates (or just templates) describe the structure of the generated file. Templates can be either YAML, JSON or NEON files and they have to conform to a JSON schema. We will talk more about the schema in the next chapter (*see [mtrgen-template-schema.json](template-structure.md#mtrgen-template-schema)*).
12
+
Generator templates (or just templates) describe the structure of the generated file. MTRGen supports two types of templates:
13
+
14
+
1.**Modern templates**: Any file format (JavaScript, PHP, TypeScript, Python, etc.) that can generate files in the same format. These templates use a header block to define metadata and use template variables for dynamic content.
15
+
16
+
2.**Legacy templates**: JSON/YAML/NEON files that generate PHP files. These templates conform to a JSON schema and are used for generating PHP classes, interfaces, and traits. (*See [Template Structure](template-structure.md) for more details on legacy templates.*)
13
17
14
18
{: .tip }
15
-
> It is recommended to name your templates with a `.mtr`suffix after the name right after the extension (eg. `entity.yaml.mtr`). If you do that, the [MTRGen Templates Syntax Highlighting](https://marketplace.visualstudio.com/items?itemName=matronator.mtrgen-yaml-templates) VSCode extension, will add syntax highlighting and snippets to all`*.mtr` files.
19
+
> It is recommended to name your templates with a `.mtr` extension (eg. `component.js.mtr`, `entity.php.mtr`). The `.mtr` extension helps identify template files and enables syntax highlighting in editors. The [MTRGen Templates Syntax Highlighting](https://marketplace.visualstudio.com/items?itemName=matronator.mtrgen-yaml-templates) VSCode extension provides syntax highlighting and snippets for`*.mtr` files.
16
20
17
21
## Template syntax
18
22
@@ -27,15 +31,18 @@ The header block looks like this:
27
31
```
28
32
--- MTRGEN ---
29
33
name: template-name
30
-
filename: <% name|firstUpper %>.php
31
-
path: ./app/Controllers
32
-
--- MTRGEN ---
34
+
filename: <% name|upperFirst %>.js
35
+
path: ./src/components
36
+
--- /MTRGEN ---
33
37
34
-
// Rest of the template
38
+
// Rest of the template content
35
39
```
36
40
37
41
You can use template variables for the header values, except for the `name` field, which must be known beforehand to be able to correctly save and use the template later.
38
42
43
+
{: .note }
44
+
> The output filename is defined in the `filename` field of the header. The generated file will have exactly the name you specify here, including the extension. For example, if your template is `component.js.mtr` and the header specifies `filename: <% name %>.js`, the generated file will be `MyComponent.js` (assuming `name` is "MyComponent").
45
+
39
46
#### Header fields
40
47
41
48
##### `name`
@@ -44,7 +51,7 @@ A unique name of the template to be saved under in the local store. If published
44
51
45
52
##### `filename`
46
53
47
-
The filename of the generated file. Can use template variables to make the filename dynamic.
54
+
The filename of the generated file, including the extension. Can use template variables to make the filename dynamic. The generated file will have exactly this name - for example, if you specify `filename: <% name %>.js`, the output will be a JavaScript file with that name.
48
55
49
56
##### `path`
50
57
@@ -154,6 +161,31 @@ There are a few built-in filters that you can use:
154
161
155
162
`truncate`- Truncates the variable to the specified length
156
163
164
+
## Examples
165
+
166
+
### Modern Template Example
167
+
168
+
Here's an example of a modern JavaScript template (`component.js.mtr`):
When you generate this template with arguments like `name=Button event=click id=btn1 className=ACTIVE`, it will create `Button.js` in the `src/components` directory with the variables replaced.
184
+
185
+
### Legacy Template Example
186
+
187
+
Legacy templates are JSON/YAML/NEON files that generate PHP files. See the [Template Structure](template-structure.md) page for details on legacy template format.
188
+
157
189
## VSCode Extension
158
190
159
191
To get syntax highlighting for template files (highlight/colorize `<% variable|filter %>` and `<% if %><% endif %>` even inside strings) and some helper snippets (like `---` to insert the template header), you can download the [MTRGen Templates Syntax Highlighting](https://marketplace.visualstudio.com/items?itemName=matronator.mtrgen-yaml-templates) extension for VS Code.
Copy file name to clipboardExpand all lines: docs/templates/template-structure.md
+7-1Lines changed: 7 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,13 @@ parent: Templates
7
7
8
8
# Template structure
9
9
10
-
As mentioned earlier, the template parser supports YAML, JSON and NEON files, so you can write your templates using any of these formats. All templates must follow the same structure, which is defined in a JSON schema. You can find the full schema here: [mtrgen-template-schema.json](https://www.mtrgen.com/storage/schemas/template/latest/mtrgen-template-schema.json)
10
+
{: .warning }
11
+
> This page describes **legacy templates** - JSON/YAML/NEON files that generate PHP files. For modern templates (any file format), see the [Templates Overview](../templates/index.md).
12
+
13
+
Legacy templates are YAML, JSON or NEON files that generate PHP files. All legacy templates must follow the same structure, which is defined in a JSON schema. You can find the full schema here: [mtrgen-template-schema.json](https://www.mtrgen.com/storage/schemas/template/latest/mtrgen-template-schema.json)
14
+
15
+
{: .note }
16
+
> Modern templates (any file format) don't use this schema. They simply use a header block and template variables directly in the file content. See the [Templates Overview](../templates/index.md) for more information.
11
17
12
18
Here is a simplified version of the schema in YAML format for better readability.
0 commit comments