public function tokenize($template, $cacheTokens = true)
    {
        $lexer = $this->getLexer();
        if (false !== strstr($template, '{{')) {
            return $lexer->compile($this, $template);
        }

        if ($cacheTokens
            && array_key_exists($template, $this->cachedTemplates)
            && is_array($this->cachedTemplates[$template])
        ) {
            return $this->cachedTemplates[$template];
        }

        $templateOrTokens = $this->fetchTemplate($template);

        if (is_string($templateOrTokens)) {
            $templateOrTokens = $lexer->compile($this, $templateOrTokens, $template);
        }

        if ($templateOrTokens instanceof Traversable) {
            $templateOrTokens = iterator_to_array($templateOrTokens);
        }

        if (! is_array($templateOrTokens)) {
            throw new Exception\InvalidTokensException(sprintf(
                '%s was unable to either retrieve or compile tokens',
                __METHOD__
            ));
        }

        if ($cacheTokens) {
            $this->cachedTemplates[$template] = $templateOrTokens;
        }

        return $templateOrTokens;
    }