Skip to content

Commit

Permalink
Rename View::render() method to View::renderToHtml()
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Apr 5, 2024
1 parent b308ff5 commit b88fc6c
Show file tree
Hide file tree
Showing 18 changed files with 47 additions and 47 deletions.
2 changes: 1 addition & 1 deletion demos/basic/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
$plane = View::addTo($app, ['template' => $planeTemplate]);

Header::addTo($app, ['Can be rendered into HTML']);
View::addTo($app, ['ui' => 'segment', 'class.raised' => true, 'element' => 'pre'])->set($plane->render());
View::addTo($app, ['ui' => 'segment', 'class.raised' => true, 'element' => 'pre'])->set($plane->renderToHtml());

Header::addTo($app, ['Has a unique global identifier']);
Label::addTo($app, ['Plane ID:', 'detail' => $plane->name]);
Expand Down
2 changes: 1 addition & 1 deletion demos/layout/layouts_manual.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
$app->initLayout([Layout::class]);

$layout->setApp($app);
Text::addTo($app->layout)->addHtml($layout->render());
Text::addTo($app->layout)->addHtml($layout->renderToHtml());
4 changes: 2 additions & 2 deletions docs/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ $grid->setModel($user);
$grid->addPaginator(); // initialize and populate paginator
$grid->addButton('Test'); // initialize and populate toolbar
echo $grid->render();
echo $grid->renderToHtml();
```

All of the objects created above - button, grid, toolbar and paginator will share the same
Expand All @@ -393,7 +393,7 @@ You can create App object on your own then add elements into it:
$app = new App('My App');
$app->add($grid);
echo $grid->render();
echo $grid->renderToHtml();
```

This does not change the output, but you can use the 'App' class to your advantage as a
Expand Down
2 changes: 1 addition & 1 deletion docs/form-control.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ class MyDropdown extends \Atk4\Ui\Dropdown
$this->_tItem->set('someOtherField', $res['someOtherField]);
$this->_tItem->set('someOtherField2', $res['someOtherField2]);
// add item to template
$this->template->dangerouslyAppendHtml('Item', $this->_tItem->render());
$this->template->dangerouslyAppendHtml('Item', $this->_tItem->renderToHtml());
}
}
```
Expand Down
4 changes: 2 additions & 2 deletions docs/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ $form->setApp($app);
$form->invokeInit();
$form->setModel(new User($db));
$html = $form->render();
$html = $form->renderToHtml();
```

This would render an individual component and will return HTML:
Expand All @@ -236,7 +236,7 @@ This would render an individual component and will return HTML:
</div>
```

For other use-cases please look into {php:meth}`View::render()`
For other use-cases please look into {php:meth}`View::renderToHtml()`

### Factory

Expand Down
12 changes: 6 additions & 6 deletions docs/render.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ $view = new \Atk4\Ui\View();
\Atk4\Ui\Button::addTo($view, ['test']);
echo $view->render();
echo $view->renderToHtml();
```

When render on the $view is executed, it will render button first then incorporate HTML into it's own template before rendering.
Expand All @@ -25,9 +25,9 @@ Here is a breakdown of how the above code works:
At this point Button is NOT element of a view just yet. This is because we can't be sure if $view will be rendered individually
or will become child of another view. Method init() is not executed on either objects.

4. render() method will call renderAll()
5. renderAll will find out that the $app property of a view is not set and will initialize it with default App.
6. renderAll will also find out that the init() has not been called for the $view and will call it.
4. renderToHtml() method will call renderAll()
5. renderAll() will find out that the $app property of a view is not set and will initialize it with default App.
6. renderAll() will also find out that the init() has not been called for the $view and will call it.
7. init() will identify that there are some "pending children" and will add them in properly.

Most of the UI classes will allow you to operate even if they are not initialized. For instance calling 'setModel()' will
Expand Down Expand Up @@ -77,7 +77,7 @@ $b2 = \Atk4\Ui\Button::addTo($v, ['Test2']);
echo $b2->name; // not set!! Not part of render tree
```

At this point, if you execute $v->render() it will create it's own App and will create its own render tree. On the other
At this point, if you execute $v->renderToHtml() it will create it's own App and will create its own render tree. On the other
hand, if you add $v inside layout, trees will merge and the same $app will be used:

```
Expand All @@ -91,7 +91,7 @@ should there be any problems with the initialization.

# Rendering outside

It's possible for some views to be rendered outside of the app. In the previous section I speculated that calling $v->render()
It's possible for some views to be rendered outside of the app. In the previous section I speculated that calling $v->renderToHtml()
will create its own tree independent from the main one.

Agile UI sometimes uses the following approach to render element on the outside:
Expand Down
12 changes: 6 additions & 6 deletions docs/view.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ The above code will produce the following HTML block:
```

All of the views combined form a "render tree". In order to get the HTML output
from all the render tree `View`s you need to execute `render()` for the top-most
from all the render tree `View`s you need to execute `renderToHtml()` for the top-most
leaf:

```
echo $v->render();
echo $v->renderToHtml();
```

Each of the views will automatically render all of the child views.
Expand Down Expand Up @@ -307,7 +307,7 @@ $button = Button::addTo($app, ['icon' => new MyAwesomeIcon('book')]);

## Rendering of a Tree

:::{php:method} render()
:::{php:method} renderToHtml()
Perform render of this View and all the child Views recursively returning a valid HTML string.
:::

Expand All @@ -317,8 +317,8 @@ Any view has the ability to render itself. Once executed, render will perform th
- call recursiveRender() to recursively render sub-elements.
- return JS code with on-dom-ready instructions along with HTML code of a current view.

You must not override render() in your objects. If you are integrating Agile UI into your
framework you shouldn't even use `render()`, but instead use `getHtml` and `getJs`.
You should not override `renderToHtml()` in your objects. If you are integrating Agile UI into your
framework you shouldn't even use `renderToHtml()`, but instead use `getHtml` and `getJs`.

:::{php:method} getHtml()
Returns HTML for this View as well as all the child views.
Expand Down Expand Up @@ -451,7 +451,7 @@ Agile UI will maintain unique ID for all the elements. The tag is set through 'n

```
$b = new \Atk4\Ui\Button(['name' => 'my-button3']);
echo $b->render();
echo $b->renderToHtml();
```

Outputs:
Expand Down
2 changes: 1 addition & 1 deletion src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ public function terminate($output = ''): void
public function terminateHtml($output): void
{
if ($output instanceof View) {
$output = $output->render();
$output = $output->renderToHtml();
} elseif ($output instanceof HtmlTemplate) {
$output = $output->renderToHtml();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Text extends View
public $content = '';

#[\Override]
public function render(): string
public function renderToHtml(): string
{
return $this->content;
}
Expand Down
4 changes: 2 additions & 2 deletions src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
class View extends AbstractView
{
/**
* When you call render() this will be populated with JavaScript chains.
* When you call renderAll() this will be populated with JavaScript chains.
*
* @var array<1|string, array<int, JsExpressionable>>
*
Expand Down Expand Up @@ -668,7 +668,7 @@ protected function renderTemplateToHtml(): string
* This method is for those cases when developer want to simply render his
* view and grab HTML himself.
*/
public function render(): string
public function renderToHtml(): string
{
$this->renderAll();

Expand Down
2 changes: 1 addition & 1 deletion tests/ButtonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public function testButtonIcon(): void
{
$button = new Button(['Load', 'icon' => 'pause']);
$button->setApp($this->createApp());
$button->render();
$button->renderToHtml();
}
}
20 changes: 10 additions & 10 deletions tests/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected function assertFormSubmit(\Closure $createFormFx, array $postData, ?\C
}
});

$form->render();
$form->renderToHtml();
$res = AppFormTestMock::assertInstanceOf($form->getApp())->output;

if ($checkExpectedErrorsFx !== null) {
Expand Down Expand Up @@ -344,24 +344,24 @@ public function testNoDisabledAttrWithHiddenType(): void
$input->readOnly = true;
$input->setApp($this->createApp());
$input->shortName = 'i';
self::assertStringContainsString(' readonly="readonly"', $input->render());
self::assertStringNotContainsString('disabled', $input->render());
self::assertStringContainsString(' readonly="readonly"', $input->renderToHtml());
self::assertStringNotContainsString('disabled', $input->renderToHtml());

$input = new Form\Control\Line();
$input->disabled = true;
$input->readOnly = true;
$input->setApp($this->createApp());
$input->shortName = 'i';
self::assertStringContainsString(' disabled="disabled"', $input->render());
self::assertStringNotContainsString('readonly', $input->render());
self::assertStringContainsString(' disabled="disabled"', $input->renderToHtml());
self::assertStringNotContainsString('readonly', $input->renderToHtml());

$input = new Form\Control\Hidden();
$input->disabled = true;
$input->readOnly = true;
$input->setApp($this->createApp());
$input->shortName = 'i';
self::assertStringNotContainsString('disabled', $input->render());
self::assertStringNotContainsString('readonly', $input->render());
self::assertStringNotContainsString('disabled', $input->renderToHtml());
self::assertStringNotContainsString('readonly', $input->renderToHtml());
}

public function testCheckboxWithNonBooleanException(): void
Expand All @@ -377,7 +377,7 @@ public function testCheckboxWithNonBooleanException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage('Checkbox form control requires field with boolean type');
$input->render();
$input->renderToHtml();
}

public function testUploadNoUploadCallbackException(): void
Expand All @@ -396,7 +396,7 @@ public function testUploadNoUploadCallbackException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage('Missing onUpload callback');
$input->render();
$input->renderToHtml();
}

public function testUploadNoDeleteCallbackException(): void
Expand All @@ -415,7 +415,7 @@ public function testUploadNoDeleteCallbackException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage('Missing onDelete callback');
$input->render();
$input->renderToHtml();
}
}

Expand Down
6 changes: 3 additions & 3 deletions tests/JsIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testUniqueId1(): void
{
$v = new Button(['icon' => 'pencil']);
$v->setApp($this->createApp());
$v->render();
$v->renderToHtml();

self::assertNotEmpty($v->icon);
self::assertNotEmpty($v->icon->name); // @phpstan-ignore-line
Expand All @@ -32,7 +32,7 @@ public function testUniqueId2(): void
$b1 = Button::addTo($v);
$b2 = Button::addTo($v);
$v->setApp($this->createApp());
$v->render();
$v->renderToHtml();

self::assertNotSame($b1->name, $b2->name);
}
Expand All @@ -42,7 +42,7 @@ public function testChainFalse(): void
$v = new Button(['name' => 'b']);
$j = $v->js()->hide();
$v->setApp($this->createApp());
$v->render();
$v->renderToHtml();

self::assertSame('$(\'#b\').hide()', $j->jsRender());
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ListerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function testListerRender2(): void
$view->invokeInit();
$lister = Lister::addTo($view, [], ['list']);
$lister->setSource(['foo', 'bar']);
self::assertSame('hello, world, world', $view->render());
self::assertSame('hello, world, world', $view->renderToHtml());
}

public function testAddAfterRender(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/RenderTreeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testBasic(): void
{
$view = new View();
$view->setApp($this->createApp());
$view->render();
$view->renderToHtml();

$view->getApp();
self::assertNotNull($view->template);
Expand All @@ -30,7 +30,7 @@ public function testBasicNested(): void
$view = new View();
$view2 = View::addTo($view);
$view->setApp($this->createApp());
$view->render();
$view->renderToHtml();

$view2->getApp();
self::assertNotNull($view2->template);
Expand Down
2 changes: 1 addition & 1 deletion tests/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testAddColumnWithoutModel(): void
$table->addColumn('eight', [Table\Column\Link::class, ['id' => 3]]);
$table->addColumn('nine');

$table->render();
$table->renderToHtml();
}

public function testAddColumnAlreadyExistsException(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/TableTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait TableTestTrait
*/
protected function extractTableRow(Table $table, string $rowDataId = '1'): string
{
preg_match('~<.*data-id="' . $rowDataId . '".*>~m', $table->render(), $matches);
preg_match('~<.*data-id="' . $rowDataId . '".*>~m', $table->renderToHtml(), $matches);

return preg_replace('~\r?\n|\r~', '', $matches[0]);
}
Expand Down
10 changes: 5 additions & 5 deletions tests/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function testMultipleTimesRender(): void
$v->set('foo');

$v->setApp($this->createApp());
$a = $v->render();
$b = $v->render();
$a = $v->renderToHtml();
$b = $v->renderToHtml();
self::assertSame($a, $b);
}

Expand All @@ -41,7 +41,7 @@ public function testAddAfterRenderException(): void
$v->set('foo');

$v->setApp($this->createApp());
$v->render();
$v->renderToHtml();

$this->expectException(Exception::class);
View::addTo($v);
Expand All @@ -51,12 +51,12 @@ public function testVoidTagRender(): void
{
$v = new View();
$v->setApp($this->createApp());
self::assertSame('<div id="atk"></div>', $v->render());
self::assertSame('<div id="atk"></div>', $v->renderToHtml());

$v = new View();
$v->element = 'img';
$v->setApp($this->createApp());
self::assertSame('<img id="atk">', $v->render());
self::assertSame('<img id="atk">', $v->renderToHtml());
}

public function testAddDelayedInit(): void
Expand Down

0 comments on commit b88fc6c

Please sign in to comment.