Exemplo n.º 1
0
 /**
  * @dataProvider renderTemplateDataProvider
  */
 public function testTemplateRendering($testFilename, $expectedResultsFilename)
 {
     // Render our template.
     $fs = MockFileSystem::create()->withPagesDir();
     $pc = $fs->getApp(array('cache' => false));
     $pc->getConfig()->setValue('site/root', 'http://whatever/');
     $pc->setTemplatesDirs(PIECRUST_UNITTESTS_DATA_DIR . 'templates');
     $testInfo = pathinfo($testFilename);
     $engine = PieCrustHelper::getTemplateEngine($pc, $testInfo['extension']);
     $this->assertNotNull($engine, "Couldn't find a template engine for extension: " . $testInfo['extension']);
     $this->assertEquals($testInfo['extension'], $engine->getExtension());
     ob_start();
     try {
         $data = $pc->getConfig()->get();
         $data['page'] = array('title' => 'The title of the page');
         $engine->renderFile(array($testInfo['basename']), $data);
     } catch (Exception $e) {
         ob_end_clean();
         throw $e;
     }
     $actualResults = ob_get_clean();
     $actualResults = str_replace("\r\n", "\n", $actualResults);
     // Compare to what we are expecting.
     $expectedResults = file_get_contents($expectedResultsFilename);
     $expectedResults = str_replace("\r\n", "\n", $expectedResults);
     $this->assertEquals($expectedResults, $actualResults, 'The rendered template is not what we expected.');
 }
Exemplo n.º 2
0
 /**
  * Renders the given page and sends the result to the standard output.
  */
 public function render()
 {
     $pieCrust = $this->page->getApp();
     $pageConfig = $this->page->getConfig();
     // Get the template name.
     $templateName = $this->page->getConfig()->getValue('layout');
     if ($templateName == null or $templateName == '' or $templateName == 'none') {
         $templateName = false;
     } else {
         if (!preg_match('/\\.[a-zA-Z0-9]+$/', $templateName)) {
             $templateName .= '.html';
         }
     }
     if ($templateName !== false) {
         // Get the template engine and the page data.
         $extension = pathinfo($templateName, PATHINFO_EXTENSION);
         $templateEngine = PieCrustHelper::getTemplateEngine($pieCrust, $extension);
         // Render the page.
         $data = DataBuilder::getTemplateRenderingData($this->page);
         $templateEngine->renderFile($templateName, $data);
     } else {
         // No template... just output the 'content' segment.
         echo $this->page->getContentSegment();
     }
     if ($pieCrust->isDebuggingEnabled()) {
         // Add a footer with version, caching and timing information.
         $this->renderStatsFooter($this->page);
     }
 }
Exemplo n.º 3
0
 /**
  * Renders the given page and sends the result to the standard output.
  */
 public function render()
 {
     $pieCrust = $this->page->getApp();
     $pageConfig = $this->page->getConfig();
     // Set the page as the current context.
     $executionContext = $pieCrust->getEnvironment()->getExecutionContext(true);
     $executionContext->pushPage($this->page);
     // Get the template name.
     $templateNames = $this->page->getConfig()->getValue('layout');
     if ($templateNames == null or $templateNames == '' or $templateNames == 'none') {
         $templateNames = false;
     } else {
         $templateNames = explode(',', $templateNames);
         foreach ($templateNames as &$name) {
             if (!preg_match('/\\.[a-zA-Z0-9]+$/', $name)) {
                 $name .= '.html';
             }
         }
     }
     if ($templateNames !== false) {
         // Get the template engine and the page data.
         $extension = pathinfo($templateNames[0], PATHINFO_EXTENSION);
         $templateEngine = PieCrustHelper::getTemplateEngine($pieCrust, $extension);
         // Render the page.
         $data = DataBuilder::getTemplateRenderingData($this->page);
         $templateEngine->renderFile($templateNames, $data);
     } else {
         // No template... just output the 'content' segment.
         echo $this->page->getContentSegment();
     }
     // Restore the previous context.
     $executionContext->popPage();
 }
Exemplo n.º 4
0
 protected function formatContentsUnsafe(array $rawSegments)
 {
     $data = DataBuilder::getPageRenderingData($this->page);
     $pieCrust = $this->page->getApp();
     $templateEngineName = $this->page->getConfig()->getValue('template_engine');
     $templateEngine = PieCrustHelper::getTemplateEngine($pieCrust, $templateEngineName);
     if (!$templateEngine) {
         throw new PieCrustException("Unknown template engine '{$templateEngineName}'.");
     }
     $contents = array();
     foreach ($rawSegments as $key => $pieces) {
         $contents[$key] = '';
         foreach ($pieces as $piece) {
             $content = $piece['content'];
             $format = $piece['format'];
             ob_start();
             try {
                 $templateEngine->renderString($content, $data);
                 $renderedContent = ob_get_clean();
             } catch (Exception $e) {
                 ob_end_clean();
                 throw $e;
             }
             if (!$format) {
                 $format = $this->page->getConfig()->getValue('format');
             }
             $renderedAndFormattedContent = PieCrustHelper::formatText($pieCrust, $renderedContent, $format);
             $contents[$key] .= $renderedAndFormattedContent;
         }
     }
     if (!empty($contents['content'])) {
         $matches = array();
         if (preg_match('/^<!--\\s*(more|(page)?break)\\s*-->\\s*$/m', $contents['content'], $matches, PREG_OFFSET_CAPTURE)) {
             // Add a special content segment for the "intro/abstract" part
             // of the article.
             $offset = $matches[0][1];
             $abstract = substr($contents['content'], 0, $offset);
             $this->page->getConfig()->appendValue('segments', 'content.abstract');
             $contents['content.abstract'] = $abstract;
         }
     }
     return $contents;
 }