Skip to content

Instantly share code, notes, and snippets.

@Kazanir
Last active October 29, 2015 01:14
Show Gist options
  • Save Kazanir/dfc3a5f91ce250c1b33f to your computer and use it in GitHub Desktop.
Save Kazanir/dfc3a5f91ce250c1b33f to your computer and use it in GitHub Desktop.
<?php
// The goal is to turn any rendering calls part of a rendering tree into
// Hacklang async functions. Within compiled Twig templates this is tricky,
// because the `await foo()` syntax is not a true expression, and can only
// be used as part of assignment or return statements. That means that I
// need to turn this:
echo $this->env->getExtension('sandbox')->ensureToStringAllowed(
$this->env->getExtension('drupal_core')->escapeFilter($this->env, (isset($context["exposed"]) ? $context["exposed"] : null), "html", null, true)
);
// Into this:
$html = await $this->env->getExtension('hackutils')->asyncEscapeFilter($this->env, (isset($context["exposed"]) ? $context["exposed"] : null), "html", null, true);
echo $this->env->getExtension('sandbox')->ensureToStringAllowed($html);
//
// HOWEVER:
//
// The most ideal would be to gather up all variables for which this is
// desired into a keyed map and pass them through an async mapping function, like so:
// A map is just a native Hack collection with keyed values.
$vars = Map {
'content.exposed' => (isset($context["exposed"]) ? $context["exposed"] : null),
'content.image' =>(isset($context["image"]) ? $context["image"] : null),
};
// HH\asio\mm() maps an iterable through an async callback and returns an
// awaitable Map. This lets the individual asyncEscapeFilters complete in
// whatever order the scheduler thinks best.
$rendered = mm($vars, ($value) ==> {
$html = await $this->env->getExtension('hackutils')->asyncEscapeFilter($this->env, $value, "html", null, true);
return $this->env->getExtension('sandbox')->ensureToStringAllowed($html);
});
await $rendered;
echo $rendered['content.exposed'];
echo $rendered['content.image'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment