a few questions

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

a few questions

Post by s.dot »

As I'm developing more and more over the years. I find myself rewriting sometimes entire sections of applications to fit the specific need (or site) I'm developing. Now, this could be a forum, with a lot of queries, lots of time debugging, and lots of code. Then I rewrite it all over again to fit a new site. Lots of extra work that I've done before.

So, I'm trying to cut this out. Obviously I'm going to have to write PHP code with any site I develop, but I think I can drastically cut down on the amount of time it takes me to develop a new site... from scratch!

From scratch, I code many of the same things I use in every site I develop over again. Lots of work. So I bring on the questions of templating and frameworks.

Templating

(do you use templating?)

1. Seperating presentation from program code. This would, I think, be the biggest change for me to get used to (being a procedural programmer). Do you advocate this practice?

2. Templating and frameworks, such as smarty. Do you recommend their use? Do you use them?

3. The idea of using a pre-existing program or code (e.g. smarty) to build a new web site or application, makes me feel like I am not the soul producer of the web site.. something I pride myself on. Why would I feel like this? Should I feel like this? Have you felt like this?

4. Obviously the code in such an application would be well-tuned and finely tested. But say there's a security flaw that is exposed to the masses, leaving my applications vulnerable to a fix is released. This would be unavoidable, correct? Say there is a fix or update released.. i update.. it would be to the core files that I didn't write, so it shouldn't effect my coding/presentation. SHOULDN'T, although in some cases, it could, couldn't it?

5. Any other thoughts on templates or seperating presentation from coding?

PHP FrameWorks

1. Do you use one? Have you wrote your own?

2. It seems to me that using a framework would be a bit like learning new php functions. Classes and wrappers, all designed to do what I could do procedurally (albeit, slower) with new function names to learn, new files to include, and a bunch of headaches until I grasp the framework wholey. Even so, would I still feel like I were using PHP, instead of some "language" that's more like an API to PHP?

3. Would using a framework save me lots of time instead of creating the same basic files on every web site/application I make.

4. With the introduction of the Zend framework, will that be the "standard", or, am I "free" to choose any framework I like?

5. Any other thoughts or input is highly valued by me!


All in all the answers and input to these questions will help me decide if I'm ready to ditch my current style of writing and developing, and if this new style (for me) will be any less suitable or controllable than my current procedural programming practices.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Camedo
Forum Newbie
Posts: 9
Joined: Mon May 14, 2007 12:29 am
Location: Gresham, Oregon

Post by Camedo »

I don't have as much experience as most of the others around here, but I can offer what i've learned through my projects, and what i've been using recently.

Templating

