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

Constructor defaults: function Smarty ( $base_dir = '.' )

 
Post new topic   Reply to topic    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  
Author Message
smathy
Smarty n00b


Joined: 12 Feb 2004
Posts: 2

PostPosted: Thu Feb 12, 2004 3:21 am    Post subject: Constructor defaults: function Smarty ( $base_dir = '.' ) Reply with quote

I've only just started using Smarty, and I've taken a good look around and can't believe that there isn't a simple way of avoiding the four assignments after every Smarty object instantiation.

The docs point out that the default behaviour of having 'templates', 'templates_c', 'cache' and 'config' in the web root is a bad idea, and yet the only way to set all four is four separate assignments after every single Smarty instantiation.

My feature proposal is to provide a default argument to the Smarty construction which would then become this:

Code:
function Smarty( $base_dir = '.')
{
  $this->template_dir    = $base_dir.'/'.$this->template_dir;
  $this->compile_dir     = $base_dir.'/'.$this->compile_dir;
  $this->config_dir      = $base_dir.'/'.$this->config_dir;
  $this->cache_dir       = $base_dir.'/'.$this->cache_dir;

  $this->assign('SCRIPT_NAME', isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME']
                : @$GLOBALS['HTTP_SERVER_VARS']['SCRIPT_NAME']);
}


Then from the caller, the ugly four assignment statements disappear and the code becomes:

Code:
$smarty = new Smarty('/directory/for/this/app');


Alternatively, you could extend the concept to include other defaults, such as delimiters:

Code:
function Smarty( $base_dir = '.', $delim = array('left' => '{', 'right' => '}'))
{
  $this->template_dir    = $base_dir.'/'.$this->template_dir;
  $this->compile_dir     = $base_dir.'/'.$this->compile_dir;
  $this->config_dir      = $base_dir.'/'.$this->config_dir;
  $this->cache_dir       = $base_dir.'/'.$this->cache_dir;

  $this->left_delimiter = $delim['left'];
  $this->right_delimiter = $delim['right'];

  $this->assign('SCRIPT_NAME', isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME']
                : @$GLOBALS['HTTP_SERVER_VARS']['SCRIPT_NAME']);
}


Sure would make the code in the caller a lot cleaner.

Has this been discussed before? Aren't there others sick of the four assignments after every single instantiation?

--
Jason King
Back to top
View user's profile Send private message
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Thu Feb 12, 2004 1:25 pm    Post subject: Reply with quote

Normally you should create your own template class which handles setting the parameters for you Smile

Code:

  class mySmarty extends Smarty {
    function mySmarty() {
      $this->Smarty();
      $this->template_dir = " ... ";
      $this->compile_dir = " ... ";
      $this->config_dir = " ... ";
      $this->plugin_dir = " ... ";
      $this->left_delimiter = "{";
      $this->right_delimiter = "}";
       // ...
    }
  }


Now just use your custom class instead of the original one.
Code:
$Smarty = new mySmarty();
$Smarty->assign(...);
$Smarty->display(...);
Back to top
View user's profile Send private message
smathy
Smarty n00b


Joined: 12 Feb 2004
Posts: 2

PostPosted: Thu Feb 12, 2004 10:29 pm    Post subject: Reply with quote

Thank you for your reply, I didn't know whether what I had written was going to be received or ignored.

Certainly you've provided an excellent and very workable solution, and one that hadn't occurred to me.

I'd be interested on further comments because at first glance it doesn't appease my OO methodology appetite quite as well as the solution I proposed because I see the location of the templates (etc.) directory more as an attribute of a given instance of a Smarty rather than being a property of a new subclass.

Although I can certainly see the scope for having a custom class for each application, so you might have MessageBoardSmarty which would then define - as you wrote - the attributes for its templates (etc.) location.

A new subclass for every application on your system simply to set that attribute seems like overkill to me, or perhaps that's too strong - let's say that it seems less elegant when compared with such a simple solution (and such a backwardly compatible one) as I proposed.

--
Jason King
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Feature Requests All times are GMT
Page 1 of 1

 
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