Horde_Template::fetch PHP Method

fetch() public method

Fetches a template from the specified file and return the parsed contents.
public fetch ( string $filename = null ) : string
$filename string The file to fetch the template from.
return string The parsed template.
    public function fetch($filename = null)
    {
        $file = $this->_basepath . $filename;
        $force = $this->getOption('forcecompile');
        if (!is_null($filename) && $file != $this->_templateFile) {
            $this->_template = $this->_templateFile = null;
        }
        /* First, check for a cached compiled version. */
        $parts = array('horde_template', filemtime($file), $file);
        if ($this->getOption('gettext')) {
            $parts[] = setlocale(LC_ALL, 0);
        }
        $cacheid = implode('|', $parts);
        if (!$force && is_null($this->_template) && $this->_cache) {
            $this->_template = $this->_cache->get($cacheid, 0);
            if ($this->_template === false) {
                $this->_template = null;
            }
        }
        /* Parse and compile the template. */
        if ($force || is_null($this->_template)) {
            $this->_template = str_replace("\n", " \n", file_get_contents($file));
            $this->_parse();
            if ($this->_cache) {
                $this->_cache->set($cacheid, $this->_template);
                if ($this->_logger) {
                    $this->_logger->log(sprintf('Saved compiled template file for "%s".', $file), 'DEBUG');
                }
            }
        }
        $this->_templateFile = $file;
        /* Template debugging. */
        if ($this->getOption('debug')) {
            echo '<pre>' . htmlspecialchars($this->_template) . '</pre>';
        }
        return $this->parse();
    }

Usage Example

Example #1
0
 /**
  * $registry
  * $notification
  * $conf
  * $criteria
  *
  */
 public function run()
 {
     extract($this->_params, EXTR_REFS);
     $templates = Horde::loadConfiguration('templates.php', 'templates', 'jonah');
     /* Get requested channel. */
     try {
         $channel = $GLOBALS['injector']->getInstance('Jonah_Driver')->getChannel($criteria['feed']);
     } catch (Exception $e) {
         Horde::log($e, 'ERR');
         $notification->push(_("Invalid channel."), 'horde.error');
         Horde::url('delivery/index.php', true)->redirect();
         exit;
     }
     $title = sprintf(_("HTML Delivery for \"%s\""), $channel['channel_name']);
     $options = array();
     foreach ($templates as $key => $info) {
         $options[] = '<option value="' . $key . '"' . ($key == $criteria['format'] ? ' selected="selected"' : '') . '>' . $info['name'] . '</option>';
     }
     $template = new Horde_Template();
     $template->setOption('gettext', 'true');
     $template->set('url', Horde::selfUrl());
     $template->set('session', Horde_Util::formInput());
     $template->set('channel_id', $criteria['feed']);
     $template->set('channel_name', $channel['channel_name']);
     $template->set('format', $criteria['format']);
     $template->set('options', $options);
     // @TODO: This is ugly. storage driver shouldn't be rendering any display
     // refactor this to use individual views possibly with a choice of different templates
     $template->set('stories', $GLOBALS['injector']->getInstance('Jonah_Driver')->renderChannel($criteria['feed'], $criteria['format']));
     // Buffer the notifications and send to the template
     Horde::startBuffer();
     $GLOBALS['notification']->notify(array('listeners' => 'status'));
     $template->set('notify', Horde::endBuffer());
     $GLOBALS['page_output']->header(array('title' => $title));
     echo $template->fetch(JONAH_TEMPLATES . '/delivery/html.html');
     $GLOBALS['page_output']->footer();
 }
All Usage Examples Of Horde_Template::fetch