application icon indicating copy to clipboard operation
application copied to clipboard

`Nette\Bridges\ApplicationLatte\Template::render()` does not accept object for template parameters

Open NoNoNo opened this issue 6 months ago • 1 comments

Version: 3.2.6 (latest)

Bug Description

Latte offers the feature to pass an object to the render method of templates.

This example is given in https://latte.nette.org/en/type-system:

class CatalogTemplateParameters
{
	public function __construct(
		public string $langs,
		/** @var ProductEntity[] */
		public array $products,
		public Address $address,
	) {}
}

$latte->render('template.latte', new CatalogTemplateParameters(
	address: $userAddress,
	lang: $settings->getLanguage(),
	products: $entityManager->getRepository('Product')->findAll(),
));

When I do this in a Control (extended from Nette\Application\UI\Control), an error is thrown:

TypeError: Nette\Bridges\ApplicationLatte\Template::render(): Argument #2 ($params) must be of type array, App\Controls\CatalogTemplateParameters given

Steps To Reproduce

public function render(): void
{
	$this->template->render(__DIR__ . '/template.latte', new CatalogTemplateParameters(
	    address: $userAddress,
	    lang: $settings->getLanguage(),
	    products: $entityManager->getRepository('Product')->findAll(),
         ));
}

Expected Behavior

Would be much appreciated, if the render method of Nette Controls would offer the same functionality as the render method of Latte.

A workaround is to pass the object as $this->template->params = $object;, but then you have to prefix all vars in the template with $params->….

Thanks and greetings!

NoNoNo avatar Jul 05 '25 15:07 NoNoNo

Okay, please send a pull request.

dg avatar Jul 17 '25 21:07 dg

My I suggest this solution:

diff --git i/vendor/nette/application/src/Bridges/ApplicationLatte/Template.php w/vendor/nette/application/src/Bridges/ApplicationLatte/Template.php
index cf7cef9..243ef5a 100644
--- i/vendor/nette/application/src/Bridges/ApplicationLatte/Template.php
+++ w/vendor/nette/application/src/Bridges/ApplicationLatte/Template.php
@@ -37,9 +37,9 @@ abstract class Template implements Nette\Application\UI\Template
        /**
         * Renders template to output.
         */
-       public function render(?string $file = null, array $params = []): void
+       public function render(?string $file = null, object|array $params = []): void
        {
-               Nette\Utils\Arrays::toObject($params, $this);
+               Nette\Utils\Arrays::toObject(is_object($params) ? get_object_vars($params) : $params, $this);
                $this->latte->render($file ?? $this->file, $this);
        }

@@ -47,9 +47,9 @@ abstract class Template implements Nette\Application\UI\Template
        /**
         * Renders template to output.
         */
-       public function renderToString(?string $file = null, array $params = []): string
+       public function renderToString(?string $file = null, object|array $params = []): string
        {
-               Nette\Utils\Arrays::toObject($params, $this);
+               Nette\Utils\Arrays::toObject(is_object($params) ? get_object_vars($params) : $params, $this);
                return $this->latte->renderToString($file ?? $this->file, $this);
        }

Veselé Vánoce a šťastný nový rok!

NoNoNo avatar Dec 28 '25 20:12 NoNoNo