A lot of people advocate separating presentation from program code. In fact, there seems to be a large group of followers around the MVC framework design (Model/View/Controller) which achieves something very much like this. From what I gather (in a rather loose grasp, i'm still very new to MVC) it seems to revolve around design views to display data, controllers to access and management data, and models to store the data. It does take some adjusting. I'm still learning the MVC concept though.

I personally use Templating, I find it to be extremely useful. However, I don't use any public packages, I have a much smaller PHP class I use. That way, when I want to render a page, I use the following:

Code: Select all

$Page = new Template( "./template/default.html" );
$Page->assign("title", "Welcome to my Page." );
$Page->assign("content", "This is the content box of the page.");
$Page->render( );
That loads default.html, replaces <TEMPLATE: TITLE/> and <TEMPLATE: CONTENT/> within it, then sends it to the browser. I find it to be very fast and lightweight, yet powerful enough for my needs. Smarty is a beautiful engine, but I don't need an entire scripting engine for my templates, just some simple replacements.

I have the same problem with using pre-existing programs and code, but sometimes you have to swallow your pride and use existing tools. I like to try writing the tools myself, but sometimes it just takes too long to re-invent the wheel. If you have the time to spend on doing it yourself, by all means. But if there's an existing package that fits the specifications, you might consider using it instead.

PHP FrameWorks

I've been reading a lot on frameworks lately, and i'm slowly catching up with the rest of the professional PHP community. I'm still not ready to make the jump to some major public framework like Zend's framework, CakePHP, or any of the others. However, using a framework - once you get adjusted to it - can save a significant amount of time in designing a website or application by reducing the amount of code you have to rewrite, and providing a solid base to build on top of. It can also reduce headaches in the future by helping you stick to a standardized style of coding throughout the project.

My solution for now is simply to write my own framework. I sat down, took two days to look over my past projects and collect my most commonly used classes and code into one location. Then I designed a simple website framework that shares MY design style, which is a combination of procedural and class-oriented code. My current version (1.3) of the Delta Framework contains a starting directory structure (cache, classes, css, images, include, javascript, template), a sample default page (index.php), my bootloader (links in the classes, configuration, function libraries, etc), sample CSS and template, common function library and a selection of my common classes. (Template, Caching, Timer, Form Handler, Session Handler, Database Abstraction)

What this means is that I can unzip the Delta Framework into a project directory, and I have a fully functional - albeit very bare - website. A quick change to the inc.configuration.php in the include directory enables the database connection, and a change to the permissions of the cache folder sets up the caching class. Then I can simply begin writing my pages, instead of piecing this all together each time I start a new project. So far, i've started two new projects using this framework instead of starting from scratch, and i've saved myself about 12 hours total. And the best part is, it suites MY coding style, so it doesn't require very much adjustment. Since it only contains core components too, it's very quick. My current server renders a page in 0.006 seconds on average.


There seems to be a huge debate about which is better, procedural or object-oriented design. They both have pros and cons, it's difficult to say if one is better than the other. I lean more towards object-oriented design, but I'm also staying in PHP4, which means I have to rely on a healthy mix of objects and procedural code. I haven't noticed a problem with this style.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Thanks for your input! As I will be developing a range of different styled sites in the future, I feel if I were to use a templating engine (and more, as smarty puts it), that I should get used to the whole package, so I can quickly develop when making a new site, so I would use something such as smarty.

I've read everything else too, but I don't have much to comment on except that I can see how my initial post can be viewed as an OO vs. procedural post, but this was not intended. I was more concerned with the usage of frameworks (whether I choose to use this in an OO fashion or procedural fashion).
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

Templating
1: Yes, separation of tasks is essential to an efficient application. It's easier to see where an error occurs (and how) and to debug it when your layers are separated. If an error occurs, you know immediately if it happened in your logic, or in your template, and you can fix it that much faster.

2: Building your own template engine from scratch is a tough deal, especially if you want it to be flexible and easy to implement. With that said, I developed my own for the sheer sake of seeing if I could do it. It's "easy" to do the basics, but you start realizing that as a coder, you want more than just the basics. You want extra features that would be cool to have, and you get suckered into developing these features. The time spent making your own could be easily replaced with time spent on learning a premade one (such as Smarty), which has a wide range of features and capabilities

3: You feel like this because you're a programmer. We like to do as much on our own as possible because you can go to other programmers and say "look what I made". It's pretty inherent to programmers in general. Should you feel like this? Absolutely. In my opinion, it shows that you pride yourself in learning, and then proving your knowledge by creating the wheel from scratch, but with your own flair. It shows the unique creative aspect of each person. Have I felt like this? Yes.

4: Unavoidable? Yes. Affecting your code/view? Depends on what the exploit and update are. If it changes some core key methods, it might affect your side, it might not. It depends on the engine you're using and the exploit/update released.

5: If you want to make your own, I suggest planning out what features (replacements, logic, etc) it will have, and stick to that until it's complete. Don't get halfway done and then realize you want a new replacement to handle custom snippets of PHP or whatever, it'll make the overall development a lot longer.

PHP Frameworks
1: Yes I use one. Yes I wrote my own. I wrote my own, again, because I wanted to prove to myself that I have the knowledge to make one that works to my specifications and requirements.

2: Using a framework IS learning new PHP functions, in essence. A framework doesn't replace every single aspect of your application, it merely makes tasks easier to accomplish without having to code everything, and it makes it so aspects are reusable instead of having to copy/paste portions of code in every page (think DB connections, queries, etc). You still have procedural "glue" that meshes your application to the framework itself.

3: Absolutely yes. The overall important purpose of a framework is to save time (whether it be programming time, or execution time).

4: There is no standard framework, because each framework is designed to do different things. They're like cars, there is no standard sports car (manual maybe ;)), but there are different flavors of them made to appeal to different people, each with different features, and similar features. Their purpose is the same, but how they do it and how they look are different.

The procedural style of coding will never dissipate completely, but it will definitely dwindle to a minimum. OO is always favorable because of reusability, readability, efficiency and the ability to add new features without interrupting or having to change other aspects of the application. Pick up a book on Design Patterns and you'll see exactly how easy they can make your life when it comes to new projects.

I picked up a book called Head First - Design Patterns and so far it's made a world of difference in my perception of OO programming. The one thing I like about the book is it's very visual, it's not just paragraph upon paragraph explaining things, and it uses real world examples that are easy to visualize (I'm a visual learner, can you tell? :)). It details the prevalent patterns (observer, factory, singleton, etc) and has a large portion (towards the end) explaining MVC and how it's not really a pattern in and of itself, but more of a collection of other design patterns put into one larger piece.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Templating: I use it frequently for anything bigger that a one page controlled display. I don't feel bad about using them any more than I don't feel bad about using PHP, Apache or MySQL.

