Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Multiple Styles / Themes and some questions
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
iriePub
Smarty Regular


Joined: 16 Jun 2006
Posts: 53

PostPosted: Fri Jun 16, 2006 2:08 pm    Post subject: Multiple Styles / Themes and some questions Reply with quote

Hi,

I'm new to Smarty, so I don't know much about it. But at least, I' not new to php. So I read the manual of smarty, but I have still several questions.

I want my site to be very very good skinnable via themes / styles. Later I will give everbody the chance to develop own themes and share them.

Since it should be easy to install them, it would be nice, if css file, js files, image files and templates are in one subdir of the theme's dir.

I saw this thread and read it: http://www.phpinsider.com/smarty-forum/viewtopic.php?t=7310 !
Since I'm not english, I didn't really understand it!

But I thought about a good structure. Would it be possible to do something like this:

Code:

// we are in the root direction of htdocs:

define('SMARTY_DIR', 'smarty/');
include(SMARTY_DIR . 'Smarty.class.php');

$smarty = new Smarty;
$smarty->compile_dir = 'smarty/compiledir/';
$smarty->cache_dir = 'smarty/cachedir/';


This would be the base. Now, I would create a directory called 'styles' and for each style a new subdir with the style's name. In this subdir, I would add the following subdirs: templates, css, js, config!

This would look like this:
Code:

// We are in htdocs now

styles/style1/config
styles/style1/css
styles/style1/js
styles/style1/images
styles/style1/templates

styles/style2/config
styles/style2/css
styles/style2/js
styles/style2/images
styles/style2/templates

This would be very usufull because all files related to a style would be in one folder, that could be zipped and offered to download, wouldn't it?

And now, I would declare the following vars as constants and assign them with smarty, so that the paths are correct:

[php:1:dc1983d492]
// whitelist of possible styles:
$styles = array();
$styles[] = 'style1';
$styles[] = 'style2';

if(isset($_COOKIE['style']) && in_array($_COOKIE['style'], $styles))
$style = $_COOKIE['style'];
else
$style = 'style1';

define('jsDir', 'styles/' . $style . '/js/');
define('cssDir', 'styles/' . $style . '/css/');
define('tplDir', 'styles/' . $style . '/templates/');
define('imgDir', 'styles/' . $style . '/img/');
define('cfgDir', 'styles/' . $style . '/config/');
define('SMARTY_DIR', 'smarty/');
include(SMARTY_DIR . 'Smarty.class.php');

$smarty = new Smarty;

// Default directories:
$smarty->compile_dir = SMARTY_DIR . 'compiledir/';
$smarty->cache_dir = SMARTY_DIR . 'cachedir/';

// Style specific directories:
$smarty->config_dir = cfgDir;
$smarty->template_dir = tplDir;

// And now, here I would assign vars and display the templates
// I would have to assign the css, img and the js dir, so that it could be
// used in the templates file to point to the correct directory:
$smarty->assign('__imgDir', imgDir);
$smarty->assign('__cssDir', cssDir);
$smarty->assign('__jsDir', jsDir);

// ... some more vars ...
$smarty->display('tplname.tpl');
[/php:1:dc1983d492]

So I could use in my templates vars like the following to point to the right css file relating to the actual style:

Code:

// header.tpl:
<html>
<head>
 <title>{$title}</title>
 <link rel="stylesheet" type="text/css" href="{$__cssDir}style.css" />
 <script src="{$__jsDir}scripts.js" type="text/javascript"></script>   
 <link rel="SHORTCUT ICON" href="{$__imgDir}fav.ico" />
</head>
<body>
<img src="{$__imgDir}logo.jpg" />


Is this possible and good to be very independent and to be able to release own style packages, that comes along with own js functions, own images and own css files!?

And comments please?

Thanks!
Back to top
View user's profile Send private message
iriePub
Smarty Regular


Joined: 16 Jun 2006
Posts: 53

