Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Be-a Smarty, Have-a PEAR DB object

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
David
Smarty Rookie


Joined: 18 Apr 2003
Posts: 17
Location: New York, NY, USA

PostPosted: Fri Apr 18, 2003 8:05 pm    Post subject: Be-a Smarty, Have-a PEAR DB object Reply with quote

You can use inheritance to leverage all those Smarty brains and add your own as you see fit, like the docs recommend.

You can use aggregation to make your Smarty extension a database wizard, and your database-aware Smarty subclass can be more concise and convenient to write.

Code:

class MySmarty extends Smarty {
    var $db = null;

   function MySmarty {
     // your config here
   }

   function connect() {
        if ($this->db != null) return; // you're already connected
        require_once 'DB.php';
        $db =  DB::connect($DSN,array  ("persistent"=>true,"optimize"=>"performance"));
        if (DB::isError($db)) {  die ($db->getMessage()); }
        $this->db = $db;
   }
}


_________________
David Mintz
http://davidmintz.org/

"Anybody else got a problem with Webistics?" -- Sopranos 24:17
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Apr 18, 2003 11:36 pm    Post subject: Reply with quote

Interesting, but what's the advantage to this vs. using a separate DB class in parallel with Smarty? I don't see a real compelling reason to do this.
Back to top
View user's profile Send private message Visit poster's website
David
Smarty Rookie


Joined: 18 Apr 2003
Posts: 17
Location: New York, NY, USA

PostPosted: Sun Apr 20, 2003 10:25 pm    Post subject: Reply with quote

makes your subclass a little more concise and convenient as there's no need to require DB.php and instantiate an instance.

but hey, if it's too silly, delete it, you're the admin Smile
_________________
David Mintz
http://davidmintz.org/

"Anybody else got a problem with Webistics?" -- Sopranos 24:17
Back to top
View user's profile Send private message
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 235
Location: Purdue University

PostPosted: Sun Apr 20, 2003 10:50 pm    Post subject: Reply with quote

Its a good idea but not exactly practical almost everyone does db access a little different I prefer ADODB you obviously use Pear:Very HappyB and my other just use the native functions. Why should Smarty have something that just adds more overhead without everyone using it. Perhaps if you wrote it up as a drop in plugin then I could see a use. Thats just MHO
_________________
"Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun Apr 20, 2003 11:11 pm    Post subject: Reply with quote

Quote:
Its a good idea but not exactly practical almost everyone does db access a little different I prefer ADODB you obviously use Pear:B and my other just use the native functions. Why should Smarty have something that just adds more overhead without everyone using it. Perhaps if you wrote it up as a drop in plugin then I could see a use.


Gotta agree, except that it may be a good idea to have either an API (like resources) that allows us to create connection and query plugins easily. Or at least a recommendation for plugin-designers. That way common plugin names could be used to connect to dbs, get data, etc.

I have a simple set of plugins that I wrote a while back that does just that: open and close connections, queries sources. Connections are token based with login information hidden in config files. Queries return back a standard array that can be formatted anywhich way (I also have generic 2d table templates).
Back to top
View user's profile Send private message
Mister Internet
Smarty Rookie


Joined: 21 Apr 2003
Posts: 9

PostPosted: Mon Apr 21, 2003 7:52 pm    Post subject: Reply with quote

It's a cool trick, but doesn't this kind of fall outside of acceptable code design? For example, assuming a standard MVC (Model - View - Controller) code design, you would not want to mix Smarty work with DB work. To keep things separated, you would want to make Smarty do all of your "View" functionality, and then your business logic (PEAR, custom functions) would do the "Controller" functionality.

I would not do this for the same reason that I would not instantiate a DB connection directly from a .tpl ... it is simply not good code design. For you Java developers out there, remember the days when we would call database connection directly from the JSP? (Probably about JSP spec 0.91) Those pages were hideous, the code was absolutely unmaintainable, and the heavy lifting of business logic was bogging down the display logic, even for a little bit of real data.

All that to say... it would seem to me to be best to create a Controller function that:


  • Instantiates the DB connection
  • Instantiates the Smarty object
  • Grabs the data from the DB
  • Creates the response and passes the results to Smarty


This creates much cleaner code. This way, Smarty does what it is supposed to do, and you leave to DB, etc to the PHP world, where it belongs...

Thoughts?
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon Apr 21, 2003 9:33 pm    Post subject: Reply with quote

Quote:
It's a cool trick, but doesn't this kind of fall outside of acceptable code design?


