public function protectCDataSectionsFromParser($templateSource)
    {
        $parts = preg_split('/(\<\!\[CDATA\[|\]\]\>)/', $templateSource, -1, PREG_SPLIT_DELIM_CAPTURE);
        $balance = 0;
        $content = '';
        $resultingParts = [];
        foreach ($parts as $index => $part) {
            unset($parts[$index]);

            if ($balance === 0 && $part === '<![CDATA[') {
                $balance++;
                continue;
            }

            if ($balance === 0) {
                $resultingParts[] = $part;
                continue;
            }

            if ($balance === 1 && $part === ']]>') {
                $balance--;
            }

            if ($balance > 0 && $part === '<![CDATA[') {
                $balance++;
            }

            if ($balance > 0 && $part === ']]>') {
                $balance--;
            }

            if ($balance > 0) {
                $content .= $part;
            }

            if ($balance === 0 && $content !== '') {
                $resultingParts[] = '<f:format.base64Decode>' . base64_encode($content) . '</f:format.base64Decode>';
                $content = '';
            }
        }

        return implode('', $resultingParts);
    }