PostPosted: Sat Jun 17, 2006 11:37 am    Post subject: Reply with quote

Just a little statemen? I tried it myself, and I have to say, it works really great defining the paths to the actual theme in a constant! What do you think?
Back to top
View user's profile Send private message
d[esign]Void
Smarty n00b


Joined: 28 Nov 2006
Posts: 3

PostPosted: Tue Nov 28, 2006 8:22 pm    Post subject: Reply with quote

Well.... I have tried this and have run into a few issues, I'm a relative noobie to SMARTY so go easy on me! Laughing

I have got it set up as described above but I cannot for the life of me get it to switch between the different style tpl files... Im hard coding the default 'else' value set here:

Code:
if(isset($_COOKIE['style']) && in_array($_COOKIE['style'], $styles))
$style = $_COOKIE['style'];
else
$style = 'style1';


So that it has different style which I assume should make it switch?

My limited knowledge is telling me that it has to do with caching etc, but I have to admit I'm at a loss - could any friendly souls give me any insight?

TIA,

d.
Back to top
View user's profile Send private message
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Tue Nov 28, 2006 8:43 pm    Post subject: Reply with quote

Try setting up a compile_id based on your cookie
_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
d[esign]Void
Smarty n00b


Joined: 28 Nov 2006
Posts: 3

PostPosted: Tue Nov 28, 2006 8:50 pm    Post subject: Reply with quote

TGKnIght wrote:
Try setting up a compile_id based on your cookie


Thanks for quick reply, any chance you could elaborate slightly, as I said, Im a bit of a n00b... Embarassed
Back to top
View user's profile Send private message
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Tue Nov 28, 2006 9:15 pm    Post subject: Reply with quote

Hi sorry for the short response... I was attacked by a coworker for information Wink

Make sure to clear your cache/compile directories after major changes like paths etc...

If the default style loads up fine the first time, but the style will not change, then it sounds like a compiling issue..

http://smarty.php.net/manual/en/variable.compile.id.php
compile_id will let you use the same compile_dir for multiple template dirs...

so you could do something like :

Code:

$smarty->compile_id = $style;


I'll assume that you don't have caching turned on right now (default installation has caching off)
_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
d[esign]Void
Smarty n00b


Joined: 28 Nov 2006
Posts: 3

PostPosted: Mon Dec 04, 2006 4:34 pm    Post subject: Reply with quote

Hi again! Sorry for the extended delay in replying RL got in the way!

I have got it working, however I fear my n00bish skills may be causing me to miss something! I'm now using:

Code:
$smarty->clear_cache('index.tpl');
$smarty->clear_compiled_tpl();
$smarty->display('index.tpl');


And it works, but could someone let me know if there are any inherent issues with doing it this way? Bearing in mind that I ahve no interest in caching as every page has dynamic content.

TIA,

d.
Back to top
View user's profile Send private message
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Mon Dec 04, 2006 6:46 pm    Post subject: Reply with quote

Don't confuse caching and compiling..

If you do not have caching turned on.. then
Code:

$smarty->clear_cache('index.tpl');

is not neccesary.

You will be killing your server by doing this every call :
Code:

$smarty->clear_compiled_tpl();


Please read my suggestion above about compile_id to avoid having to do this...
_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Mon Dec 04, 2006 6:47 pm    Post subject: Reply with quote

Don't confuse caching and compiling..

If you do not have caching turned on.. then
Code:

$smarty->clear_cache('index.tpl');

is not neccesary.

You will be killing your server by doing this every call :
Code:

$smarty->clear_compiled_tpl();


Please read my suggestion above about compile_id to avoid having to do this...
_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
Ancient
Smarty Pro


Joined: 07 Jul 2007
Posts: 196
Location: Omaha, Nebraska, United States of America

PostPosted: Sun Jun 01, 2008 2:31 am    Post subject: Reply with quote