Maybe. I agree in principal that different tasks should be kept separate, but I'm not religious about it. What is acceptable, anyhow--and when does it matter? I mean, MVC is way overkill for disconnected web-apps and keeping track of the myriad of classes you end up with is not always "easier". You say less code?

Here is a sample template using my own plugins:

{OPN db=MyDb}
{QRY name=test db=MyDB select=* from=MyTable}
{TBL data=$test}
{CLS db=MyDb}

I know it won't win any awards for design, but that isn't the point. Its terseness makes it very useful for me to quickly explore ideas without having to write both PHP and Smarty code. I bet I don't have to explain what any of the plugin functions do--even to casual users.

The {OPN} and {CLS} can also be automated reducing the template even further. Does it make sense to have PHP code drive this simple type of work? Maybe, but there are cases where it makes just as much sense to me to have it in-template. ESPECIALLY when I want to provide some of that functionality to users who do not have much programming background (or who I don't want messing with the PHP side of the code).

Granted, I rarely use this style for in my final deliverables, but not every website (or application--Smarty is not limited to html production over http) requires the same amount of infrastructure. Besides, if your point is simply for sepatation, then everything you want to template should be reduced to simple strings with replacement values and that is all. Smarty is a non-issue in that case, since PHP is itself a templating language....

SO, does it belong in the template? Well, some will (rightly) say that Smarty is a language in itself. Its not general purpose and it does have an intended use, but that doesn't mean it needs to be pigeonholed, yes?

Perhaps the question here is whether it is worthwhile having such features instead of whether they are acceptable from XY methodology.

As far as methodology goes I prefer KISS and PLS Wink


Last edited by boots on Mon Apr 28, 2003 8:56 am; edited 1 time in total
Back to top
View user's profile Send private message
cauliflo
Smarty n00b


Joined: 24 Apr 2003
Posts: 1
Location: France

PostPosted: Thu Apr 24, 2003 11:55 pm    Post subject: Reply with quote

boots wrote:


{OPN db=MyDb}
{QRY name=test db=MyDB select=* from=MyTable}
{TBL data=$test}
{CLS db=MyDb}


hi,

i used some similar functions for my own use, i.e. a function which fetch all the results from a query passed by parameter given a dsn for connecting to the database.

But i found it afterwards quite ugly, in the way it gives some specific information about the way to retrieve data in the template.

it would be cleaner and faster to fetch all the results in a php script and then send it to the template.

An alternative way to call data from the template would be to use some bean object. For example, you can use PEAR::Dataobject for object/relationnal mapping and then implement a smarty plug-in wrapper for accessing this object and fetching the results.
i agree it's almost the same thing as specifying the request in the template, but this has two advantages :
- it encapsulates the logic in only one line in the template
- the plug-in can be used as a bean for accessing data (in fact it's part of the model, even if it's called by the template, MVC doesn't mean we have to respect the order controller/model/view, in fact the order in the acronym is model/view/controller so... Wink )

hope i'm clear.

by the way, i'm doing a 'little framework' using smarty and tons of PEAR classes (DataObject, MDB, Log, Error, Config, Net_Url, File_Find, QuickForm, Html_Table, Pager_Sliding) and found interesting to follow the double MVC implementation.

a user request a page, which load a PHP script (model) and smarty template (view).
in the page template, there are some blocks which describe which module to load and which action to call on this module. then this module/action determine a php/script and a smarty template for the block. we can have multiple blocks per page.
example : a page containing core/menu, core/login, core/changeTheme, news/last, articles/last, core/search calls six little mvc engines for responding according to the actions.

in this way, the model is loaded from a function in a smarty template... but this still sounds good to me Very Happy


cauliflo
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri Apr 25, 2003 1:18 am    Post subject: Reply with quote

cauliflo, you framework sounds interesting. I've written various systems using a wide variety models (the recursive meta-driven models are neat, but slow, I'm sad to report). As it happens, I'm currently working on something similar to what you describe, though I don't think I'm trying to hit as many features--and I'm not leveraging pear.

Sometimes I like to think of Smarty as a more general purpose tool to get little tasks done easily or to template more difficult tasks. Usually, these usage patterns won't get worked into an application model, but I have found some novel uses.

Of course, when we say that something doesn't belong in template is it because we aim to get the most out of Smarty in terms of its intended function--fast websites? I see 'true' templatating as nothing but replacement--all logic (including looping) handled in code for performance. But that turns out to be very messy and doesn't match too well with actual labour + logical division. I see Smarty as providing more flexibility by reducing the code dependance of traditional templates by providing a verb (commands) / noun (replacements) environment which happily compiles down to code (and caches!). Its an abstraction, a simplification with a muscle punch.

One of the things that it enables is a custom vocabulary for an application (like my simple OPN, CLS, QRY for db query prototyping). Those few verbs and Smarty's clear syntax hide a lot of underlying code and lets me focus on the results instead of the details. One thing that it enables me to do is create highly customized and narrowly focused vocabularies for specific low-volume client tasks. Smarty can do more than websites. Maybe not effeciently in all cases, but with tremendous flexibility. I see no reason not to create classes of templates that do something other than produce html. Instead of thinking of them as smarty templates, I think of them as my-custom-lang-x-smarty file.

Maybe I just like to play with toys, but I admit that I often colour outside the lines Smile

I'm interested to hear more details about your framework, if you'd care to share them. The application model is the most important thing to me in terms of robust high-performance sites, so I'm always interested to see what others are doing. I make a lot of mistakes.
Back to top
View user's profile Send private message
David
Smarty Rookie


Joined: 18 Apr 2003
Posts: 17
Location: New York, NY, USA

PostPosted: Tue May 06, 2003 2:37 pm    Post subject: Reply with quote

Mister Internet wrote:
It's a cool trick, but doesn't this kind of fall outside of acceptable code design? For example, assuming a standard MVC (Model - View - Controller) code design, you would not want to mix Smarty work with DB work. To keep things separated, you would want to make Smarty do all of your "View" functionality, and then your business logic (PEAR, custom functions) would do the "Controller" functionality.


Upon further reflection, yeah, you have a point. I am still learning all this good stuff, so thanks for the help with the learning process.
_________________
David Mintz
http://davidmintz.org/

"Anybody else got a problem with Webistics?" -- Sopranos 24:17
Back to top
View user's profile Send private message
Mister Internet
Smarty Rookie


Joined: 21 Apr 2003
Posts: 9

PostPosted: Thu May 08, 2003 8:30 pm    Post subject: Reply with quote

Hey, we're all still learning... you'll never hear me say I've got ANY programming concept mastered, because I believe that's impossible. In much the same way as "there's always a better mousetrap", I believe there's always a better way to program a problem... we never really come up with the best solutions, simply more ideal ones. Smile

I myself am transitioning from the Java/Enterprise world, and the way "everyone is supposed to code" has been drilled into me pretty heavily. I wish to state that I was not coming down on anyone who was suggesting these shortcut tools... I myself have hacks in place to facilitate quick idea exploration and development. I guess I just assumed "AHHHH PRODUCTION!!!" and went from there. Wink

I am still struggling with making "patterns" fit the "Real world" ... I think everyone does... you know the saying, "You can have programs written fast, well, and cheap, but you only get to pick 2" ? Smile
Back to top
View user's profile Send private message
san20
Smarty n00b


Joined: 06 Mar 2004
Posts: 1

PostPosted: Sat Mar 06, 2004 6:55 am    Post subject: Connect To ADODB Reply with quote

Hi,

Can You help me Integrating Smarty AND ADODB with paging feature
Back to top
View user's profile Send private message
coffee_ninja
Smarty n00b


Joined: 08 Apr 2004
Posts: 1
Location: PA

PostPosted: Thu Apr 08, 2004 7:20 pm    Post subject: PEAR::DB & Smarty Reply with quote

I just started using Smarty and am by no means an expert on either it or PHP. I found it really interesting reading everyone's opinions on either integrating or entirely separating Smarty templating from database access. My solution (probably not an original one) was to extend Smarty, and have the constructor instantiate the database connection, if it is not already. The constructor also turns on output buffering, so output from the PHP code gets slapped into the {$content} in my template. For other portions of my page, such as the Most recent 5 headlines displayed on the left nav bar, I wrote plugins that access the database and return the XML back to smarty.

This approach works great for me right now, but I haven't given much thought to it's performance as my site expands. Does anyone with more experience with Smarty think I will run into any problems with this in the future?
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address
vcrfix
Smarty n00b


Joined: 10 May 2007
Posts: 3

PostPosted: Fri May 11, 2007 11:00 am    Post subject: Reply with quote

"You can have programs written fast, well, and cheap, but you only get to pick 2" ?

Seems that

. well and cheap doesn't work
. fast and well doesn't work

that leaves just

- fast and cheap !

Amazing, when a client wants a 'well' solution, they cannot leave 'cheap' out of the equation!
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks 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