Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove model support from HtmlTemplate #2171

Merged
merged 4 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions docs/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ following commands:

```
$template = HtmlTemplate::addTo($this);

$template->loadFromString('Hello, {name}world{/}');
```

Expand Down Expand Up @@ -320,7 +319,6 @@ Example:

```
$template = HtmlTemplate::addTo($this);

$template->loadFromString('Hello, {name}world{/}');

$template->set('name', 'John');
Expand Down
1 change: 0 additions & 1 deletion src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,6 @@ public function run(): void
public function loadTemplate(string $filename)
{
$template = new $this->templateClass();
$template->setApp($this);

if ((['.' => true, '/' => true, '\\' => true][substr($filename, 0, 1)] ?? false) || str_contains($filename, ':\\')) {
return $template->loadFromFile($filename);
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Control/Lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public function outputApiResponse(): void
*
* @param int|bool $limit
*
* @return array<int, array{value: string, title: mixed}>
* @return list<array{value: string, title: mixed}>
*/
public function getData($limit = true): array
{
Expand Down
2 changes: 2 additions & 0 deletions src/Form/Control/ScopeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,8 @@ public function scopeToQuery(Scope\AbstractScope $scope, array $inputsMap = []):

/**
* Converts a Condition to VueQueryBuilder query array.
*
* @return array{rule: string, operator: string, value: string|null, option: array|null}
*/
public function conditionToQuery(Condition $condition, array $inputsMap = []): array
{
Expand Down
65 changes: 20 additions & 45 deletions src/HtmlTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace Atk4\Ui;

use Atk4\Core\AppScopeTrait;
use Atk4\Core\WarnDynamicPropertyTrait;
use Atk4\Data\Model;
use Atk4\Ui\HtmlTemplate\TagTree;
use Atk4\Ui\HtmlTemplate\Value as HtmlValue;

Expand All @@ -15,7 +13,6 @@
*/
class HtmlTemplate
{
use AppScopeTrait;
use WarnDynamicPropertyTrait;

public const TOP_TAG = '_top';
Expand Down Expand Up @@ -107,10 +104,6 @@ public function cloneRegion(string $tag): self
// TODO prune unreachable nodes
// $template->rebuildTagsIndex();

if ($this->issetApp()) {
$template->setApp($this->getApp());
}

return $template;
}

Expand Down Expand Up @@ -141,31 +134,13 @@ protected function emptyTagTree(TagTree $tagTree): void
*
* If tag contains another tag trees, these tag trees are emptied.
*
* @param string|array<string, string>|Model $tag
* @param ($tag is array|Model ? never : string|null) $value
* @param string|array<string, string> $tag
* @param ($tag is array ? never : string|null) $value
*/
protected function _setOrAppend($tag, string $value = null, bool $encodeHtml = true, bool $append = false, bool $throwIfNotFound = true): void
{
if ($tag instanceof Model && $value === null) {
if (!$encodeHtml) {
throw new Exception('HTML is not allowed to be dangerously set from Model');
}

// $tag passed as model
// in this case we don't throw exception if tags don't exist
$uiPersistence = $this->getApp()->uiPersistence;
foreach ($tag->getFields() as $k => $field) {
if ($this->_hasTag($k)) {
$v = $uiPersistence->typecastSaveField($field, $tag->get($k));
$this->_setOrAppend($k, $v, $encodeHtml, $append);
}
}

return;
}

// $tag passed as associative array [tag => value]
if (is_array($tag) && $value === null) {
if (is_array($tag) && $value === null) { // @phpstan-ignore-line
if ($throwIfNotFound) {
foreach ($tag as $k => $v) {
if (!$this->_hasTag($k)) {
Expand All @@ -182,7 +157,7 @@ protected function _setOrAppend($tag, string $value = null, bool $encodeHtml = t
}

if (!is_string($tag) || $tag === '') {
throw (new Exception('Tag must be non-empty string'))
throw (new Exception('Tag must be ' . (is_string($tag) ? 'non-empty ' : '') . 'string'))
->addMoreInfo('tag', $tag)
->addMoreInfo('value', $value);
}
Expand Down Expand Up @@ -216,8 +191,8 @@ protected function _setOrAppend($tag, string $value = null, bool $encodeHtml = t
* If tag is found inside template several times, all occurrences are
* replaced.
*
* @param string|array<string, string>|Model $tag
* @param ($tag is array|Model ? never : string|null) $value
* @param string|array<string, string> $tag
* @param ($tag is array ? never : string|null) $value
*
* @return $this
*/
Expand All @@ -232,8 +207,8 @@ public function set($tag, string $value = null): self
* Same as set(), but won't generate exception for non-existing
* $tag.
*
* @param string|array<string, string>|Model $tag
* @param ($tag is array|Model ? never : string|null) $value
* @param string|array<string, string> $tag
* @param ($tag is array ? never : string|null) $value
*
* @return $this
*/
Expand All @@ -248,8 +223,8 @@ public function trySet($tag, string $value = null): self
* Set value of a tag to a HTML content. The value is set without
* encoding, so you must be sure to sanitize.
*
* @param string|array<string, string>|Model $tag
* @param ($tag is array|Model ? never : string|null) $value
* @param string|array<string, string> $tag
* @param ($tag is array ? never : string|null) $value
*
* @return $this
*/
Expand All @@ -264,8 +239,8 @@ public function dangerouslySetHtml($tag, string $value = null): self
* See dangerouslySetHtml() but won't generate exception for non-existing
* $tag.
*
* @param string|array<string, string>|Model $tag
* @param ($tag is array|Model ? never : string|null) $value
* @param string|array<string, string> $tag
* @param ($tag is array ? never : string|null) $value
*
* @return $this
*/
Expand All @@ -279,8 +254,8 @@ public function tryDangerouslySetHtml($tag, string $value = null): self
/**
* Add more content inside a tag.
*
* @param string|array<string, string>|Model $tag
* @param ($tag is array|Model ? never : string|null) $value
* @param string|array<string, string> $tag
* @param ($tag is array ? never : string|null) $value
*
* @return $this
*/
Expand All @@ -295,8 +270,8 @@ public function append($tag, ?string $value): self
* Same as append(), but won't generate exception for non-existing
* $tag.
*
* @param string|array<string, string>|Model $tag
* @param ($tag is array|Model ? never : string|null) $value
* @param string|array<string, string> $tag
* @param ($tag is array ? never : string|null) $value
*
* @return $this
*/
Expand All @@ -311,8 +286,8 @@ public function tryAppend($tag, ?string $value): self
* Add more content inside a tag. The content is appended without
* encoding, so you must be sure to sanitize.
*
* @param string|array<string, string>|Model $tag
* @param ($tag is array|Model ? never : string|null) $value
* @param string|array<string, string> $tag
* @param ($tag is array ? never : string|null) $value
*
* @return $this
*/
Expand All @@ -327,8 +302,8 @@ public function dangerouslyAppendHtml($tag, ?string $value): self
* Same as dangerouslyAppendHtml(), but won't generate exception for non-existing
* $tag.
*
* @param string|array<string, string>|Model $tag
* @param ($tag is array|Model ? never : string|null) $value
* @param string|array<string, string> $tag
* @param ($tag is array ? never : string|null) $value
*
* @return $this
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Lister.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ protected function renderView(): void
*/
public function renderRow(): void
{
$this->tRow->trySet($this->currentRow);
$this->tRow->trySet($this->getApp()->uiPersistence->typecastSaveRow($this->currentRow, $this->currentRow->get()));

if ($this->tRow->hasTag('_title')) {
$this->tRow->set('_title', $this->currentRow->getTitle());
Expand Down
3 changes: 1 addition & 2 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,6 @@ protected function renderView(): void
$this->tRowMaster->dangerouslySetHtml('cells', $this->getDataRowHtml());
$this->tRowMaster->set('dataId', '{$dataId}');
$this->tRow = new HtmlTemplate($this->tRowMaster->renderToHtml()); // TODO reparse should not be needed
$this->tRow->setApp($this->getApp());

if ($this->hook(self::HOOK_BEFORE_ROW) === false) {
continue;
Expand Down Expand Up @@ -444,7 +443,7 @@ protected function renderView(): void
#[\Override]
public function renderRow(): void
{
$this->tRow->set($this->currentRow);
$this->tRow->trySet($this->getApp()->uiPersistence->typecastSaveRow($this->currentRow, $this->currentRow->get()));

if ($this->useHtmlTags) {
// prepare row-specific HTML tags
Expand Down
5 changes: 3 additions & 2 deletions src/Table/Column/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ protected function init(): void

if (is_string($this->url)) {
$this->url = new HtmlTemplate($this->url);
$this->url->setApp($this->getApp());
}
if (is_string($this->page)) {
$this->page = [$this->page];
Expand Down Expand Up @@ -145,7 +144,9 @@ public function getDataCellTemplate(Field $field = null): string
public function getHtmlTags(Model $row, ?Field $field): array
{
if ($this->url) {
return ['c_' . $this->shortName => $this->url->set($row)->renderToHtml()];
$this->url->trySet($this->getApp()->uiPersistence->typecastSaveRow($row, $row->get()));

return ['c_' . $this->shortName => $this->url->renderToHtml()];
}

$page = $this->page ?? [];
Expand Down
3 changes: 1 addition & 2 deletions src/Table/Column/Multiformat.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public function getHtmlTags(Model $row, ?Field $field): array
}

$template = new HtmlTemplate($cellHtml);
$template->setApp($this->getApp());
$template->set($row);
$template->trySet($this->getApp()->uiPersistence->typecastSaveRow($row, $row->get()));
$template->dangerouslySetHtml($htmlTags);

$val = $template->renderToHtml();
Expand Down
4 changes: 0 additions & 4 deletions src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,6 @@ protected function init(): void
}
}

if ($this->template !== null && (!$this->template->issetApp() || $this->template->getApp() !== $app)) {
$this->template->setApp($app);
}

foreach ($addLater as [$object, $region]) {
$this->add($object, $region);
}
Expand Down
27 changes: 0 additions & 27 deletions tests/HtmlTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Atk4\Ui\Tests;

use Atk4\Core\Phpunit\TestCase;
use Atk4\Data\Model;
use Atk4\Ui\Exception;
use Atk4\Ui\HtmlTemplate;

Expand Down Expand Up @@ -183,23 +182,6 @@ public function testParseDollarTags(): void
self::assertSameTemplate('{foo}Hello{/} guys and {bar}welcome{/} here', $t);
}

public function testSetFromEntity(): void
{
$model = new Model();
$model->addField('foo');
$model->addField('bar');
$model->addField('baz');
$entity = $model->createEntity();
$entity->set('foo', 'Hello');
$entity->set('bar', '<br>');
$entity->set('baz', 'not in template');

$t = new HtmlTemplate('{$foo} {$bar}');
$t->setApp($this->createApp());
$t->set($entity);
self::assertSameTemplate('{foo}Hello{/foo} {bar}&lt;br&gt;{/bar}', $t);
}

public function testSetFromArray(): void
{
$t = new HtmlTemplate('{$foo} {$bar}');
Expand Down Expand Up @@ -231,15 +213,6 @@ public function testTagNotDefinedFromArrayException(): void
}
}

public function testSetHtmlFromEntityException(): void
{
$t = new HtmlTemplate();

$this->expectException(Exception::class);
$this->expectExceptionMessage('HTML is not allowed to be dangerously set from Model');
$t->dangerouslySetHtml(new Model());
}

public function testSetEmptyTagException(): void
{
$t = new HtmlTemplate();
Expand Down