I've looked at this and seems pretty good, but if we submit $smarty->template_dir, won't that cause all other users template dir to change too?
Code:
//
// Define as array
//
$styles = array();
$list_styles = $DBSQL->Query("SELECT * FROM `" . _STYLES_TABLE_ . "`");
while($row = $DBSQL->Fetch_Array($list_styles))
   {
      //
      // Add Style / Theme / Templates name into the array
      //
      $styles[] = $row['name'];
   }
if(count($styles) > 1)
   {
      for($i = 0; $i < count($styles); $i++)
         {
            //
            // Find Users Template and see if is in styles array (That is bigger than 1)
            //
            if($styles[$i] == find_template($user_id))
               {
                  $style_path = find_template($user_id);
               }
         }
   }
else{
      //
      //  There is only one defined theme / template, check to see if the style matches the users.
      //
      if($siteconfig['style'] == find_template($user_id))
         {
            $style_path = $siteconfig['style'];
         }
   }
$CFE->template_dir = 'templates' . '/' . $style_path;

_________________
Smarty all the way.
Back to top
View user's profile Send private message Send e-mail MSN Messenger
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5067
Location: Hamburg / Germany

PostPosted: Sun Jun 01, 2008 7:01 am    Post subject: Reply with quote

If you use different templates with same name you have to use unique compile_id to the compiled templates and cache files seperated.

It could be something like this:

$smarty->compile_id = md5($style_path);
Back to top
View user's profile Send private message
Ancient
Smarty Pro


Joined: 07 Jul 2007
Posts: 196
Location: Omaha, Nebraska, United States of America

PostPosted: Sun Jun 01, 2008 8:09 pm    Post subject: Reply with quote

If they are being placed in different cache folders, why do their compile id have to be different?
Code:
$CFE->template_dir = 'templates' . '/' . $style_path;
$CFE->compile_id = md5($style_path);
$CFE->cache_dir = 'cache' . '/' . md5($style_path);
//
// Incase the template cache folder hasn't been made yet.
//
if(!file_exists($siteconfig['path'] . '/' . 'cache' . '/' . md5($style_path)))
   {
      mkdir($siteconfig['path'] . '/' . 'cache' . '/' . md5($style_path));
   }

_________________
Smarty all the way.
Back to top
View user's profile Send private message Send e-mail MSN Messenger
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5067
Location: Hamburg / Germany

PostPosted: Sun Jun 01, 2008 8:59 pm    Post subject: Reply with quote

Templates are first compiled into PHP code, which is done once after a template is created or modified. The compiled templates are stored in the folder of $smarty->compile_dir.

The compiled template is used to render the HTML output with the assigned content. If you use caching the rendered output is cached in the folder of $smarty->cache_dir.

If you are using a compile_id for your different styles there is no need that you create subfolders for the compile_dir and cache_dir. Smarty is doing it automatically to keep the different styles separated.

Now there is also a cache_id. This can be used if you want to cache different content which is rendered with the same (style) template.

So compile_id keeps different templates with same name separated and cache_id keeps different content rendered with the same template separeted. It's all build in. No need to create subdirs manually.
Back to top
View user's profile Send private message
Ancient
Smarty Pro


Joined: 07 Jul 2007
Posts: 196
Location: Omaha, Nebraska, United States of America

PostPosted: Mon Jun 02, 2008 12:58 am    Post subject: Reply with quote

So, would having a diff compile_id and seperated cache folder slow down the performance?
Recently it seems that the load time had shrunk by 3 times.
_________________
Smarty all the way.
Back to top
View user's profile Send private message Send e-mail MSN Messenger
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5067
Location: Hamburg / Germany

PostPosted: Mon Jun 02, 2008 3:00 pm    Post subject: Reply with quote

Well you are checking each time if the style specific cache folde is already existing. this is small overhead. Again if you use compile_id you do not need to create style specific cache folders. The compile_id should not reduce performance as it is just assembled into the file name to make them unique.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP