protected function loadTemplateBlocks(Template &$template)
 {
     if ($this->benchmarkRendering) {
         $this->Benchmark->start('load-template-blocks-' . $template->getName());
     }
     $unparsedContent = $template->getContents();
     $templateBlocks = array();
     $parsedContent = '';
     $foundOne = false;
     while (($pos = strpos($unparsedContent, '{% begin ')) !== false) {
         $foundOne = true;
         $parsedContent .= substr($unparsedContent, 0, $pos);
         $content = substr($unparsedContent, $pos);
         $eol = strpos($content, "\n");
         $line = $content;
         if ($eol !== FALSE) {
             $line = substr($content, 0, $eol !== FALSE ? $eol : 0);
         }
         if (preg_match("/\\{\\%\\s+begin\\s+([^\\%]+?)\\s+\\%\\}[\n\r\t]*/s", $line, $m)) {
             $blockName = $m[1];
             $endpos = strpos($content, '{% end %}');
             if ($endpos === FALSE) {
                 throw new Exception('No end block found for :' . $m[0]);
             }
             $blockContents = trim(substr($content, strlen($m[0]), $endpos - strlen($m[0])), "\n\r\t");
             $blockContents = $this->parseFormatVariables($blockContents, $this->getConstants());
             $templateBlocks[$blockName] = $blockContents;
         } else {
             throw new Exception('Invalid begin block found: ' . $line);
         }
         $unparsedContent = substr($content, $endpos + strlen('{% end %}'));
     }
     if (!$foundOne) {
         $blockContents = $this->parseFormatVariables($unparsedContent, $this->getConstants());
         $templateBlocks['contents'] = $blockContents;
     }
     $template->setTemplateBlocks($templateBlocks);
     if ($this->benchmarkRendering) {
         $this->Benchmark->end('load-template-blocks-' . $template->getName());
     }
     return $template;
 }