Get Smarty, No tv, just PHP code

So this week I started working on a new project to port some on premise software into a shared hosted model.  My first thought of course was some type of MVC pattern.  I stumbled across CakePHP and some others, but ended with using the smarty template engine.  This is my first time using a template engine in PHP, but so far I really like it.  This allows a much better separation of concerns in my mind with keeping all the business logic in the php file and then passing in the data to be used in the view, or template in this instance.  This keeps the display file easily readable, and the php code clean.

What I quickly found as I went to start implementing it is that instead of doing my usual static class with configuration information, I was able to include all of that into a special section of the smarty config file.  This greatly simplified my code in a lot of ways.  One problem that did take me a while to get around however was using hidden config sections.  For instance, in my data access classes I will load the hidden config section to get the database connection details.  After a fair amount of googling I was able to finally find a way to accomplish this in code.  An example of this is below…

public function Db() {
    $smarty = new Smarty();
    $smarty->config_read_hidden = true;
    $smarty->configLoad('my.conf', 'database');
    // now that the smarty config has been read, set the private class variables
    $this->host = $smarty->getConfigVariable("host");
    $this->user = $smarty->getConfigVariable("user");
    $this->password = $smarty->getConfigVariable("password");
    $this->dbname = $smarty->getConfigVariable("database");

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s