Frameworks: I don't use them... yet. I am developing my own though because I have experienced many of the things you are describing and frankly I am tired of modifying code to suit the needs of this app. Generalized codebases can often times lead to faster development because each module/class/object does one thing and does it well. Your app just needs to hook into that.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I roll my own. That's just how I roll.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I use TemplateLite wherever possible. It's almost identical to Smarty except that it's faster. Like (I hope) most everyone, I started off coding in spaghetti code. Going back to look through my old code makes me cringe at the interwoven layers I see. Rigourous templating has really cleaned up my code & like everyone else is saying, helps to debug & modularize the code.

I don't use a framework per se. I have abstraction classes for a bunch of stuff, but I've never used a framework. I've always wondered about page load times & script efficiency if I'm using 3 or 4 levels of abstraction to do one simple task.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

In PHP I've written my own framework code for most things, except these days I use ADODB Lite because it's better than anything I could write in a sensible timeframe.

In Perl and JavaScript the opposite is true. I use CPAN modules all the time in Perl, and a variety of different JavaScript frameworks for clientside stuff.

I guess I'm a bit weird or something.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I use templates to a degree now, but I'm leaning more towards an "active view layer" than a "template", per se. I try to keep all my decision making and data manipulation in the controller, but I don't mind retrieving data and modifying it for output inside the template. Makes sense to me! Never used smarty et al, but I'm sure they're fine engines.

I've written one framework so far, and am working on it's replacement now. Never felt entirely comfortable with the first, but it sure did save me oodles of time! I can see how this next one will be even more of a time saver.

In javascript I totally use a framework (jQuery, lol) and it has been such an amazingly positive experience I'm basing my data model on it. I'll admit I'm a bit of a jQuery fanboi, but I really believe it's earned my advocacy. NSG will back me up on this one ;-)

I get the impression that the "F word" (framework) is almost a right of passage for web programmers - it seems to be a phase we all go through when we write our own. Oddly inefficient, but very rewarding. I recommend you try and write your own, if only to use it once or twice and ditch it... the experience is quite educational, and, dare I say: fun!
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Templating

I use templating all the time. In MVC it's pretty much the entire "V" (View) in a nutshell. It doesn't however end there. One important principle is DRY (Don't Repeat Yourself). If you are using similar presentation logic anywhere (whether it be a pure PHP solution, or a tagsoup solution like Smarty) once you are duplicating snippets you should isolate these in a "View Helper". Think of the work needed to build a Tag Cloud (recent example of mine ;)). Rather then fiddle with the logic directly in the template, I extracted it to a reusable helper. Im Smarty this carries an important task - knowing how to introduce new tags!

Personally using pure PHP, or Smarty, or any other templating library is a personal preference. Good practice in designing, managing and securing Views doesn't change regardless of which you use and each solution is equally capable with a little coaxing. I actually find it horribly difficult to code a website without tinkering with the core templating engine a little (mostly adding helpers, introducing Layout support, and esp. adding new tags to Smarty/Template Lite).
scottayy wrote:3. The idea of using a pre-existing program or code (e.g. smarty) to build a new web site or application, makes me feel like I am not the soul producer of the web site.. something I pride myself on. Why would I feel like this? Should I feel like this? Have you felt like this?
You're not a producer, you're a programmer. As a programmer your time is valuable and every layer of automation, regardless of who wrote it, decreases the time you need to develop stuff. The less time you need for developing, the more you'll get done in a day. More work = more money ;). My logic is impeccable!
scottayy wrote:5. Any other thoughts on templates or seperating presentation from coding?
It's an old argument I had (and continue to have ;)) across IM. In MVC, you three separated strands. I won't call them layers since the common definition of "layer" puts V and C in the "Presentation Layer". So three strands. A lot of misguided folk try to enforce a rule that only the Controller talks to the Model. They're wrong. It's equally valid for the View to talk (but only in a read-only fashion!) to the Model. In my Tag Cloud (recent thing) I have a View Helper which generates the cloud by reading all tags and their frequency directly from the database (really important that it only reads - absolutely NO writing to DB). Smarty can do the same thing really - I have a blog post all about Smarty custom tags and View Helper coming up soon since I get a lot of questions about it.

PHP Frameworks

I have written a framework called Partholan. At least part of it was open sourced. I valued it primarily as a learning experience since my PHP knowledge at the time badly needed an injection of personal experience to up my game. Was it any good? It worked, but it has some significant flaws since I only needed it for small uncomplicated sites. It was one hell of a ride while it lasted :). I doubt I learned so much about PHP in such a short amount of time before.
scottayy wrote:2. It seems to me that using a framework would be a bit like learning new php functions. Classes and wrappers, all designed to do what I could do procedurally (albeit, slower) with new function names to learn, new files to include, and a bunch of headaches until I grasp the framework wholey. Even so, would I still feel like I were using PHP, instead of some "language" that's more like an API to PHP?
A lot of people complain about Framework forcing a new language. Personally I think it's total bull. As a PHP developer we're expected to review and learn the codebases of dozens of applications as we encounter them. Have you ever tweaked Wordpress? Added a new plugin? Wrote a tack-on for phpBB? Dug around in a large app/lib finding bugs? These all require you to spend a significant amount of time learning a new API/Interface specific to that app. A framework is about (depending on complexity) the same cost initially. The benefit is that in reusing the same framework continually you're exposed to it over the long term learning it tricks, hacks, and places where you can tack on extra features. If you use it more than once it's already paying for itself.

