Skip to content

Commit

Permalink
Merge pull request #396 from FriendsOfCake/5.x-fix-bake-templates
Browse files Browse the repository at this point in the history
5.x - Some bake/template fixes.
  • Loading branch information
ndm2 authored Oct 24, 2023
2 parents 61ae882 + d90d192 commit 32105f2
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 9 deletions.
6 changes: 4 additions & 2 deletions templates/bake/Template/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,20 @@
<tr>
{% for field in fields %}
{% set isKey = false %}
{% if associations.BelongsTo %}
{% if associations.BelongsTo is defined %}
{% for alias, details in associations.BelongsTo %}
{% if field == details.foreignKey %}
{% set isKey = true %}
<td><?= ${{ singularVar }}->has('{{ details.property }}') ? $this->Html->link(${{ singularVar }}->{{ details.property }}->{{ details.displayField }}, ['controller' => '{{ details.controller }}', 'action' => 'view', ${{ singularVar }}->{{ details.property }}->{{ details.primaryKey[0] }}]) : '' ?></td>
<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>
{% endif %}
{% endfor %}
{% endif %}
{% if isKey is not same as(true) %}
{% set columnData = Bake.columnData(field, schema) %}
{% if columnData.type not in ['integer', 'float', 'decimal', 'biginteger', 'smallinteger', 'tinyinteger'] %}
<td><?= h(${{ singularVar }}->{{ field }}) ?></td>
{% elseif columnData.null %}
<td><?= ${{ singularVar }}->{{ field }} === null ? '' : $this->Number->format(${{ singularVar }}->{{ field }}) ?></td>
{% else %}
<td><?= $this->Number->format(${{ singularVar }}->{{ field }}) ?></td>
{% endif %}
Expand Down
11 changes: 8 additions & 3 deletions templates/bake/Template/view.twig
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
<table class="table table-striped">
{% if groupedFields['string'] %}
{% for field in groupedFields['string'] %}
{% if associationFields[field] %}
{% if associationFields[field] is defined %}
{% set details = associationFields[field] %}
<tr>
<th scope="row"><?= __('{{ details.property|humanize }}') ?></th>
<td><?= ${{ singularVar }}->has('{{ details.property }}') ? $this->Html->link(${{ singularVar }}->{{ details.property }}->{{ details.displayField }}, ['controller' => '{{ details.controller }}', 'action' => 'view', ${{ singularVar }}->{{ details.property }}->{{ details.primaryKey[0] }}]) : '' ?></td>
<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>
</tr>
{% else %}
<tr>
Expand All @@ -53,15 +53,20 @@
{% for alias, details in associations.HasOne %}
<tr>
<th scope="row"><?= __('{{ alias|underscore|singularize|humanize }}') ?></th>
<td><?= ${{ singularVar }}->has('{{ details.property }}') ? $this->Html->link(${{ singularVar }}->{{ details.property }}->{{ details.displayField }}, ['controller' => '{{ details.controller }}', 'action' => 'view', ${{ singularVar }}->{{ details.property }}->{{ details.primaryKey[0] }}]) : '' ?></td>
<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>
</tr>
{% endfor %}
{% endif %}
{% if groupedFields.number %}
{% for field in groupedFields.number %}
<tr>
<th scope="row"><?= __('{{ field|humanize }}') ?></th>
{% set columnData = Bake.columnData(field, schema) %}
{% if columnData.null %}
<td><?= ${{ singularVar }}->{{ field }} === null ? '' : $this->Number->format(${{ singularVar }}->{{ field }}) ?></td>
{% else %}
<td><?= $this->Number->format(${{ singularVar }}->{{ field }}) ?></td>
{% endif %}
</tr>
{% endfor %}
{% endif %}
Expand Down
4 changes: 2 additions & 2 deletions templates/bake/element/form.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<?php
{% for field in fields %}
{%- if field not in primaryKey %}
{%- if keyFields[field] %}
{%- if keyFields[field] is defined %}
{%- set fieldData = Bake.columnData(field, schema) %}
{%- if fieldData.null %}
echo $this->Form->control('{{ field }}', ['options' => ${{ keyFields[field] }}, 'empty' => true]);
Expand All @@ -27,7 +27,7 @@
{%- endif %}
{%- endfor %}
{%- if associations.BelongsToMany %}
{%- if associations.BelongsToMany is defined %}
{%- for assocName, assocData in associations.BelongsToMany %}
echo $this->Form->control('{{ assocData.property }}._ids', ['options' => ${{ assocData.variable }}]);
{{- "\n" }}
Expand Down
3 changes: 3 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
date_default_timezone_set('UTC');
mb_internal_encoding('UTF-8');

