/**
  * Renders a template, and either outputs or returns it.
  *
  * @param mixed $template      The name of the template to load in a format supported by
  *                             {@link TemplatesService::findTemplate()}, or a {@link StringTemplate} object.
  * @param array $variables     The variables that should be available to the template.
  * @param bool  $return        Whether to return the results, rather than output them. (Default is `false`.)
  * @param bool  $processOutput Whether the output should be processed by {@link processOutput()}.
  *
  * @throws HttpException
  * @return mixed The rendered template if $return is set to `true`.
  */
 public function renderTemplate($template, $variables = array(), $return = false, $processOutput = false)
 {
     if (($output = craft()->templates->render($template, $variables)) !== false) {
         if ($processOutput) {
             $output = $this->processOutput($output);
         }
         if ($return) {
             return $output;
         } else {
             // Set the MIME type for the request based on the matched template's file extension (unless the
             // Content-Type header was already set, perhaps by the template via the {% header %} tag)
             if (!HeaderHelper::isHeaderSet('Content-Type')) {
                 // Safe to assume that findTemplate() will return an actual template path here, and not `false`.
                 // If the template didn't exist, a TemplateLoaderException would have been thrown when calling
                 // craft()->templates->render().
                 $templateFile = craft()->templates->findTemplate($template);
                 $extension = IOHelper::getExtension($templateFile, 'html');
                 if ($extension == 'twig') {
                     $extension = 'html';
                 }
                 HeaderHelper::setContentTypeByExtension($extension);
             }
             // Set the charset header
             HeaderHelper::setHeader(array('charset' => 'utf-8'));
             // Are we serving HTML or XHTML?
             if (in_array(HeaderHelper::getMimeType(), array('text/html', 'application/xhtml+xml'))) {
                 // Are there any head/foot nodes left in the queue?
                 $headHtml = craft()->templates->getHeadHtml();
                 $footHtml = craft()->templates->getFootHtml();
                 if ($headHtml) {
                     if (($endHeadPos = mb_stripos($output, '</head>')) !== false) {
                         $output = mb_substr($output, 0, $endHeadPos) . $headHtml . mb_substr($output, $endHeadPos);
                     } else {
                         $output .= $headHtml;
                     }
                 }
                 if ($footHtml) {
                     if (($endBodyPos = mb_stripos($output, '</body>')) !== false) {
                         $output = mb_substr($output, 0, $endBodyPos) . $footHtml . mb_substr($output, $endBodyPos);
                     } else {
                         $output .= $footHtml;
                     }
                 }
             }
             // Output it into a buffer, in case TasksService wants to close the connection prematurely
             ob_start();
             echo $output;
             // End the request
             craft()->end();
         }
     } else {
         throw new HttpException(404);
     }
 }