public function actionGenerate() { $sectionUrls = $this->_generateSections(); $categoryUrls = $this->_generateCategories(); $productTypeUrls = $this->_generateProductTypes(); HeaderHelper::setContentTypeByExtension('xml'); HeaderHelper::setHeader(array('charset' => 'utf-8')); $path = craft()->path->getPluginsPath() . 'seo/templates'; craft()->templates->setTemplatesPath($path); $this->renderTemplate('_sitemap', array('sectionUrls' => $sectionUrls, 'categoryUrls' => $categoryUrls, 'productTypeUrls' => $productTypeUrls, 'customUrls' => array_key_exists('customUrls', $this->sitemap) ? $this->sitemap['customUrls'] : [])); }
/** * 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); } }
public function actionIndex() { $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?>' . '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"/>'); $criteria = craft()->elements->getCriteria(ElementType::Entry); $criteria->limit = null; $excludeList = array(); if (craft()->config->exists('sitemap') && array_key_exists('excludeIds', craft()->config->get('sitemap'))) { $excludeList = craft()->config->get('sitemap')['excludeIds']; } foreach ($criteria as $entry) { if ($entry->url && !in_array($entry->id, $excludeList)) { $url = $xml->addChild('url'); $url->addChild('loc', $entry->url); $url->addChild('lastmod', $entry->dateUpdated->format(\DateTime::W3C)); $url->addChild('priority', $entry->uri == '__home__' ? 0.75 : 0.5); } } HeaderHelper::setContentTypeByExtension('xml'); ob_start(); echo $xml->asXML(); craft()->end(); }
/** * @return null */ public static function sendJsonHeaders() { HeaderHelper::setNoCache(); HeaderHelper::setContentTypeByExtension('json'); }
/** * Sets the Content-Type header to 'application/json'. */ public static function setJsonContentTypeHeader() { HeaderHelper::setContentTypeByExtension('json'); }
/** * Set Headers * * @param array $headers Headers * * @return Response Response */ public function setHeaders(array $headers) { HeaderHelper::setNoCache(); HeaderHelper::setContentTypeByExtension('json'); return $this; }
/** * Outputs the sitemap */ public function actionOutput() { HeaderHelper::setContentTypeByExtension('xml'); echo craft()->sitemap->sitemap; }