I use the Zend Framework predominantly. Symfony also. I don't use phpCake.

The ZF does have a flaw in that it does not bundle as many upfront features as many other maturing frameworks. However it's PHP5, relies entirely on OO (no procedural functions), and has some pretty cool components beyond the much hyped Controller/View stuff (check out the web services, PDF library and it's excellent Internationalisation support via Zend_Locale, Zend_Date and Zend_Translate). It does require a bit more manual involvement in stringing an application together - which I really like since I can subclass in new features dynamically to attain framework heaven in my IDE ;). The problem with a framework doing everything for you is that you're not forced to make decisions on which components to use, abuse, or replace. The RAILs community is already facing a backlash from it's convention use - there's been a few publicised remarks about RAILs recently and the difficulty developers are having in scaling it.

Symfony is just cool. I can kick up a website very quickly and it's a pleasure to work with. My main concerns would be it's heavy handed ORM use (Propel is not for the faint of heart given it's performance cost though the next version may change the ORM backend). I can live with the procedural functions since they just work with me. If my time is really limited I'd use Symfony over the ZF - the ZF is more flexible but a bit slower to work with since it's missing several basic features like ORM, Input Validation Chaining, nested Views (or even butt simple Layouts - blogged one simple solution for a subclass approach yesterday). The ZF developer's fascination with dispatch plugins is like an arcane art to new user - It's a neat approach but it's a barrier to entry if simple stuff is not added concretely.

Where do frameworks suck? They do tie you into a specific way of doing things. Sometimes they are just not suitable for what you're really after. In some cases they completely work against you. I guess education is forewarned ;). Get a framework and learn it - see if it fits, or can fit, into what development you do.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

Once you start using a templating engine you never go back. I recommend phptal but you can use whatever.

Everything else I would have to say has already been said. Good luck
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

That's all very interesting. Most of you advocate the usage of a templating engine -- but what I hear about most on these forums is php frameworks. Is templating a part of most frameworks? Perhaps so, but I didn't know templating was such a popular practice.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

A framework without templating is a framework about to die a swift painful death ;). It practically rules the entire V in MVC.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Ah, good thing I didn't get flamed for that question. :lol:

So, let's say I choose to learn a templating engine still using procedural coding to produce variables and pass them to the templates.

Then, later on, I decide to try out a php framework. Will I be forced to use their "view" instead of the one I have already learned?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

not if the framework is flexible. some frameworks try and strong-arm you into using only their components, but that's why you don't use those frameworks unless you like that. Zend framework's components are pretty interchangeable. I tend to use their Zend_View classes for my (simple) templating needs. It doesn't have to use any additional template code like smarty (and friends) does to be a templating system:

Code: Select all

<html>

  <head>

    <title>I am a Zend Framework template!</title>

  </head>

  <body>

    <h1><?php echo $this->escape($this->welcome_message);</h1>

    <h2>Favorite places:</h2>

    <p>

      <?php foreach ($this->favorites as $favorite): ?>

        <a href="<?php echo $favorite->href ?>"><?php echo $this->escape($favorite->name); ?></a><br>

      <?php endforeach; ?>

    </p>

  </body>

</html>
Post Reply