Skip to content

Commit

Permalink
Rename View::render() method to View::renderToHtml() (#2198)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Apr 5, 2024
1 parent b308ff5 commit 7732b14
Show file tree
Hide file tree
Showing 25 changed files with 70 additions and 85 deletions.
2 changes: 1 addition & 1 deletion demos/_unit-test/crud-nested.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
$entity = $model->load($id);

$innerCrud = Crud::addTo($v);
$innerCrud->setModel($entity->Products);
$innerCrud->setModel($entity->products);
});
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/collection/crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function addFormTo(View $view): Form

if (File::assertInstanceOf($this->action->getEntity())->is_folder) {
Grid::addTo($right, ['menu' => false, 'ipp' => 5])
->setModel(File::assertInstanceOf($this->getAction()->getModel())->SubFolder);
->setModel(File::assertInstanceOf($this->getAction()->getModel())->subFolder);
} else {
Message::addTo($right, ['Not a folder', 'type' => 'warning']);
}
Expand Down
2 changes: 1 addition & 1 deletion demos/collection/multitable.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,4 @@ public function setModel(Model $model, array $route = []): void
->on('click', new JsModal('Now importing ... ', $vp));

$finderClass::addTo($app, ['bottom attached segment'])
->setModel($model->setLimit(10), [$model->fieldName()->SubFolder]);
->setModel($model->setLimit(10), [$model->fieldName()->subFolder]);
4 changes: 2 additions & 2 deletions demos/form-control/dropdown-plus.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
$form = Form::addTo($demo->right);

$form->addControl('category_id', [Form\Control\Dropdown::class, 'model' => new Category($app->db)]);
$form->addControl('sub_category_id', [Form\Control\DropdownCascade::class, 'cascadeFrom' => 'category_id', 'reference' => Category::hinting()->fieldName()->SubCategories]);
$form->addControl('product_id', [Form\Control\DropdownCascade::class, 'cascadeFrom' => 'sub_category_id', 'reference' => SubCategory::hinting()->fieldName()->Products]);
$form->addControl('sub_category_id', [Form\Control\DropdownCascade::class, 'cascadeFrom' => 'category_id', 'reference' => Category::hinting()->fieldName()->subCategories]);
$form->addControl('product_id', [Form\Control\DropdownCascade::class, 'cascadeFrom' => 'sub_category_id', 'reference' => SubCategory::hinting()->fieldName()->products]);

