RE: [SMARTY] Including CSS

From: Date: Wed, 21 Apr 2004 17:46:25 +0000
Subject: RE: [SMARTY] Including CSS
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-11290@lists.php.net to get a copy of this message
SG wrote: > This seems basic, but how do you include a css stylesheet > in a smarty template? I have no trouble including inline styles > using {literal}{/literal}, but including <link rel="stylesheet" href= > "../css/style.css" type="text/css"> between the <head> tags does nothing > even though > it works in a regular html page [ie, not one called via > $smarty->display()]. > I've tried putting {literal} tags around the styles in the external > sheet (as well as not putting them), & importing the sheet via @ import > url or > including the file via {include file=""}, but nothing > goes. The docs indicate this is doable: > > "The simplest thing is to avoid the situation altogether by separating > your > Javascript and CSS code into their own files and then using standard > HTML methods to access them. " > > What am I missing? Does s/o have an example of a template that includes > a stylesheet that works? Are .conf files supposed to replace > css stylesheets? Thanks in advance, > > s. > In my Smarty config script, I always create something like this: <?php /*********************************************************************** * * setup.php * * This is a setup file which defines all of the important directories * for Smarty and the php templates. * ************************************************************************ / // load Smarty library files ini_set("include_path","./php/lib/:./php/templates_c/"); require("./php/lib/Smarty.class.php"); /*---------------------------------------------------------- * Define an array of the directory structure for the site *----------------------------------------------------------*/ class MySmarty extends Smarty { function MySmarty() { // Class Constructor. These get set with each new instance. $this->Smarty(); $this->template_dir = './php/templates/'; $this->compile_dir = './php/templates_c/'; $this->config_dir = './php/configs/'; $this->cache_dir = './php/cache/'; // The url for the site $root = $DOCUMENT_ROOT; // Create an array of directories to be passed around $this->assign('SITE', array("images" => $root."/images/", "logos" => $root."/images/logos/", "javascripts" => $root."/js/", "css" => $root."/css/", "photos" => $root."/photos/", "sounds" => $root."/sounds/", )); // Set to 2 for caching on per individual template $this->caching = 2; } /*---------------------------------------------------------- * clearCache() * * This function will clear out the cache for a specific id. * Expects one argument, the id of the cache *----------------------------------------------------------*/ function clearCache($id) { if(! $id) { die("Cannot clear the cache - missing the id"); } $this->clear_cache(null, $id); } /*---------------------------------------------------------- * clearAllCache() * * This function clears out all of the cache files *----------------------------------------------------------*/ function clearAllCache() { $this->clear_all_cache(); } } ?> Then, in my php script: include("./php/config/setup.php"); // Create a new instance of the Smarty class $smarty = new MySmarty; // Assign the global variables $smarty->assign($home); $smarty->assign($SITE); Now, in my templates, I have access to all the paths in the config file. They are accessed like this: <link href="{$SITE.css}style.css" type="text/css" rel="STYLESHEET"> <img src="{$SITE.images}myimage.jpg"> and so on... HTH -Erich-

« previous php.smarty.general (#11290) next »