// Enable strict_variables Twig configuration
Configure::write('Bake.twigStrictVariables', true);

Configure::write('debug', true);
Configure::write('App', [
'namespace' => 'TestApp',
Expand Down
1 change: 1 addition & 0 deletions tests/comparisons/Template/testBakeAdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
echo $this->Form->control('author_id', ['options' => $authors, 'empty' => true]);
echo $this->Form->control('title');
echo $this->Form->control('body');
echo $this->Form->control('rating');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
Expand Down
1 change: 1 addition & 0 deletions tests/comparisons/Template/testBakeEdit.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
echo $this->Form->control('author_id', ['options' => $authors, 'empty' => true]);
echo $this->Form->control('title');
echo $this->Form->control('body');
echo $this->Form->control('rating');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
Expand Down
4 changes: 3 additions & 1 deletion tests/comparisons/Template/testBakeIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@
<th scope="col"><?= $this->Paginator->sort('id') ?></th>
<th scope="col"><?= $this->Paginator->sort('author_id') ?></th>
<th scope="col"><?= $this->Paginator->sort('title') ?></th>
<th scope="col"><?= $this->Paginator->sort('rating') ?></th>
<th scope="col" class="actions"><?= __('Actions') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($articles as $article) : ?>
<tr>
<td><?= $this->Number->format($article->id) ?></td>
<td><?= $article->has('author') ? $this->Html->link($article->author->name, ['controller' => 'Authors', 'action' => 'view', $article->author->id]) : '' ?></td>
<td><?= $article->hasValue('author') ? $this->Html->link($article->author->name, ['controller' => 'Authors', 'action' => 'view', $article->author->id]) : '' ?></td>
<td><?= h($article->title) ?></td>
<td><?= $article->rating === null ? '' : $this->Number->format($article->rating) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', $article->id], ['title' => __('View'), 'class' => 'btn btn-secondary']) ?>
<?= $this->Html->link(__('Edit'), ['action' => 'edit', $article->id], ['title' => __('Edit'), 'class' => 'btn btn-secondary']) ?>
Expand Down
6 changes: 5 additions & 1 deletion tests/comparisons/Template/testBakeView.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<table class="table table-striped">
<tr>
<th scope="row"><?= __('Author') ?></th>
<td><?= $article->has('author') ? $this->Html->link($article->author->name, ['controller' => 'Authors', 'action' => 'view', $article->author->id]) : '' ?></td>
<td><?= $article->hasValue('author') ? $this->Html->link($article->author->name, ['controller' => 'Authors', 'action' => 'view', $article->author->id]) : '' ?></td>
</tr>
<tr>
<th scope="row"><?= __('Title') ?></th>
Expand All @@ -32,6 +32,10 @@
<th scope="row"><?= __('Id') ?></th>
<td><?= $this->Number->format($article->id) ?></td>
</tr>
<tr>
<th scope="row"><?= __('Rating') ?></th>
<td><?= $article->rating === null ? '' : $this->Number->format($article->rating) ?></td>
</tr>
</table>
</div>
<div class="text">
Expand Down
1 change: 1 addition & 0 deletions tests/schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
'author_id' => ['type' => 'integer', 'null' => true],
'title' => ['type' => 'string', 'null' => true],
'body' => 'text',
'rating' => ['type' => 'integer', 'null' => true],
],
'constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
],
Expand Down

0 comments on commit 32105f2

Please sign in to comment.