Skip to content

Commit 4495dd8

Browse files
authored
Merge pull request #1055 from cakephp/feat-template-cleanup
Update templates to use line stripping tags
2 parents ad3962d + 9cb606b commit 4495dd8

File tree

12 files changed

+177
-209
lines changed

12 files changed

+177
-209
lines changed

templates/bake/Controller/controller.twig

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
{% endif %}
3333

3434
{%- for component in components %}
35-
{% set classInfo = Bake.classInfo(component, 'Controller/Component', 'Component') %}
35+
{%~ set classInfo = Bake.classInfo(component, 'Controller/Component', 'Component') %}
3636
* @property {{ classInfo.fqn }} ${{ classInfo.name }}
3737
{% endfor %}
3838
*/
@@ -48,20 +48,20 @@ class {{ name }}Controller extends AppController
4848
{
4949
parent::initialize();
5050

51-
{% for component in components %}
51+
{%~ for component in components %}
5252
$this->loadComponent('{{ component }}');
53-
{% endfor %}
54-
{% if helpers %}
53+
{%~ endfor %}
54+
{%~ if helpers %}
5555
$this->viewBuilder()->setHelpers({{ Bake.exportArray(helpers)|raw }});
56-
{% endif %}
57-
{% if has_login %}
56+
{%~ endif %}
57+
{%~ if has_login %}
5858
$this->Authentication->allowUnauthenticated(['login']);
59-
{% endif %}
59+
{%~ endif %}
6060
}
61-
{% if actions|length %}{{ "\n" }}{% endif %}
61+
{%~ if actions|length %}{{ "\n" }}{% endif %}
6262
{% endif %}
63-
{%- for action in actions %}
64-
{% if loop.index > 1 %}{{ "\n" }}{% endif %}
63+
{% for action in actions %}
64+
{%~ if loop.index > 1 %}{{ "\n" }}{% endif %}
6565
{{- element('Bake.Controller/' ~ action) -}}
6666
{% endfor %}
6767
}

templates/bake/Model/entity.twig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
{% set annotations = DocBlock.propertyHints(propertyHintMap) %}
1919

2020
{%- if associationHintMap %}
21-
{%- set annotations = annotations|merge(['']) %}
22-
{%- set annotations = annotations|merge(DocBlock.propertyHints(associationHintMap)) %}
21+
{%~ set annotations = annotations|merge(['']) %}
22+
{%~ set annotations = annotations|merge(DocBlock.propertyHints(associationHintMap)) %}
2323
{% endif %}
2424

