Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not render helper output #26

Closed
thekid opened this issue Apr 7, 2023 · 3 comments
Closed

Do not render helper output #26

thekid opened this issue Apr 7, 2023 · 3 comments
Labels

Comments

@thekid
Copy link
Member

thekid commented Apr 7, 2023

The following code produces the output YEHUDA Katz by interpolating the string returned from the helper:

use com\handlebarsjs\HandlebarsEngine;

$engine= new HandlebarsEngine();
$engine->withHelper('full', function($node, $context, $options) {
  return strtoupper($options[0]).' {{lastname}}';
});

echo $engine->render('{{full firstname}}', [
  'firstname' => 'Yehuda',
  'lastname'  => 'Katz',
]);

However, the official Handlebars engine does not do this:

Screenshot

This is due to how the Mustache library is implemented. It even has a unittest verifying this behavior.

@thekid
Copy link
Member Author

thekid commented Apr 7, 2023

One idea would be to decouple this library from the Mustache library. This would mean quite a bit of changes!

@thekid
Copy link
Member Author

thekid commented Apr 7, 2023

The other idea would be to overwrite the asRendering() method as follows:

diff --git a/src/main/php/com/handlebarsjs/DefaultContext.class.php b/src/main/php/com/handlebarsjs/DefaultContext.class.php
index ae1dfb4..1ea8308 100755
--- a/src/main/php/com/handlebarsjs/DefaultContext.class.php
+++ b/src/main/php/com/handlebarsjs/DefaultContext.class.php
@@ -36,6 +36,24 @@ class DefaultContext extends DataContext {
     return new self($result, $parent ?: $this);
   }
 
+  /**
+   * Returns a rendering of a given helper closure
+   *
+   * @param  var $closure
+   * @param  com.github.mustache.Node $node
+   * @param  string[] $options
+   * @param  string $start
+   * @param  string $end
+   * @return string
+   */
+  public function asRendering($closure, $node, $options= [], $start= '{{', $end= '}}') {
+    $pass= [];
+    foreach ($options as $key => $option) {
+      $pass[$key]= $this->isCallable($option) ? $option($node, $this, $pass) : $option;
+    }
+    return $closure($node, $this, $pass);
+  }
+
   /**
    * Parse input into segments. Handles literal segments including quoted
    * strings, e.g. `input.["item-class"]`.

thekid added a commit that referenced this issue Apr 7, 2023
This ensures compatibility with the official Handlebars implementation.
See #26
@thekid thekid added the refactor label Apr 7, 2023
@thekid
Copy link
Member Author

thekid commented Apr 7, 2023

@thekid thekid closed this as completed Apr 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant