Skip to content

Commit 30c815b

Browse files
committed
Improve comments
1 parent 4fd5beb commit 30c815b

4 files changed

Lines changed: 21 additions & 24 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ PHP Handlebars compiles and executes complex templates up to 40% faster than Lig
1010
| Library | Compile time | Runtime | Total time | Peak memory usage |
1111
|--------------------|--------------|---------|------------|-------------------|
1212
| LightnCandy 1.2.6 | 5.2 ms | 2.8 ms | 8.0 ms | 5.3 MB |
13-
| PHP Handlebars 1.2 | 3.7 ms | 1.5 ms | 5.2 ms | 3.6 MB |
13+
| PHP Handlebars 1.2 | 3.6 ms | 1.5 ms | 5.1 ms | 3.6 MB |
1414

1515
_Tested on PHP 8.5 with the JIT enabled. See the `benchmark` branch to run the same test._
1616

@@ -106,7 +106,7 @@ echo $template(['first' => 'John']); // Error: "last" not defined
106106
`Handlebars::compile` returns a closure which can be invoked as `$template($context, $options)`.
107107
The `$options` parameter takes an array of runtime options, accepting the following keys:
108108

109-
* `data`: An associative array of initial `@data` variables (e.g. `['version' => '1.0']` makes `@version` available in the template).
109+
* `data`: An associative array of custom `@data` variables (e.g. `['version' => '1.0']` makes `@version` available in the template).
110110

111111
* `helpers`: An `array<string, \Closure>` of helpers to merge with the built-in helpers. Can also be used to override a built-in helper by using the same name.
112112

@@ -218,8 +218,8 @@ If a custom helper is executed in a `{{ }}` expression, the return value will be
218218
When a helper is executed in a `{{{ }}}` expression, the original return value will be output directly.
219219

220220
Helpers may return a `DevTheorem\Handlebars\SafeString` instance to prevent escaping the return value.
221-
When constructing the string that will be marked as safe, any external content should be properly escaped
222-
using the `Handlebars::escapeExpression()` method to avoid potential security concerns.
221+
Because `SafeString` bypasses the automatic HTML escaping that `{{ }}` applies, any user-supplied content
222+
embedded in it must first be escaped with `Handlebars::escapeExpression()` to prevent XSS vulnerabilities.
223223

224224
## Data Frames
225225

src/HelperOptions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function fn(mixed $context = Scope::Use, mixed $data = null): string
6969
if ($this->inv === null) {
7070
throw new \Exception('fn() is not supported for inline helpers');
7171
}
72+
// Occurs when blockHelperMissing routes a truthy context through fn() for an inverted block.
7273
return '';
7374
}
7475
return $this->invokeBlock($this->cb, $context, $data);

src/Runtime.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ public static function createContext(mixed $context, array $options, array $comp
201201

202202
/**
203203
* Invoke $v if it is callable, passing any extra args; otherwise return $v as-is.
204-
* Used for data variables that may hold functions (e.g. {{@hello}} or {{@hello "arg"}}).
204+
* Used for data variables that may hold functions (e.g. {{@hello}}) and for non-simple
205+
* pathed expressions with arguments (e.g. {{./helper "arg"}}).
205206
*/
206207
public static function dv(mixed $v, mixed ...$args): mixed
207208
{
@@ -241,18 +242,13 @@ public static function hv(RuntimeContext $cx, string $name, mixed &$_this): mixe
241242
}
242243

243244
/**
244-
* For {{#if}} and {{#unless}}.
245-
*
246-
* @param array<array<mixed>|string|int>|string|\Stringable|int|float|bool|null $v value to be tested
247-
* @param bool $zero include zero as true
248-
*
249-
* @return bool Return true when the value is not null nor false.
245+
* Returns true or false following the semantics of {{#if}} and {{#unless}} in Handlebars.js.
250246
*/
251-
public static function ifvar(mixed $v, bool $zero = false): bool
247+
public static function ifvar(mixed $v, bool $includeZero = false): bool
252248
{
253249
return $v !== null
254250
&& $v !== false
255-
&& ($zero || ($v !== 0 && $v !== 0.0))
251+
&& ($includeZero || ($v !== 0 && $v !== 0.0))
256252
&& $v !== ''
257253
&& (!is_array($v) || $v)
258254
&& (!$v instanceof \Stringable || (string) $v !== '');
@@ -300,7 +296,8 @@ public static function raw(mixed $value): string
300296

301297
/**
302298
* For {{#var}} and {{^var}} sections.
303-
* Pass null for $cb when compiling an inverted section ({{^var}}) — blockHelperMissing will call inverse().
299+
* Pass null for $cb when compiling an inverted section ({{^var}}): blockHelperMissing routes
300+
* truthy contexts through fn() (which returns '' when $cb is null) and falsy contexts through inverse().
304301
*
305302
* @param mixed $in input data with current scope
306303
* @param \Closure|null $cb callback function to render child context; null for inverted sections
@@ -328,11 +325,6 @@ public static function sec(RuntimeContext $cx, mixed $value, mixed $in, ?\Closur
328325

329326
/**
330327
* Get merged context.
331-
*
332-
* @param array<array<mixed>|string|int>|object|string|int|null $a the context to be merged
333-
* @param array<array<mixed>|string|int|null>|string|int|null $b the new context to overwrite
334-
*
335-
* @return array<array<mixed>|string|int|null>|object|string|int|null the merged context object
336328
*/
337329
public static function merge(mixed $a, mixed $b): mixed
338330
{
@@ -451,7 +443,10 @@ public static function dynhbch(RuntimeContext $cx, string $name, array $position
451443
}
452444

453445
/**
454-
* For single known helpers.
446+
* Invoke a resolved helper Closure with positional params, hash, and a HelperOptions instance.
447+
* Used for known helpers (direct hbch calls from generated code), runtime-registered helpers
448+
* (called from hv()), context closures (called from dynhbch()), and built-in fallbacks like
449+
* helperMissing/blockHelperMissing.
455450
*
456451
* @param array<mixed> $positional
457452
* @param array<string, mixed> $hash
@@ -516,8 +511,8 @@ public static function hbbch(RuntimeContext $cx, \Closure $helper, string $name,
516511
* @param array<mixed> $positional
517512
* @param array<string, mixed> $hash
518513
* @param array<string,array<mixed>|string|int> $_this current rendering context for the helper
519-
* @param \Closure $cb callback function to render child context
520-
* @param \Closure|null $else callback function to render child context when {{else}}
514+
* @param \Closure|null $cb callback function to render main block; null for inverted sections with params/hash
515+
* @param \Closure|null $else callback function to render {{else}}
521516
* @param array<mixed> $outerBlockParams outer block param stack for block params declared by the template
522517
*/
523518
public static function dynhbbch(RuntimeContext $cx, string $name, mixed $callable, array $positional, array $hash, mixed &$_this, ?\Closure $cb, ?\Closure $else, int $blockParamCount, array $outerBlockParams): mixed

src/SafeString.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
/**
66
* Can be returned from a custom helper to prevent an HTML string from being escaped
7-
* when the template is rendered. When constructing, any external content should be
8-
* properly escaped using Handlebars::escapeExpression() to avoid potential security concerns.
7+
* when the template is rendered. Because SafeString bypasses the automatic HTML escaping
8+
* that {{ }} applies, any user-supplied content embedded in it must first be escaped with
9+
* Handlebars::escapeExpression() to prevent XSS vulnerabilities.
910
*/
1011
class SafeString implements \Stringable
1112
{

0 commit comments

Comments
 (0)