/** * get a TemplateFile instance of a given template name * * @param string $templateName template name without extension * @return TemplateFile TemplateFile instance */ public static function getTemplate($templateName, $array = null) { $template = new TemplateFile(dirname(__FILE__) . "/../templatefiles/{$templateName}.php"); if ($array) { $template->setArray($array); } return $template; }
/** * Render a list of categories, optionally showing a few posts from each * * @param PageArray $categories * @param int Number of posts to show from each category (default=0) * @return string * */ function renderCategories(PageArray $categories, $showNumPosts = 0) { foreach ($categories as $category) { $category->posts = wire('pages')->find("template=post, categories={$category}, limit={$showNumPosts}, sort=-date"); } $t = new TemplateFile(wire('config')->paths->templates . 'markup/categories.php'); $t->set('categories', $categories); return $t->render(); }
/** * Render a list of tags * * As seen on: /tags/ * * @param PageArray $tags * @return string * */ function renderTags(PageArray $tags) { foreach ($tags as $tag) { $tag->numPosts = wire('pages')->count("template=post, tags={$tag}"); } $t = new TemplateFile(wire('config')->paths->templates . 'markup/tags.php'); $t->set('tags', $tags); return $t->render(); }
/** * Render archives returned by the getArchives() function * * Archives links include a year headline followed by a list of months in that year with posts, * and the number of posts in each month. * * @param array $years as returned by the getArchives() function * @return string * */ function renderArchives(array $years) { $out = ''; foreach ($years as $year) { $t = new TemplateFile(wire('config')->paths->templates . 'markup/archives.php'); $t->set('year', $year['name']); $t->set('total', $year['total']); $t->set('months', $year['months']); $t->set('url', $year['url']); $out .= $t->render(); } return $out; }
$template->set('builddate', $firmware->builddate); $template->set('release', $firmware->release); $template->set('hash', $firmware->hash ? $firmware->hash->md5 : ""); $template->set('filename', $firmware->filename); $template->set('url', "http://firmware.freifunk-myk.de" . $key); if ($firmware->sysupgrade) { if ($firmware->branch == "stable") { $stableSysupgrade .= $template->render(); } else { if ($firmware->branch == "beta") { $betaSysupgrade .= $template->render(); } } } else { if ($firmware->branch == "stable") { $stableFactory .= $template->render(); } else { if ($firmware->branch == "beta") { $betaFactory .= $template->render(); } } } } $table = new TemplateFile($config->paths->templates . "markup/router_firmware_accordion.inc"); $table->set('stableSysupgrade', $stableSysupgrade); $table->set('stableFactory', $stableFactory); $table->set('betaSysupgrade', $betaSysupgrade); $table->set('betaFactory', $betaSysupgrade); $page->set('firmwareTable', $table->render()); $page->set('headline', $page->parent->title . " " . $page->title); $content = renderPage();
$superuserRole = $roles->get('superuser'); $authors = $users->find("roles={$authorRole}|{$superuserRole}, sort=title"); $authorLinks = array(); foreach ($authors as $a) { // we set a separate URL (url2) to reflect the public url of the author, since // the author's $author->url is actually a page in the admin $a->url2 = $page->url . $a->name . '/'; $authorLinks[$a->url2] = $a->get('title|name'); } if ($input->urlSegment1) { // author specified: display biography and posts by this author $name = $sanitizer->pageName($input->urlSegment1); $author = $users->get($name); if (!$author->id || !$author->hasRole($authorRole) && !$author->isSuperuser()) { throw new Wire404Exception(); } $posts = $pages->find("template=post, created_users_id={$author}, sort=-date, limit=10"); $authorName = $author->get('title|name'); $t = new TemplateFile($config->paths->templates . "markup/author.php"); $t->set('authorName', $authorName); $t->set('authorURL', ''); $t->set('author', $author); $headline = $page->title; $content = $t->render() . renderPosts($posts, true); $subnav = renderNav($page->title, $authorLinks, $page->url . $author->name . '/'); } else { // no author specified: display list of authors $headline = $page->title; $content = $page->body . renderAuthors($authors); } include "./main.inc";
/** * Given a filename, render it as a ProcessWire template file * * This is a shortcut to using the TemplateFile class. * * File is assumed relative to /site/templates/ (or a directory within there) unless you specify a full path. * If you specify a full path, it will accept files in or below site/templates/, site/modules/, wire/modules/. * * Note this function returns the output for you to output wherever you want (delayed output). * For direct output, use the wireInclude() function instead. * * @param string $filename Assumed relative to /site/templates/ unless you provide a full path name with the filename. * If you provide a path, it must resolve somewhere in site/templates/, site/modules/ or wire/modules/. * @param array $vars Optional associative array of variables to send to template file. * Please note that all template files automatically receive all API variables already (you don't have to provide them) * @param array $options Associative array of options to modify behavior: * - defaultPath: Path where files are assumed to be when only filename or relative filename is specified (default=/site/templates/) * - autoExtension: Extension to assume when no ext in filename, make blank for no auto assumption (default=php) * - allowedPaths: Array of paths that are allowed (default is templates, core modules and site modules) * - allowDotDot: Allow use of ".." in paths? (default=false) * - throwExceptions: Throw exceptions when fatal error occurs? (default=true) * @return string|bool Rendered template file or boolean false on fatal error (and throwExceptions disabled) * @throws WireException if template file doesn't exist * */ function wireRenderFile($filename, array $vars = array(), array $options = array()) { $paths = wire('config')->paths; $defaults = array('defaultPath' => $paths->templates, 'autoExtension' => 'php', 'allowedPaths' => array($paths->templates, $paths->adminTemplates, $paths->modules, $paths->siteModules), 'allowDotDot' => false, 'throwExceptions' => true); $options = array_merge($defaults, $options); if (DIRECTORY_SEPARATOR != '/') { $filename = str_replace(DIRECTORY_SEPARATOR, '/', $filename); } // add .php extension if filename doesn't already have an extension if ($options['autoExtension'] && !strrpos(basename($filename), '.')) { $filename .= "." . $options['autoExtension']; } if (!$options['allowDotDot'] && strpos($filename, '..')) { // make path relative to /site/templates/ if filename is not an absolute path $error = 'Filename may not have ".."'; if ($options['throwExceptions']) { throw new WireException($error); } wire()->error($error); return false; } if ($options['defaultPath'] && strpos($filename, './') === 0) { $filename = rtrim($options['defaultPath'], '/') . '/' . substr($filename, 2); } else { if ($options['defaultPath'] && strpos($filename, '/') !== 0) { // filename is relative to defaultPath (typically /site/templates/) $filename = rtrim($options['defaultPath'], '/') . '/' . $filename; } else { if (strpos($filename, '/') !== false) { // filename is absolute, make sure it's in a location we consider safe $allowed = false; foreach ($options['allowedPaths'] as $path) { if (strpos($filename, $path) === 0) { $allowed = true; } } if (!$allowed) { $error = "Filename {$filename} is not in an allowed path."; if ($options['throwExceptions']) { throw new WireException($error); } wire()->error($error); return false; } } } } // render file and return output $t = new TemplateFile(); $t->setThrowExceptions($options['throwExceptions']); $t->setFilename($filename); foreach ($vars as $key => $value) { $t->set($key, $value); } return $t->render(); }
<?php $output_posts = ''; $posts = $pages->find("template=post,sort=-date"); foreach ($posts as $post) { $template = new TemplateFile($config->paths->templates . "markup/post_small.inc"); $template->set('title', $post->title); $template->set('date', $post->getUnformatted('date')); $template->set('url', $post->httpUrl); $template->set('image', count($post->images) ? "<img class='responsive' src='{$post->images->first()->size(300, 200)->url}'></img>" : ""); $template->set('body', $post->get('summary|body')); $template->set('id', $post->id); $output_posts .= $template->render(); } $template = new TemplateFile($config->paths->templates . "markup/list_blog.inc"); $template->set('posts', $output_posts); $content = $template->render();
/** * Get the output TemplateFile object for rendering this page * * You can retrieve the results of this by calling $page->out or $page->output * * @internal This method is intended for internal use only, not part of the public API. * @param bool $forceNew Forces it to return a new (non-cached) TemplateFile object (default=false) * @return TemplateFile * */ public function output($forceNew = false) { if ($this->output && !$forceNew) { return $this->output; } if (!$this->template) { return null; } $this->output = new TemplateFile(); $this->output->setThrowExceptions(false); $this->output->setFilename($this->template->filename); $fuel = self::getAllFuel(); $this->output->set('wire', $fuel); foreach ($fuel as $key => $value) { $this->output->set($key, $value); } $this->output->set('page', $this); return $this->output; }
/** * Execute the process and return the resulting content generated by the process * * @return string * @throws ProcessController404Exception * */ public function ___execute() { $content = ''; $method = ''; $debug = $this->wire('config')->debug; $breadcrumbs = $this->wire('breadcrumbs'); $headline = $this->wire('processHeadline'); $numBreadcrumbs = $breadcrumbs ? count($breadcrumbs) : null; if ($process = $this->getProcess()) { if ($method = $this->getProcessMethodName($this->process)) { $className = $this->process->className(); if ($debug) { Debug::timer("{$className}.{$method}()"); } $content = $this->process->{$method}(); if ($debug) { Debug::saveTimer("{$className}.{$method}()"); } if ($method != 'execute') { // some method other than the main one if (!is_null($numBreadcrumbs) && $numBreadcrumbs === count($breadcrumbs)) { // process added no breadcrumbs, but there should be more if ($headline === $this->wire('processHeadline')) { $process->headline(str_replace('execute', '', $method)); } $moduleInfo = $this->wire('modules')->getModuleInfo($process); $href = substr($this->wire('input')->url(), -1) == '/' ? '../' : './'; $process->breadcrumb($href, $moduleInfo['title']); } } } else { throw new ProcessController404Exception("Unrecognized path"); } } else { throw new ProcessController404Exception("The requested process does not exist"); } if (empty($content) || is_bool($content)) { $content = $this->process->getViewVars(); } if (is_array($content)) { // array of returned content indicates variables to send to a view if (count($content)) { $viewFile = $this->getViewFile($this->process, $method); if ($viewFile) { // get output from a separate view file $template = new TemplateFile($viewFile); foreach ($content as $key => $value) { $template->set($key, $value); } $content = $template->render(); } } else { $content = ''; } } return $content; }
<?php $routers = $pages->find("template=router, sort=title"); $table_tr = ""; foreach ($routers as $router) { $companyRouter = strtolower($router->parent->title) . "-" . strtolower($router->title); $firmwareList = json_decode(file_get_contents("http://firmware.freifunk-myk.de/.static/filter/?filter={$companyRouter}&branch[]=stable&branch[]=beta&output=json")); foreach ($firmwareList as $key => $firmware) { $template = new TemplateFile($config->paths->templates . "markup/router_firmware_table_tr.inc"); $template->set('version', $firmware->version); $template->set('builddate', $firmware->builddate); $template->set('release', $firmware->release); $template->set('hash', $firmware->hash ? $firmware->hash->md5 : ""); $template->set('filename', $firmware->filename); $template->set('url', "http://firmware.freifunk-myk.de" . $key); $table_tr .= $template->render(); } $template = new TemplateFile($config->paths->templates . "markup/router_firmware_table.inc"); $template->set('table', $table_tr); $firmwareTable = $template->render(); $content .= "<h2>{$router->title}</h2>{$firmwareTable}"; }
/** * gets the available composer templates * used for editing instances of the BlockType while in the composer ui in the dashboard * @return TemplateFile[] */ function getBlockTypeComposerTemplates() { $btHandle = $this->getBlockTypeHandle(); $files = array(); $fh = Loader::helper('file'); $dir = DIR_FILES_BLOCK_TYPES . "/{$btHandle}/" . DIRNAME_BLOCK_TEMPLATES_COMPOSER; if (is_dir($dir)) { $files = array_merge($files, $fh->getDirectoryContents($dir)); } $dir = DIR_FILES_BLOCK_TYPES_CORE . "/{$btHandle}/" . DIRNAME_BLOCK_TEMPLATES_COMPOSER; if (file_exists($dir)) { $files = array_merge($files, $fh->getDirectoryContents($dir)); } Loader::library('template_file'); $templates = array(); foreach (array_unique($files) as $file) { $templates[] = new TemplateFile($this, $file); } return TemplateFile::sortTemplateFileList($templates); }
/** Callable function used by sortTemplateFileList. * @param TemplateFile $a * @param TemplateFile $b * @return int */ protected static function sortTemplateFileListSorter($a, $b) { return strcasecmp($a->getTemplateFileDisplayName('text'), $b->getTemplateFileDisplayName('text')); }