RuntimeError example

if (!class_exists($mainCls, false)) {
                    /* Last line of defense if either $this->bcWriteCacheFile was used, * $this->cache is implemented as a no-op or we have a race condition * where the cache was cleared between the above calls to write to and load from * the cache. */
                    eval('?>'.$content);
                }

                if (!class_exists($cls, false)) {
                    throw new RuntimeError(sprintf('Failed to load Twig template "%s", index "%s": cache might be corrupted.', $name$index), -1, $source);
                }
            }
        }

        $this->extensionSet->initRuntime();

        return $this->loadedTemplates[$cls] = new $cls($this);
    }

    /** * Creates a template from source. * * This method should not be used as a generic way to load templates. * * @param string $template The template source * @param string $name An optional name of the template to be used in error messages * * @throws LoaderError When the template cannot be found * @throws SyntaxError When an error occurred during compilation */

        }
    }

    if (!twig_test_iterable($values)) {
        return $values;
    }

    $values = twig_to_array($values);

    if (0 === \count($values)) {
        throw new RuntimeError('The random function cannot pick from an empty array.');
    }

    return $values[array_rand($values, 1)];
}

/** * Converts a date to the given format. * * {{ post.published_at|date("m/d/Y") }} * * @param \DateTimeInterface|\DateInterval|string $date A date * @param string|null $format The target format, null to use the default * @param \DateTimeZone|string|false|null $timezone The target timezone, null to use the default, false to leave unchanged * * @return string The formatted date */

    public function displayParentBlock($name, array $context, array $blocks = [])
    {
        if (isset($this->traits[$name])) {
            $this->traits[$name][0]->displayBlock($name$context$blocks, false);
        } elseif (false !== $parent = $this->getParent($context)) {
            $parent->displayBlock($name$context$blocks, false);
        } else {
            throw new RuntimeError(sprintf('The template has no parent and no traits defining the "%s" block.', $name), -1, $this->getSourceContext());
        }
    }

    /** * Displays a block. * * This method is for internal use only and should never be called * directly. * * @param string $name The block name to display * @param array $context The context * @param array $blocks The current set of blocks * @param bool $useBlocks Whether to use the current set of blocks */
return iconv('UTF-8', $charset$string);

        case 'js':
            // escape all non-alphanumeric characters             // into their \x or \uHHHH representations             if ('UTF-8' !== $charset) {
                $string = twig_convert_encoding($string, 'UTF-8', $charset);
            }

            if (!preg_match('//u', $string)) {
                throw new RuntimeError('The string to escape is not a valid UTF-8 string.');
            }

            $string = preg_replace_callback('#[^a-zA-Z0-9,\._]#Su', function D$matches) {
                $char = $matches[0];

                /* * A few characters have short escape sequences in JSON and JavaScript. * Escape sequences supported only by JavaScript, not JSON, are omitted. * \" is also supported but omitted, because the resulting string is not HTML safe. */
                static $shortMap = [
                    
public function hasExtension(string $class): bool
    {
        return isset($this->extensions[ltrim($class, '\\')]);
    }

    public function getExtension(string $class): ExtensionInterface
    {
        $class = ltrim($class, '\\');

        if (!isset($this->extensions[$class])) {
            throw new RuntimeError(sprintf('The "%s" extension is not enabled.', $class));
        }

        return $this->extensions[$class];
    }

    /** * @param ExtensionInterface[] $extensions */
    public function setExtensions(array $extensions): void
    {
        foreach ($extensions as $extension) {
            
Home | Imprint | This part of the site doesn't use cookies.