public function __construct() { // Smarty can resolve the template automatically too, providing I send in the directories. $this->getSmarty()->addTemplateDir(Templates\Template::GetPaths()); // Tack on the search directories from the loaded components. // Also handle the plugins directory search. foreach (\Core::GetComponents() as $c) { /** @var \Component_2_1 $c */ $plugindir = $c->getSmartyPluginDirectory(); if ($plugindir) $this->getSmarty()->addPluginsDir($plugindir); foreach($c->getSmartyPlugins() as $name => $call){ if(strpos($call, '::') !== false){ $parts = explode('::', $call); $this->getSmarty()->registerPlugin('function', $name, $parts); } else{ $this->getSmarty()->registerPlugin('function', $name, $call); } } } }
public function render(){ if(!$this->get('templatename')){ throw new Exception('Unable to render pageselectinput element without templatename set!'); } // Figure out the template directory for custom pages, (if it exists) // In order to get the types, I need to sift through all the potential template directories and look for a directory // with the matching name. $tmpname = substr($this->get('templatename'), 0, -4) . '/'; $matches = array(); foreach(\Core\Templates\Template::GetPaths() as $d){ if(is_dir($d . $tmpname)){ // Yay, sift through that and get the files! $dir = \Core\Filestore\Factory::Directory($d . $tmpname); foreach($dir->ls('tpl') as $file){ // Skip directories if($file instanceof \Core\Filestore\Directory) continue; /** @var $file \Core\Filestore\File */ //$fullpath = $tmpname . $file->getBaseFilename(); $fullpath = $file->getBaseFilename(); $name = $file->getBaseFilename(); // Do some template updates and make it a little more friendlier to read. $name = ucwords(str_replace('-', ' ', substr($name, 0, -4))) . ' Template'; $matches[ $fullpath ] = $name; } } } // Are there any matches? If not just return a blank string. if(!sizeof($matches)){ return ''; } $options = array_merge(array('' => '-- Default Page Template --'), $matches); $this->set('options', $options); return parent::render(); }
/** * Get an array of alternative display templates for this instance. * * This is based on the widget's baseurl. * * @return array */ public function getAlternativeTemplateOptions(){ $parts = $this->splitParts(); // Figure out the template directory for custom pages, (if it exists) // In order to get the types, I need to sift through all the potential template directories and look for a directory // with the matching name. $tmpname = 'widgets' . strtolower('/' . substr($parts['controller'], 0, -6) . '/' . $parts['method']); $matches = []; foreach(\Core\Templates\Template::GetPaths() as $d){ if(is_dir($d . $tmpname)){ // Yay, sift through that and get the files! $dir = \Core\Filestore\Factory::Directory($d . $tmpname); foreach($dir->ls('tpl') as $file){ // Skip directories if($file instanceof \Core\Filestore\Directory) continue; /** @var $file \Core\Filestore\File */ //$fullpath = $tmpname . $file->getBaseFilename(); $name = $fullpath = $file->getBaseFilename(); // Do some template updates and make it a little more friendlier to read. $name = ucwords(str_replace('-', ' ', substr($name, 0, -4))) . ' Template'; $matches[ $fullpath ] = $name; } } } return ['' => '-- Default Template --'] + $matches; }
/** * Inject a CSS file or snippet into the head of a page. * * This is the recommended way to inject stylesheets into your application. * * Any inline styles or links to stylesheets added via the `{css}` smarty block are automatically moved into the head of the document. * Redundant file includes and inline styles are omitted automatically. * * CSS files have their minified version sent automatically when the Core config option is set to do so. * * #### Smarty Parameters * * * media * * (string) The media attribute, defaults to "all". * * href * * The source of the linked CSS asset. * * Can be fully resolved or a Core asset/* path. * * link * * alias of href. * * src * * alias of href. * * optional * * Set to "1" if this is an optional stylesheet where the admin can toggle on/off its inclusion. * * Currently only supported in theme skins. * * default * * If optional="1", this is if the file is included by default or not. * * title * * If optional="1", this is an optional title displayed for the admin. * * #### Example Usage * * Include an asset file located in css/ called styles.css. * <pre> * {css src="css/styles.css"}{/css} * </pre> * * (For a theme skin), provide the admin with the option to include this stylesheet * <pre> * {css src="css/opt/full-width.css" optional="1" default="0" title="Set the page to be full width"}{/css} * </pre> * * Inject these styles into the page * <pre> * {css} * <style> * .blah { * width: auto; * } * </style> * {/css} * </pre> * * @param array $params Associative (and/or indexed) array of smarty parameters passed in from the template * @param string|null $content Null on opening pass, rendered source of the contents inside the block on closing pass * @param Smarty $smarty Parent Smarty template object * @param boolean $repeat True at the first call of the block-function (the opening tag) and * false on all subsequent calls to the block function (the block's closing tag). * Each time the function implementation returns with $repeat being TRUE, * the contents between {func}...{/func} are evaluated and the function implementation * is called again with the new block contents in the parameter $content. */ function smarty_block_css($params, $content, $smarty, &$repeat){ // This only needs to be called once. if($repeat) return; // media type is the first parameter to check for. $media = (isset($params['media'])) ? $params['media'] : 'all'; $inline = isset($params['inline']) && $params['inline'] == '1' ? true : false; $tmpl = $smarty->getTemplateVars('__core_template'); $view = ($tmpl instanceof \Core\Templates\TemplateInterface) ? $tmpl->getView() : \Core\view(); // See if there's a "href" set. If so, that's probably an asset. // I have a tendency of calling this different things, since things in the head all have // different names for this crap! // as such, support a bunch of different properties.... $href = null; if(isset($params['href'])) $href = $params['href']; elseif(isset($params['link'])) $href = $params['link']; elseif(isset($params['src'])) $href = $params['src']; // Standard include from an asset. if($href !== null){ // If optional is set, then look up the data to see if it's set. if(isset($params['optional']) && $params['optional']){ $file = $smarty->template_resource; // Trim off the base directory. $paths = \Core\Templates\Template::GetPaths(); foreach($paths as $p){ if(strpos($file, $p) === 0){ $file = substr($file, strlen($p)); break; } } // Look up and see if this css is requested to be loaded by the user. $model = TemplateCssModel::Construct($file, $href); $enabled = $model->exists() ? $model->get('enabled') : (isset($params['default']) ? $params['default'] : 0); if(!$enabled) return; } if($inline){ // Allow stylesheets to be rendered "in-line" in the code. // This is only really useful for emails and other HTML fragments. $file = Core\Filestore\resolve_asset_file($href); if(\ConfigHandler::Get('/core/javascript/minified')){ // Remove the extension from the filename, (makes the logic cleaner). $dir = $file->getDirectoryName(); $filename = $file->getBaseFilename(true); $ext = $file->getExtension(); // Core is set to use minified css and javascript assets, try to locate those! // I need to do the check based on the base $filename, because 'assets/css/reset.css' may reside in one // of many locations, and not all of them may have a minified version. // Try to load the minified version instead. $minified = $filename . '.min.' . $ext; $minfile = \Core\Filestore\Factory::File($dir . $minified); if($minfile->exists()){ // Overwrite the $file variable so it's returned instead. $file = $minfile; } } $view->addStyle('<style media="' . $media . '">' . $file->getContents() . '</style>'); } else{ $view->addStylesheet($href, $media); } } // Styles defined inline, fine as well. The styles will be displayed in the head. elseif($content){ $view->addStyle($content); } }