LightnCandy\Runtime::hbch PHP Метод

hbch() публичный статический Метод

For custom helpers.
public static hbch ( array\arraystring | integer> $cx, string $ch, arraystring | integer> | integer | null $vars, string $op, boolean $inverted, Closure | null $cb = null, Closure | null $else = null ) : string
$cx array\arraystring | integer>
$ch string the name of custom helper to be executed
$vars arraystring | integer> | integer | null
$op string the name of variable resolver. should be one of: 'raw', 'enc', or 'encq'.
$inverted boolean the logic will be inverted
$cb Closure | null callback function to render child context
$else Closure | null callback function to render child context when {{else}}
Результат string The rendered string of the token
    public static function hbch($cx, $ch, $vars, $op, $inverted, $cb = null, $else = null)
    {
        $isBlock = is_object($cb) && $cb instanceof \Closure;
        if (isset($cx['blparam'][0][$ch])) {
            return $cx['blparam'][0][$ch];
        }
        $args = $vars[0];
        $options = array('name' => $ch, 'hash' => $vars[1], 'contexts' => count($cx['scopes']) ? $cx['scopes'] : array(null), 'fn.blockParams' => 0);
        if ($isBlock) {
            $options['_this'] =& $op;
        } else {
            $options['_this'] =& $inverted;
        }
        if (isset($vars[2])) {
            $options['fn.blockParams'] = count($vars[2]);
        }
        // $invert the logic
        if ($inverted) {
            $tmp = $else;
            $else = $cb;
            $cb = $tmp;
        }
        if ($isBlock) {
            $options['fn'] = function ($context = '_NO_INPUT_HERE_', $data = null) use($cx, &$op, $cb, $options, $vars) {
                if ($cx['flags']['echo']) {
                    ob_start();
                }
                if (isset($data['data'])) {
                    $old_spvar = $cx['sp_vars'];
                    $cx['sp_vars'] = array_merge(array('root' => $old_spvar['root']), $data['data'], array('_parent' => $old_spvar));
                }
                $ex = false;
                if (isset($data['blockParams']) && isset($vars[2])) {
                    $ex = array_combine($vars[2], array_slice($data['blockParams'], 0, count($vars[2])));
                    array_unshift($cx['blparam'], $ex);
                } else {
                    if (isset($cx['blparam'][0])) {
                        $ex = $cx['blparam'][0];
                    }
                }
                if ($context === '_NO_INPUT_HERE_' || $context === $op) {
                    $ret = $cb($cx, is_array($ex) ? static::m($cx, $op, $ex) : $op);
                } else {
                    $cx['scopes'][] = $op;
                    $ret = $cb($cx, is_array($ex) ? static::m($cx, $context, $ex) : $context);
                    array_pop($cx['scopes']);
                }
                if (isset($data['data'])) {
                    $cx['sp_vars'] = $old_spvar;
                }
                return $cx['flags']['echo'] ? ob_get_clean() : $ret;
            };
        }
        if ($else) {
            $options['inverse'] = function ($context = '_NO_INPUT_HERE_') use($cx, $op, $else) {
                if ($cx['flags']['echo']) {
                    ob_start();
                }
                if ($context === '_NO_INPUT_HERE_') {
                    $ret = $else($cx, $op);
                } else {
                    $cx['scopes'][] = $op;
                    $ret = $else($cx, $context);
                    array_pop($cx['scopes']);
                }
                return $cx['flags']['echo'] ? ob_get_clean() : $ret;
            };
        } else {
            $options['inverse'] = function () {
                return '';
            };
        }
        if ($cx['flags']['spvar']) {
            $options['data'] = $cx['sp_vars'];
        }
        $args[] = $options;
        $e = null;
        $r = true;
        try {
            $r = call_user_func_array($cx['helpers'][$ch], $args);
        } catch (\Exception $E) {
            $e = "Runtime: call custom helper '{$ch}' error: " . $E->getMessage();
        }
        if ($e !== null) {
            static::err($cx, $e);
        }
        return $r;
    }