$form->onSubmit(static function (Form $form) use ($app) {
$message = $app->encodeJson($app->uiPersistence->typecastSaveRow($form->entity, $form->entity->get()));
Expand Down
18 changes: 9 additions & 9 deletions demos/init-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ class Percent extends Field
* @property string $name @Atk4\Field()
* @property string $type @Atk4\Field()
* @property bool $is_folder @Atk4\Field()
* @property File $SubFolder @Atk4\RefMany()
* @property File $subFolder @Atk4\RefMany()
* @property int $count @Atk4\Field()
* @property Folder $parent_folder_id @Atk4\RefOne()
*/
Expand All @@ -490,7 +490,7 @@ protected function init(): void
$this->addField($this->fieldName()->type, ['caption' => 'MIME Type']);
$this->addField($this->fieldName()->is_folder, ['type' => 'boolean']);

$this->hasMany($this->fieldName()->SubFolder, [
$this->hasMany($this->fieldName()->subFolder, [
'model' => [self::class],
'theirField' => self::hinting()->fieldName()->parent_folder_id,
])
Expand Down Expand Up @@ -538,7 +538,7 @@ public function importFromFilesystem(string $path, ?bool $isSub = null): void
]);

if ($fileinfo->isDir()) {
$entity->SubFolder->importFromFilesystem($fileinfo->getPath() . '/' . $fileinfo->getFilename(), true);
$entity->subFolder->importFromFilesystem($fileinfo->getPath() . '/' . $fileinfo->getFilename(), true);
}

// skip full/slow import for Behat CI testing
Expand All @@ -562,8 +562,8 @@ protected function init(): void

/**
* @property string $name @Atk4\Field()
* @property SubCategory $SubCategories @Atk4\RefMany()
* @property Product $Products @Atk4\RefMany()
* @property SubCategory $subCategories @Atk4\RefMany()
* @property Product $products @Atk4\RefMany()
*/
class Category extends ModelWithPrefixedFields
{
Expand All @@ -576,11 +576,11 @@ protected function init(): void

$this->addField($this->fieldName()->name);

$this->hasMany($this->fieldName()->SubCategories, [
$this->hasMany($this->fieldName()->subCategories, [
'model' => [SubCategory::class],
'theirField' => SubCategory::hinting()->fieldName()->product_category_id,
]);
$this->hasMany($this->fieldName()->Products, [
$this->hasMany($this->fieldName()->products, [
'model' => [Product::class],
'theirField' => Product::hinting()->fieldName()->product_category_id,
]);
Expand All @@ -590,7 +590,7 @@ protected function init(): void
/**
* @property string $name @Atk4\Field()
* @property Category $product_category_id @Atk4\RefOne()
* @property Product $Products @Atk4\RefMany()
* @property Product $products @Atk4\RefMany()
*/
class SubCategory extends ModelWithPrefixedFields
{
Expand All @@ -606,7 +606,7 @@ protected function init(): void
$this->hasOne($this->fieldName()->product_category_id, [
'model' => [Category::class],
]);
$this->hasMany($this->fieldName()->Products, [
$this->hasMany($this->fieldName()->products, [
'model' => [Product::class],
'theirField' => Product::hinting()->fieldName()->product_sub_category_id,
]);
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
6 changes: 3 additions & 3 deletions 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 Expand Up @@ -544,8 +544,8 @@ Assume that each data model are defined and model Category has many Sub-Category
```
$form = \Atk4\Ui\Form::addTo($app);
$form->addControl('category_id', [Dropdown::class, 'model' => new Category($db)]);
$form->addControl('sub_category_id', [DropdownCascade::class, 'cascadeFrom' => 'category_id', 'reference' => Category::hinting()->fieldName()->SubCategories]);
$form->addControl('product_id', [DropdownCascade::class, 'cascadeFrom' => 'sub_category_id', 'reference' => SubCategory::hinting()->fieldName()->Products]);
$form->addControl('sub_category_id', [DropdownCascade::class, 'cascadeFrom' => 'category_id', 'reference' => Category::hinting()->fieldName()->subCategories]);
$form->addControl('product_id', [DropdownCascade::class, 'cascadeFrom' => 'sub_category_id', 'reference' => SubCategory::hinting()->fieldName()->products]);
```

## Lookup
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
10 changes: 5 additions & 5 deletions src/JsSse.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,17 @@ protected function flush(): void

private function outputEventResponse(string $content): void
{
// workaround flush() ignored by Apache mod_proxy_fcgi
// https://stackoverflow.com/questions/30707792/how-to-disable-buffering-with-apache2-and-mod-proxy-fcgi#36298336
// https://bz.apache.org/bugzilla/show_bug.cgi?id=68827
$content .= ': ' . str_repeat('x', 4_096) . "\n\n";

if ($this->echoFunction) {
($this->echoFunction)($content);

return;
}

// workaround flush() ignored by Apache mod_proxy_fcgi
// https://stackoverflow.com/questions/30707792/how-to-disable-buffering-with-apache2-and-mod-proxy-fcgi#36298336
// https://bz.apache.org/bugzilla/show_bug.cgi?id=68827
$content .= ': ' . str_repeat('x', 4_096) . "\n\n";

$app = $this->getApp();
\Closure::bind(static function () use ($app, $content): void {
$app->outputResponse($content);
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();
}
}
19 changes: 2 additions & 17 deletions tests/DemosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class DemosTest extends TestCase
|(?<array>\[(?:(?&json)(?:,(?&json))*|\s*)\])
|(?<object>\{(?:(?<pair>\s*(?&string)\s*:(?&json))(?:,(?&pair))*|\s*)\})
)\s*)$~sx';
protected static string $regexSseLine = '~^(event|data|(?=: x{4096}$)): .*$~';
protected static string $regexSse = '~^(event: [^\n]+\n(data: [^\n]*\n)+\n: x{4096}\n\n)+$~';

#[\Override]
public static function setUpBeforeClass(): void
Expand Down Expand Up @@ -452,22 +452,7 @@ public function testDemoSseResponse(string $path): void

$response = $this->getResponseFromRequest($path);
self::assertSame(200, $response->getStatusCode());

$outputLines = array_filter(
explode("\n", $response->getBody()->getContents()),
static fn ($v) => $v !== ''
);

// check SSE Syntax
self::assertGreaterThan(0, count($outputLines));
foreach ($outputLines as $index => $line) {
preg_match_all(self::$regexSseLine, $line, $matchesAll);
self::assertSame(
$line,
implode('', $matchesAll[0] ?? ['error']),
'Testing SSE response line ' . $index . ' with content ' . $line
);
}
self::assertMatchesRegularExpression(self::$regexSse, $response->getBody()->getContents());
}

public static function provideDemoJsonResponsePostCases(): iterable
Expand Down
Loading

0 comments on commit 7732b14

Please sign in to comment.