2525
{%- set accessible = Bake.getFieldAccessibility(fields, primaryKey) %}
@@ -39,7 +39,7 @@ class {{ name }} extends Entity{{ fileBuilder.classBuilder.implements ? ' implem
3939

4040
{% endif %}
4141
{% if accessible %}
42-
{%- set generatedProperties = generatedProperties|merge(['_accessible']) %}
42+
{%~ set generatedProperties = generatedProperties|merge(['_accessible']) %}
4343
/**
4444
* Fields that can be mass assigned using newEntity() or patchEntity().
4545
*
@@ -54,8 +54,8 @@ class {{ name }} extends Entity{{ fileBuilder.classBuilder.implements ? ' implem
5454
{% if accessible and hidden %}
5555

5656
{% endif %}
57-
{%- if hidden %}
58-
{%- set generatedProperties = generatedProperties|merge(['_hidden']) %}
57+
{% if hidden %}
58+
{%~ set generatedProperties = generatedProperties|merge(['_hidden']) %}
5959
/**
6060
* Fields that are excluded from JSON versions of the entity.
6161
*

templates/bake/Model/table.twig

Lines changed: 45 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -50,58 +50,43 @@ class {{ name }}Table extends Table{{ fileBuilder.classBuilder.implements ? ' im
5050
{%- if displayField %}
5151
$this->setDisplayField({{ (displayField is iterable ? Bake.exportArray(displayField) : Bake.exportVar(displayField))|raw }});
5252
{% endif %}
53-
54-
{%- if primaryKey %}
55-
{%- if primaryKey is iterable and primaryKey|length > 1 %}
53+
{% if primaryKey %}
54+
{%~ if primaryKey is iterable and primaryKey|length > 1 %}
5655
$this->setPrimaryKey({{ Bake.exportArray(primaryKey)|raw }});
57-
{{- "\n" }}
58-
{%- else %}
56+
{%~ else %}
5957
$this->setPrimaryKey('{{ primaryKey|as_array|first }}');
60-
{{- "\n" }}
61-
{%- endif %}
58+
{%~ endif %}
6259
{% endif %}
60+
{% if enums %}
6361

64-
{%- if enums %}
65-
66-
{% endif %}
67-
68-
{%- if enums %}
69-
70-
{%- for name, className in enums %}
62+
{%~ for name, className in enums %}
7163
$this->getSchema()->setColumnType('{{ name }}', \Cake\Database\Type\EnumType::from(\{{ className }}::class));
72-
{% endfor %}
64+
{%~ endfor %}
7365
{% endif %}
66+
{% if behaviors %}
7467

75-
{%- if behaviors %}
76-
77-
{% endif %}
78-
79-
{%- for behavior, behaviorData in behaviors %}
68+
{%~ for behavior, behaviorData in behaviors %}
8069
$this->addBehavior('{{ behavior }}'{{ (behaviorData ? (", " ~ Bake.exportArray(behaviorData, 2)|raw ~ '') : '')|raw }});
81-
{% endfor %}
82-
83-
{%- if associations.belongsTo or associations.hasMany or associations.belongsToMany %}
84-
70+
{%~ endfor %}
8571
{% endif %}
86-
87-
{%- for type, assocs in associations %}
88-
{%- for assoc in assocs %}
89-
{%- set assocData = [] %}
90-
{%- for key, val in assoc %}
91-
{%- if key is not same as('alias') %}
92-
{%- set assocData = assocData|merge({(key): val}) %}
93-
{%- endif %}
94-
{%- endfor %}
72+
{% if associations.belongsTo or associations.hasMany or associations.belongsToMany %}
73+
74+
{%~ for type, assocs in associations %}
75+
{%~ for assoc in assocs %}
76+
{%~ set assocData = [] %}
77+
{%~ for key, val in assoc %}
78+
{%~ if key is not same as('alias') %}
79+
{%~ set assocData = assocData|merge({(key): val}) %}
80+
{%~ endif %}
81+
{%~ endfor %}
9582
$this->{{ type }}('{{ assoc.alias }}', {{ Bake.exportArray(assocData, 2)|raw }});
96-
{{- "\n" }}
97-
{%- endfor %}
98-
{% endfor %}
83+
{%~ endfor %}
84+
{%~ endfor %}
85+
{% endif %}
9986
}
100-
{{- "\n" }}
101-
102-
{%- if validation %}
103-
{% set generatedFunctions = generatedFunctions|merge(['validationDefault']) %}
87+
{% if validation %}
10488

89+
{%~ set generatedFunctions = generatedFunctions|merge(['validationDefault']) %}
10590
/**
10691
* Default validation rules.
10792
*
@@ -110,25 +95,24 @@ class {{ name }}Table extends Table{{ fileBuilder.classBuilder.implements ? ' im
11095
*/
11196
public function validationDefault(Validator $validator): Validator
11297
{
113-
{% for field, rules in validation %}
114-
{% set validationMethods = Bake.getValidationMethods(field, rules) %}
115-
{% if validationMethods %}
98+
{%~ for field, rules in validation %}
99+
{%~ set validationMethods = Bake.getValidationMethods(field, rules) %}
100+
{%~ if validationMethods %}
116101
$validator
117-
{% for validationMethod in validationMethods %}
118-
{% if loop.last %}
119-
{% set validationMethod = validationMethod ~ ';' %}
120-
{% endif %}
102+
{%~ for validationMethod in validationMethods %}
103+
{%~ if loop.last %}
104+
{%~ set validationMethod = validationMethod ~ ';' %}
105+
{%~ endif %}
121106
{{ validationMethod|raw }}
122-
{% endfor %}
107+
{%~ endfor %}
123108

124-
{% endif %}
125-
{% endfor %}
109+
{%~ endif %}
110+
{%~ endfor %}
126111
return $validator;
127112
}
128113
{% endif %}
129-
130114
{%- if rulesChecker %}
131-
{% set generatedFunctions = generatedFunctions|merge(['buildRules']) %}
115+
{%~ set generatedFunctions = generatedFunctions|merge(['buildRules']) %}
132116

133117
/**
134118
* Returns a rules checker object that will be used for validating
@@ -139,21 +123,20 @@ class {{ name }}Table extends Table{{ fileBuilder.classBuilder.implements ? ' im
139123
*/
140124
public function buildRules(RulesChecker $rules): RulesChecker
141125
{
142-
{% for rule in rulesChecker %}
143-
{% set fields = Bake.exportArray(rule.fields) %}
144-
{% set options = '' %}
145-
{% for optionName, optionValue in rule.options %}
146-
{%~ set options = (loop.first ? '[' : options) ~ "'#{optionName}' => " ~ Bake.exportVar(optionValue) ~ (loop.last ? ']' : ', ') %}
147-
{% endfor %}
126+
{%~ for rule in rulesChecker %}
127+
{%~ set fields = Bake.exportArray(rule.fields) %}
128+
{%~ set options = '' %}
129+
{%~ for optionName, optionValue in rule.options %}
130+
{%~ set options = (loop.first ? '[' : options) ~ "'#{optionName}' => " ~ Bake.exportVar(optionValue) ~ (loop.last ? ']' : ', ') %}
131+
{%~ endfor %}
148132
$rules->add($rules->{{ rule.name }}({{ fields|raw }}{{ (rule.extra|default ? ", '#{rule.extra}'" : '')|raw }}{{ (options ? ', ' ~ options : '')|raw }}), ['errorField' => '{{ rule.fields[0] }}']);
149-
{% endfor %}
133+
{%~ endfor %}
150134

151135
return $rules;
152136
}
153137
{% endif %}
154-
155-
{%- if connection is not same as('default') %}
156-
{% set generatedFunctions = generatedFunctions|merge(['defaultConnectionName']) %}
138+
{% if connection is not same as('default') %}
139+
{%~ set generatedFunctions = generatedFunctions|merge(['defaultConnectionName']) %}
157140

158141
/**
159142
* Returns the database connection name to use by default.

templates/bake/Template/add.twig

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,15 @@
1717
/**
1818
* @var \{{ namespace }}\View\AppView $this
1919
* @var \{{ entityClass }} ${{ singularVar }}
20-
{{- "\n" }}
21-
{%- if associations.BelongsTo is defined %}
22-
{%- for assocName, assocData in associations.BelongsTo %}
20+
{% if associations.BelongsTo is defined %}
21+
{%~ for assocName, assocData in associations.BelongsTo %}
2322
* @var \Cake\Collection\CollectionInterface|string[] ${{ assocData.variable }}
24-
{{- "\n" }}
25-
{%- endfor %}
23+
{%~ endfor %}
2624
{% endif %}
27-
{%- if associations.BelongsToMany is defined %}
28-
{%- for assocName, assocData in associations.BelongsToMany %}
25+
{% if associations.BelongsToMany is defined %}
26+
{%~ for assocName, assocData in associations.BelongsToMany %}
2927
* @var \Cake\Collection\CollectionInterface|string[] ${{ assocData.variable }}
30-
{{- "\n" }}
31-
{%- endfor %}
28+
{%~ endfor %}
3229
{% endif %}
3330
*/
3431
?>

templates/bake/Template/edit.twig

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,15 @@
1717
/**
1818
* @var \{{ namespace }}\View\AppView $this
1919
* @var \{{ entityClass }} ${{ singularVar }}
20-
{{- "\n" }}
21-
{%- if associations.BelongsTo is defined %}
22-
{%- for assocName, assocData in associations.BelongsTo %}
20+
{% if associations.BelongsTo is defined %}
21+
{%~ for assocName, assocData in associations.BelongsTo %}
2322
* @var string[]|\Cake\Collection\CollectionInterface ${{ assocData.variable }}
24-
{{- "\n" }}
25-
{%- endfor %}
23+
{%~ endfor %}
2624
{% endif %}
27-
{%- if associations.BelongsToMany is defined %}
28-
{%- for assocName, assocData in associations.BelongsToMany %}
25+
{% if associations.BelongsToMany is defined %}
26+
{%~ for assocName, assocData in associations.BelongsToMany %}
2927
* @var string[]|\Cake\Collection\CollectionInterface ${{ assocData.variable }}
30-
{{- "\n" }}
31-
{%- endfor %}
28+
{%~ endfor %}
3229
{% endif %}
3330
*/
3431
?>

templates/bake/Template/index.twig

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,28 @@
3838
<?php foreach (${{ pluralVar }} as ${{ singularVar }}): ?>
3939
<tr>
4040
{% for field in fields %}
41-
{% set isKey = false %}
42-
{% if associations.BelongsTo is defined %}
43-
{% for alias, details in associations.BelongsTo %}
44-
{% if field == details.foreignKey %}
45-
{% set isKey = true %}
41+
{%~ set isKey = false %}
42+
{%~ if associations.BelongsTo is defined %}
43+
{%~ for alias, details in associations.BelongsTo %}
44+
{%~ if field == details.foreignKey %}
45+
{%~ set isKey = true %}
4646
<td><?= ${{ singularVar }}->hasValue('{{ details.property }}') ? $this->Html->link(${{ singularVar }}->{{ details.property }}->{{ details.displayField }}, ['controller' => '{{ details.controller }}', 'action' => 'view', ${{ singularVar }}->{{ details.property }}->{{ details.primaryKey[0] }}]) : '' ?></td>
47-
{% endif %}
48-
{% endfor %}
49-
{% endif %}
50-
{% if isKey is not same as(true) %}
51-
{% set columnData = Bake.columnData(field, schema) %}
52-
{% set supportsLabel = Bake.enumSupportsLabel(field, schema) %}
53-
{% if columnData.type starts with 'enum-' %}
47+
{%~ endif %}
48+
{%~ endfor %}
49+
{%~ endif %}
50+
{%~ if isKey is not same as(true) %}
51+
{%~ set columnData = Bake.columnData(field, schema) %}
52+
{%~ set supportsLabel = Bake.enumSupportsLabel(field, schema) %}
53+
{%~ if columnData.type starts with 'enum-' %}
5454
<td><?= ${{ singularVar }}->{{ field }} === null ? '' : h(${{ singularVar }}->{{ field }}->{% if supportsLabel %}label(){% else %}value{% endif %}) ?></td>
55-
{% elseif columnData.type not in ['integer', 'float', 'decimal', 'biginteger', 'smallinteger', 'tinyinteger'] %}
55+
{%~ elseif columnData.type not in ['integer', 'float', 'decimal', 'biginteger', 'smallinteger', 'tinyinteger'] %}
5656
<td><?= h(${{ singularVar }}->{{ field }}) ?></td>
57-
{% elseif columnData.null %}
57+
{%~ elseif columnData.null %}
5858
<td><?= ${{ singularVar }}->{{ field }} === null ? '' : $this->Number->format(${{ singularVar }}->{{ field }}) ?></td>
59-
{% else %}
59+
{%~ else %}
6060
<td><?= $this->Number->format(${{ singularVar }}->{{ field }}) ?></td>
61-
{% endif %}
62-
{% endif %}
61+
{%~ endif %}
62+
{%~ endif %}
6363
{% endfor %}
6464
{% set pk = '$' ~ singularVar ~ '->' ~ primaryKey[0] %}
6565
<td class="actions">

0 commit comments

Comments
 (0)