EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
handlebars update
Yuriicf0f9985015f
diff --git a/application/Espo/Core/Htmlizer/Helper/Data.php b/application/Espo/Core/Htmlizer/Helper/Data.phpindex 6a435565d3..746be40b43 100644--- a/application/Espo/Core/Htmlizer/Helper/Data.php+++ b/application/Espo/Core/Htmlizer/Helper/Data.php@@ -35,9 +35,9 @@ use Closure;class Data{/**- * @param mixed[] $argumentList+ * @internal+ * @param array<int, mixed> $argumentList* @param array<string, mixed> $rootContext- * @noinspection PhpPluralMixedCanBeReplacedWithArrayInspection*/public function __construct(private string $name,@@ -46,7 +46,7 @@ class Dataprivate mixed $context,private array $rootContext,private ?Closure $func,- private ?Closure $inverseFunc+ private ?Closure $inverseFunc,) {}public function getName(): string@@ -55,7 +55,9 @@ class Data}/**- * @return mixed A context.+ * A scope context.+ *+ * @return mixed*/public function getContext(): mixed{@@ -63,6 +65,8 @@ class Data}/**+ * A root context.+ ** @return array<string, mixed>*/public function getRootContext(): array@@ -70,29 +74,36 @@ class Datareturn $this->rootContext;}+ /**+ * Specified helper options (parameters).+ *+ * @return stdClass+ */public function getOptions(): stdClass{return $this->options;}/**- * @return mixed[]- * @noinspection PhpPluralMixedCanBeReplacedWithArrayInspection+ * @return array<int, mixed>*/public function getArgumentList(): array{return $this->argumentList;}+ /**+ * Has a specified helper option (parameter).+ */public function hasOption(string $name): bool{return property_exists($this->options, $name);}/**- * @return mixed+ * Get the value of a specified helper option (parameter).*/- public function getOption(string $name)+ public function getOption(string $name): mixed{return $this->options->$name ?? null;}diff --git a/application/Espo/Core/Htmlizer/Helper/SafeString.php b/application/Espo/Core/Htmlizer/Helper/SafeString.phpindex 65262cb367..ec8ee55eae 100644--- a/application/Espo/Core/Htmlizer/Helper/SafeString.php+++ b/application/Espo/Core/Htmlizer/Helper/SafeString.php@@ -29,15 +29,15 @@namespace Espo\Core\Htmlizer\Helper;-use LightnCandy\SafeString as LightnCandySafeString;+use DevTheorem\Handlebars\SafeString as SafeStringHandlebars;class SafeString{- private $wrappee;+ private SafeStringHandlebars $internalSafeString;public function __construct(string $value){- $this->wrappee = new LightnCandySafeString($value);+ $this->internalSafeString = new SafeStringHandlebars($value);}public static function create(string $value): self@@ -45,13 +45,16 @@ class SafeStringreturn new self($value);}- public function getWrappee(): LightnCandySafeString+ /**+ * @internal+ */+ public function getInternal(): SafeStringHandlebars{- return $this->wrappee;+ return $this->internalSafeString;}public function __toString(){- return (string) $this->wrappee;+ return (string) $this->internalSafeString;}}diff --git a/application/Espo/Core/Htmlizer/Htmlizer.php b/application/Espo/Core/Htmlizer/Htmlizer.phpindex 0a51ab7389..aac97b5bee 100644--- a/application/Espo/Core/Htmlizer/Htmlizer.php+++ b/application/Espo/Core/Htmlizer/Htmlizer.php@@ -29,7 +29,9 @@namespace Espo\Core\Htmlizer;-use Closure;+use DevTheorem\Handlebars\Handlebars;+use DevTheorem\Handlebars\HelperOptions;+use DevTheorem\Handlebars\Options;use DOMDocument;use DOMElement;use DOMException;@@ -48,7 +50,6 @@ use Espo\Repositories\Attachment as AttachmentRepository;use Espo\Core\Utils\Json;use Espo\Core\Acl;use Espo\Core\InjectableFactory;-use Espo\Core\ServiceFactory;use Espo\Core\Utils\Config;use Espo\Core\Utils\DateTime;use Espo\Core\Utils\Language;@@ -59,9 +60,6 @@ use Espo\ORM\Collection;use Espo\ORM\Entity;use Espo\ORM\EntityManager;-use LightnCandy\Flags;-use LightnCandy\LightnCandy as LightnCandy;-use LogicException;use RuntimeException;use stdClass;@@ -90,7 +88,6 @@ class Htmlizerprivate InjectableFactory $injectableFactory,private PrecisionProvider $precisionProvider,private ?Acl $acl = null,- private ?ServiceFactory $serviceFactory = null,) {}/**@@ -99,6 +96,7 @@ class Htmlizer* @param ?string $cacheId @deprecated To be skipped.* @param ?array<string, mixed> $additionalData Data will be passed to the template.* @param bool $skipLinks Do not process related records.+ * @internal*/public function render(?Entity $entity,@@ -113,86 +111,22 @@ class Htmlizer$template = $this->prepare($template, array_keys($helpers));- $code = LightnCandy::compile($template, [- 'flags' => Flags::FLAG_HANDLEBARSJS | Flags::FLAG_ERROR_EXCEPTION,- 'helpers' => $helpers,- ]);-- if ($code === false) {- throw new RuntimeException("Template compile error.");- }-- /**- * @var Closure|false $renderer- */- $renderer = LightnCandy::prepare($code);-- if ($renderer === false) {- throw new RuntimeException("Template compile error.");- }-- if ($additionalData === null) {- $additionalData = [];- }-- $data = $entity ?- $this->getDataFromEntity($entity, $skipLinks, 0, $template, $additionalData) :- $additionalData;-- if (!array_key_exists('today', $data)) {- $data['today'] = $this->dateTime->getTodayString();- $data['today_RAW'] = $this->dateTime->getTodayString(null, DateTime::SYSTEM_DATE_FORMAT);- }+ $options = new Options(+ helpers: $helpers,+ );- if (!array_key_exists('now', $data)) {- $data['now'] = $this->dateTime->getNowString();- $data['now_RAW'] = date(DateTime::SYSTEM_DATE_TIME_FORMAT);- }+ $renderer = Handlebars::compile($template, $options);- $data['__injectableFactory'] = $this->injectableFactory;- $data['__config'] = $this->config;- $data['__dateTime'] = $this->dateTime;- $data['__metadata'] = $this->metadata;- $data['__entityManager'] = $this->entityManager;- $data['__language'] = $this->language;- $data['__serviceFactory'] = $this->serviceFactory;- $data['__log'] = $this->log;- $data['__entityType'] = $entity?->getEntityType();+ $data = $this->prepareRendererData(+ additionalData: $additionalData,+ entity: $entity,+ skipLinks: $skipLinks,+ template: $template,+ );$html = $renderer($data);- if (!$skipInlineAttachmentHandling) {- $html = str_replace('?entryPoint=attachment&', '?entryPoint=attachment&', $html);- }-- if (!$skipInlineAttachmentHandling) {- /** @var string $html */- $html = preg_replace_callback(- '/\?entryPoint=attachment&id=([A-Za-z0-9\-]*)/',- function ($matches) {- $id = $matches[1];-- if (!$id) {- return '';- }-- /** @var Attachment $attachment */- $attachment = $this->entityManager->getEntityById(Attachment::ENTITY_TYPE, $id);-- if (!$attachment) {- return '';- }-- /** @var AttachmentRepository $repository */- $repository = $this->entityManager->getRepository(Attachment::ENTITY_TYPE);-- return $repository->getFilePath($attachment);- },- $html- );- }-- return $html;+ return $this->postProcessHtml($skipInlineAttachmentHandling, $html);}private function format(mixed $value): mixed@@ -409,28 +343,30 @@ class Htmlizerreturn '';}- /** @noinspection PhpUndefinedClassInspection */- /** @noinspection PhpUndefinedNamespaceInspection */+ /** @noinspection PhpUndefinedClassInspection, PhpUndefinedNamespaceInspection *//** @phpstan-ignore-next-line */- return new LightnCandy\SafeString("?entryPoint=attachment&id=" . $id);+ return new DevTheorem\Handlebars\SafeString("?entryPoint=attachment&id=" . $id);},'pagebreak' => function () {- /** @noinspection PhpUndefinedClassInspection, HtmlUnknownAttribute */- /** @noinspection PhpUndefinedNamespaceInspection */+ /** @noinspection PhpUndefinedClassInspection, HtmlUnknownAttribute, PhpUndefinedNamespaceInspection *//** @phpstan-ignore-next-line */- return new LightnCandy\SafeString('<br pagebreak="true">');+ return new DevTheorem\Handlebars\SafeString('<br pagebreak="true">');},'imageTag' => function () {$args = func_get_args();$context = $args[count($args) - 1];- $field = $context['hash']['field'] ?? null;+ if (!$context instanceof HelperOptions) {+ throw new RuntimeException();+ }++ $field = $context->hash['field'] ?? null;$id = null;if ($field) {- $id = $context['_this'][$field . 'Id'] ?? null;+ $id = $context->scope[$field . 'Id'] ?? null;} else if (count($args) > 1) {$id = $args[0];}@@ -439,8 +375,8 @@ class Htmlizerreturn null;}- $width = $context['hash']['width'] ?? null;- $height = $context['hash']['height'] ?? null;+ $width = $context->hash['width'] ?? null;+ $height = $context->hash['height'] ?? null;$attributesPart = "";@@ -455,22 +391,21 @@ class Htmlizer/** @noinspection HtmlRequiredAltAttribute */$html = "<img src=\"?entryPoint=attachment&id=$id\"$attributesPart>";- /** @noinspection PhpUndefinedNamespaceInspection */- /** @noinspection PhpUndefinedClassInspection */+ /** @noinspection PhpUndefinedNamespaceInspection, PhpUndefinedClassInspection *//** @phpstan-ignore-next-line */- return new LightnCandy\SafeString($html);+ return new DevTheorem\Handlebars\SafeString($html);},'var' => function () {$args = func_get_args();- $c = $args[1] ?? [];+ $object = $args[1] ?? [];$key = $args[0] ?? null;if (is_null($key)) {return null;}- return $c[$key] ?? null;+ return $object[$key] ?? null;},'numberFormat' => function () {$args = func_get_args();@@ -482,13 +417,17 @@ class Htmlizer$context = $args[count($args) - 1];$number = $args[0] ?? null;+ if (!$context instanceof HelperOptions) {+ throw new RuntimeException();+ }+if (is_null($number)) {return '';}- $decimals = $context['hash']['decimals'] ?? 0;- $decimalPoint = $context['hash']['decimalPoint'] ?? '.';- $thousandsSeparator = $context['hash']['thousandsSeparator'] ?? ',';+ $decimals = $context->hash['decimals'] ?? 0;+ $decimalPoint = $context->hash['decimalPoint'] ?? '.';+ $thousandsSeparator = $context->hash['thousandsSeparator'] ?? ',';return number_format((float) $number, $decimals, $decimalPoint, $thousandsSeparator);},@@ -502,10 +441,22 @@ class Htmlizer$context = $args[count($args) - 1];$dateValue = $args[0];- $format = $context['hash']['format'] ?? null;- $timezone = $context['hash']['timezone'] ?? null;- $language = $context['hash']['language'] ?? null;- $dateTime = $context['data']['root']['__dateTime'];+ if (!$context instanceof HelperOptions) {+ throw new RuntimeException();+ }++ $format = $context->hash['format'] ?? null;+ $timezone = $context->hash['timezone'] ?? null;+ $language = $context->hash['language'] ?? null;++ $rootData = $context->data['root'];++ $dateTime = $rootData['__dateTime'];++ /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */+ if (!$dateTime instanceof \Espo\Core\Utils\DateTime) {+ throw new RuntimeException();+ }if (strlen($dateValue) > 11) {return $dateTime->convertSystemDateTime($dateValue, $timezone, $format, $language);@@ -523,7 +474,11 @@ class Htmlizer$context = $args[count($args) - 1];$value = $args[0];- $params = $context['hash'];+ if (!$context instanceof HelperOptions) {+ throw new RuntimeException();+ }++ $params = $context->hash;$params['value'] = $value;/** @phpstan-ignore-next-line */@@ -532,65 +487,83 @@ class Htmlizer/** @noinspection PhpUndefinedNamespaceInspection *//** @noinspection PhpUndefinedClassInspection *//** @phpstan-ignore-next-line */- return new LightnCandy\SafeString("<barcodeimage data=\"$paramsString\"/>");+ return new DevTheorem\Handlebars\SafeString("<barcodeimage data=\"$paramsString\"/>");},'ifEqual' => function () {$args = func_get_args();$context = $args[count($args) - 1];+ if (!$context instanceof HelperOptions) {+ throw new RuntimeException();+ }+if ($args[0] === $args[1]) {- return $context['fn']();+ return $context->fn();}- return $context['inverse'] ? $context['inverse']() : '';+ return $context->inverse();},'ifNotEqual' => function () {$args = func_get_args();$context = $args[count($args) - 1];+ if (!$context instanceof HelperOptions) {+ throw new RuntimeException();+ }+if ($args[0] !== $args[1]) {- return $context['fn']();+ return $context->fn();}- return $context['inverse'] ? $context['inverse']() : '';+ return $context->inverse();},'ifInArray' => function () {$args = func_get_args();$context = $args[count($args) - 1];-$array = $args[1] ?? [];+ if (!$context instanceof HelperOptions) {+ throw new RuntimeException();+ }+if (in_array($args[0], $array)) {- return $context['fn']();+ return $context->fn();}- return $context['inverse'] ? $context['inverse']() : '';+ return $context->inverse();},'ifMultipleOf' => function () {$args = func_get_args();$context = $args[count($args) - 1];+ if (!$context instanceof HelperOptions) {+ throw new RuntimeException();+ }+if ($args[0] % $args[1] === 0) {- return $context['fn']();+ return $context->fn();}- return $context['inverse'] ? $context['inverse']() : '';+ return $context->inverse();},'checkboxTag' => function () {$args = func_get_args();$context = $args[count($args) - 1];+ if (!$context instanceof HelperOptions) {+ throw new RuntimeException();+ }+if (count($args) < 2) {return null;}- $color = $context['hash']['color'] ?? '#000';-- $option = $context['hash']['option'] ?? null;+ $color = $context->hash['color'] ?? '#000';+ $option = $context->hash['option'] ?? null;if (is_null($option)) {return null;@@ -617,7 +590,7 @@ class Htmlizer/** @noinspection PhpUndefinedNamespaceInspection *//** @noinspection PhpUndefinedClassInspection *//** @phpstan-ignore-next-line */- return new LightnCandy\SafeString($html);+ return new DevTheorem\Handlebars\SafeString($html);},];@@ -626,26 +599,41 @@ class Htmlizer$argumentList = array_slice($args, 0, -1);$context = $args[count($args) - 1];- $options = $context['hash'];- $rootData = $context['data']['root'];+ if (!$context instanceof HelperOptions) {+ throw new RuntimeException();+ }++ $hash = $context->hash;+ $rootData = $context->data['root'];$injectableFactory = $rootData['__injectableFactory'];$metadata = $rootData['__metadata'];- $name = $context['name'];+ /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */+ if (!$injectableFactory instanceof \Espo\Core\InjectableFactory) {+ throw new RuntimeException();+ }++ /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */+ if (!$metadata instanceof \Espo\Core\Utils\Metadata) {+ throw new RuntimeException();+ }++ $name = $context->name;- $className = $metadata->get(['app', 'templateHelpers', $name]);+ /** @var class-string<Helper> $className */+ $className = $metadata->get("app.templateHelpers.$name");// Not using FQN deliberately./** @noinspection PhpFullyQualifiedNameUsageInspection */$data = new \Espo\Core\Htmlizer\Helper\Data(- $name,- $argumentList,- (object) $options,- $context['_this'],- $rootData,- $context['fn'] ?? null,- $context['inverse'] ?? null,+ name: $name,+ argumentList: $argumentList,+ options: (object) $hash,+ context: $context->scope,+ rootContext: $rootData,+ func: $context->fn,+ inverseFunc: $context->inverse,);$helper = $injectableFactory->create($className);@@ -656,7 +644,7 @@ class Htmlizer/** @noinspection PhpFullyQualifiedNameUsageInspection */if ($value instanceof \Espo\Core\Htmlizer\Helper\SafeString) {- return $value->getWrappee();+ return $value->getInternal();}return $value;@@ -1006,7 +994,8 @@ class Htmlizerif (is_array($v)) {foreach ($v as $k => $w) {$keyRaw = $k . '_RAW';- $v[$keyRaw] = $v[$k];++ $v[$keyRaw] = $w;$v[$k] = $this->format($v[$k]);}}@@ -1148,4 +1137,81 @@ class Htmlizerreturn $this->number->format($value, $precision);}++ /**+ * @param ?array<string, mixed> $additionalData+ * @return array<string, mixed>+ */+ private function prepareRendererData(+ ?array $additionalData,+ ?Entity $entity,+ bool $skipLinks,+ string $template,+ ): array {++ if ($additionalData === null) {+ $additionalData = [];+ }++ $data = $entity ?+ $this->getDataFromEntity($entity, $skipLinks, 0, $template, $additionalData) :+ $additionalData;++ if (!array_key_exists('today', $data)) {+ $data['today'] = $this->dateTime->getTodayString();+ $data['today_RAW'] = $this->dateTime->getTodayString(null, DateTime::SYSTEM_DATE_FORMAT);+ }++ if (!array_key_exists('now', $data)) {+ $data['now'] = $this->dateTime->getNowString();+ $data['now_RAW'] = date(DateTime::SYSTEM_DATE_TIME_FORMAT);+ }++ $data['__injectableFactory'] = $this->injectableFactory;+ $data['__config'] = $this->config;+ $data['__dateTime'] = $this->dateTime;+ $data['__metadata'] = $this->metadata;+ $data['__entityManager'] = $this->entityManager;+ $data['__language'] = $this->language;+ $data['__log'] = $this->log;+ $data['__entityType'] = $entity?->getEntityType();++ return $data;+ }++ private function postProcessHtml(bool $skipInlineAttachmentHandling, string $html): string+ {+ if (!$skipInlineAttachmentHandling) {+ $html = str_replace('?entryPoint=attachment&', '?entryPoint=attachment&', $html);+ }++ if (!$skipInlineAttachmentHandling) {+ /** @var string $html */+ $html = preg_replace_callback(+ '/\?entryPoint=attachment&id=([A-Za-z0-9\-]*)/',+ function ($matches) {+ $id = $matches[1];++ if (!$id) {+ return '';+ }++ /** @var Attachment $attachment */+ $attachment = $this->entityManager->getEntityById(Attachment::ENTITY_TYPE, $id);++ if (!$attachment) {+ return '';+ }++ /** @var AttachmentRepository $repository */+ $repository = $this->entityManager->getRepository(Attachment::ENTITY_TYPE);++ return $repository->getFilePath($attachment);+ },+ $html+ );+ }++ return $html;+ }}diff --git a/composer.json b/composer.jsonindex 565faab697..c40bfbfdae 100644--- a/composer.json+++ b/composer.json@@ -25,7 +25,6 @@"dragonmantank/cron-expression": "^3.4","laminas/laminas-ldap": "2.20.x-dev#e7b9fe0e295f3898fb97d08b320654c92849918a","monolog/monolog": "^3.9",- "zordius/lightncandy": "dev-espo#v1.2.5e","composer/semver": "^3","spatie/async": "^1.8.1","symfony/process": "^7",@@ -56,7 +55,8 @@"lasserafn/php-initial-avatar-generator": "dev-update-image-lib#a46ab8f1427f93c5b37957e739205da7fcca0290","directorytree/imapengine": "^1.19","zbateson/mail-mime-parser": "^3.0",- "guzzlehttp/guzzle": "^7.10"+ "guzzlehttp/guzzle": "^7.10",+ "devtheorem/php-handlebars": "^0.9.9"},"require-dev": {"phpunit/phpunit": "^11.5",@@ -95,10 +95,6 @@}},"repositories": [- {- "type": "git",- "url": "https://github.com/yurikuzn/lightncandy.git"- },{"type": "git","url": "https://github.com/yurikuzn/laminas-ldap.git"diff --git a/composer.lock b/composer.lockindex e6e0c98b74..e2c4238fa3 100644--- a/composer.lock+++ b/composer.lock@@ -4,7 +4,7 @@"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies","This file is @generated automatically"],- "content-hash": "3e508d38a30eb43d18cd36d7412dd02d",+ "content-hash": "af96bcd2ee292e2b37ba362dbdbf96ba","packages": [{"name": "async-aws/core",@@ -634,6 +634,63 @@],"time": "2025-08-20T19:15:30+00:00"},+ {+ "name": "devtheorem/php-handlebars",+ "version": "v0.9.9",+ "source": {+ "type": "git",+ "url": "https://github.com/devtheorem/php-handlebars.git",+ "reference": "7bfbe91b0330cd92dc0abc55585bcc6128b6506b"+ },+ "dist": {+ "type": "zip",+ "url": "https://api.github.com/repos/devtheorem/php-handlebars/zipball/7bfbe91b0330cd92dc0abc55585bcc6128b6506b",+ "reference": "7bfbe91b0330cd92dc0abc55585bcc6128b6506b",+ "shasum": ""+ },+ "require": {+ "php": ">=8.2"+ },+ "require-dev": {+ "friendsofphp/php-cs-fixer": "^3.87",+ "jbboehr/handlebars-spec": "dev-master",+ "phpstan/phpstan": "^2.1.28",+ "phpunit/phpunit": "^11.5"+ },+ "type": "library",+ "autoload": {+ "psr-4": {+ "DevTheorem\\Handlebars\\": "src/"+ }+ },+ "notification-url": "https://packagist.org/downloads/",+ "license": [+ "MIT"+ ],+ "authors": [+ {+ "name": "Theodore Brown",+ "email": "[email protected]"+ },+ {+ "name": "Zordius Chen",+ "email": "[email protected]"+ }+ ],+ "description": "A fast, spec-compliant PHP implementation of Handlebars.",+ "keywords": [+ "compile",+ "handlebars",+ "php",+ "render",+ "template"+ ],+ "support": {+ "issues": "https://github.com/devtheorem/php-handlebars/issues",+ "source": "https://github.com/devtheorem/php-handlebars/tree/v0.9.9"+ },+ "time": "2025-10-15T19:05:55+00:00"+ },{"name": "directorytree/imapengine","version": "v1.22.4",@@ -8181,51 +8238,6 @@}],"time": "2024-04-29T21:42:39+00:00"- },- {- "name": "zordius/lightncandy",- "version": "dev-espo",- "source": {- "type": "git",- "url": "https://github.com/yurikuzn/lightncandy.git",- "reference": "207d424c4ced34644663fdc0758c19d5b6974adc"- },- "require": {- "php": ">=7.1.0"- },- "require-dev": {- "phpunit/phpunit": "^7"- },- "type": "library",- "extra": {- "branch-alias": {- "dev-master": "1.2.5-dev"- }- },- "autoload": {- "psr-4": {- "LightnCandy\\": "src"- }- },- "license": [- "MIT"- ],- "authors": [- {- "name": "Zordius Chen",- "email": "[email protected]"- }- ],- "description": "An extremely fast PHP implementation of handlebars ( http://handlebarsjs.com/ ) and mustache ( http://mustache.github.io/ ).",- "homepage": "https://github.com/zordius/lightncandy",- "keywords": [- "PHP",- "handlebars",- "logicless",- "mustache",- "template"- ],- "time": "2021-02-18T13:06:28+00:00"}],"packages-dev": [@@ -10054,10 +10066,9 @@"aliases": [],"minimum-stability": "stable","stability-flags": {- "cboden/ratchet": 20,"laminas/laminas-ldap": 20,- "lasserafn/php-initial-avatar-generator": 20,- "zordius/lightncandy": 20+ "cboden/ratchet": 20,+ "lasserafn/php-initial-avatar-generator": 20},"prefer-stable": false,"prefer-lowest": false,@@ -10075,9 +10086,9 @@"ext-pdo": "*","ext-ctype": "*"},- "platform-dev": {},+ "platform-dev": [],"platform-overrides": {"php": "8.3.0"},- "plugin-api-version": "2.9.0"+ "plugin-api-version": "2.6.0"}