public function evaluate(TemplateManager $manager, array $context, array $current = NULL) { // check filters foreach ($this->m_filters as $filter) { if (!$manager->filter($filter)) { throw new \Exception("Undefined filter `{$filter}` in template `{$this->m_name}`"); } } // search the inheritance hierarchy $t = $this; while ($t->m_parent) { $t = $t->m_parent; $t = $manager->template($t); if (isset($context[$t->m_name])) { // copy definitions to corresponding child if (!isset($context[$this->m_name])) { $context[$this->m_name] = []; } foreach ($context[$t->m_name] as $parentKey => $parentVal) { if (!isset($context[$this->m_name][$parentKey])) { $context[$this->m_name][$parentKey] = $parentVal; } } } } if ($this->anonymous()) { $rootName = $this->m_root->name(); if ($current != NULL && isset($current[$rootName])) { $current = $current[$rootName]; // check for int key to determine if this is a list // (this is a hack) reset($current); if (count($current) && !is_int(key($current))) { $current = [$current]; } foreach ($current as $key => $row) { $row['__iteration'] = $key; $this->m_root->evaluate($manager, $context, $row); } } else { // how to define optional sources? //die(sprintf('No mapping for source "%s".', $this->m_name)); //echo $this->m_name."<br>\n"; } } else { if ($context != NULL && isset($context[$this->m_name])) { $current = $context[$this->m_name]; } else { //$current = NULL; } $this->m_root->evaluate($manager, $context, $current); } }
public function before() { parent::before(); $this->m_manager = TemplateManager::create(BRAMBLE_TEMPLATES); $this->m_manager->register('subvert', function ($options, $context, $text) { $subvert_options = []; $subvert_options['image_callback'] = function (&$attributes) { // add dimensions to url $width = isset($attributes['width']) ? $attributes['width'] : '0'; $height = isset($attributes['height']) ? $attributes['height'] : '0'; if (ctype_digit($width) && ctype_digit($height) && ($width != 0 || $height != 0)) { $parts = pathinfo($attributes['src']); $attributes['src'] = sprintf('%s/%s-%sx%s.%s', $parts['dirname'], $parts['filename'], $width, $height, $parts['extension']); } }; if ($options) { if (isset($options['code'])) { $subvert_options['code_formatting'] = true; } if (isset($options['root'])) { $subvert_options['root_url'] = BRAMBLE_URL; } if (isset($options['header'])) { $subvert_options['header_level'] = (int) $options['header']; } else { // does it make sense to have this here? $subvert_options['header_level'] = 3; } } return Subvert::Parse($text, $subvert_options); }); $this->m_manager->register('format-date', function ($options, $context, $date) { if (is_string($date)) { $date = strtotime($date); } if ($options) { if (isset($options['atom'])) { $date = date(\DateTime::ATOM, $date); } else { if (count($options) === 1) { $date = date(trim(key($options), "'"), $date); } } } else { $date = date('Y-m-d', $date); } return $date; }); $this->m_manager->register('format-url', function ($options, $context) { if ($options && count($options) === 1) { if (isset($options['post'])) { $time = $context['time']; if (is_string($time)) { $time = strtotime($time); } return self::_format_post_url($time, $context['slug']); } else { if (isset($options['page'])) { return self::_format_page_url($context['slug']); } else { if (isset($options['category'])) { return self::_format_category_url($context['slug']); } else { if (isset($options['archive'])) { $time = $context['month']; if (is_string($time)) { $time = strtotime($time); } return self::_format_archive_url($time); } } } } } return ''; }); }
public function action__templates() { TemplateManager::create(BRAMBLE_TEMPLATES, true); return NULL; }