?
Solved

I have just discovered the Smarty template engine - what do ppl reckon to it?

Posted on 2010-01-09
13
Medium Priority
?
480 Views
Last Modified: 2012-05-08
I want to create a commercial website.

There is a script available which does a lot of the donkey work, in PHP. I havn't got the latest version yet, but we do have an very old version (6years?) which was used as the basis for another website we inherited, which is in the same application domain as the one i want to create.

I've hacked away at the existing site as I've been learning HTML, CSS, and PHP, SEO. I've been fixing bugs, adding functionality, and making it SE friendly. Certainly using this site as the basis for the new site is an option, however I've come to realise that the age of the script, and the fixed design of the site, make this a non-favoured option.

If I was going down the PHP route, I'd prefer to get the latest script, and start from scratch in terms of presentation, by hanging a new UI onto it.

A while back I started learning ASP.net, and was encouraged by the apparent seperation of presentation and function offered by the page-behind model. While I'm good at C#, I have to admit that I'm a bit intimidated by ASP.net. Also I'm not aware of any scripts (unlike in the PHP world) that would do a lot of the donkey work, so a lot of the business logic MIGHT (if we don't find any scripts) have to be developed from scratch. There is then the concern that ViewState bloats the HTML output, and could affect SE rankings.

Also there is the fact that I've developed a fair working ability in PHP since I last looked at ASP.net, and would be more confident in PHP. So back to the new PHP script, and hanging a new UI off of it, which is already what I was leaning towards.

Then along comes Smarty... which I've only really started reading about on Friday.

It seems like it does a good job of seperating presentation layer from application logic. However, I don't think the latest version of the PHP script I'm keen on uses smarty at all (the old one certainly didn't and I haven't seen anything in the release notes to indicate the new one does), so the first step would be to seperate all the presentation code in the new script out into templates, and make small alterations to the application code to load the templates and assign the data into them.

Then we could sit down and look at the "distilled" presentation code and analyse it against SEO, design, best practice etc. and it could be amended at will.

Is this going to be a job fraught with issues? Anyone gone down this route before?

Maybe its easier in the long run to leave Smarty out and just hang the UI on the script as is (well not exactly as is as I'll be making business rule amendments to the script in places)

Or is it better to give up on PHP, bite the bullet and go down the ASP.net route.

ASP.net promises the ultimate in flexibility, but probably at the cost of me needing to be fairly expert, as I think I concluded on last evaluation that to get the control over the UI I might want, I'd probably need to extend the controls by derviing new classes from them.

At this stage there is only me on the project, so the seperation into presentation code and application logic will not create an advantage in terms of concurrent progress on both strands that u would have if you had your designer and programmer able to work on each element at the same time (as promised by PHP/smarty and ASP.net done properly - i.e. customising the controls). However design is not my forte, and I might want to outsource this bit towards the end, to get that "agency" look.

OK, a lot above!

Apart from the headline question, I'm also looking for views on ASP.net vs PHP/Smarty.

thanks in advance
0
Comment
Question by:zorba111
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
  • +1
13 Comments
 
LVL 20

Assisted Solution

by:virmaior
virmaior earned 600 total points
ID: 26277960
There are a number of other presentation frameworks like smarty.   The big benefit is the theoretical separation between presentation and business logic.  At least for me, I find it a pain to write some sections of code this way.

I personally find the ASP model quite bothersome, but I'm mostly a PHP developer.  I don't think there's that big of a hit to SEO from using the ASP model.  Plenty of super-popular sites are run on ASP.

What would be need to have a better idea of what you should do is a better idea of what sort of application you're building.  "Commercial website" remains too vague to know whether the best tool is a jack-hammer, a swiss army knife, or a machette.
0
 
LVL 51

Accepted Solution

by:
Steve Bink earned 1000 total points
ID: 26278782
I have worked with Smarty a couple times, and have no appreciation for what it brings to the table.  A "normal" developer would know PHP and HTML, and separate them in their code.  Smarty is designed for PHP developers working with HTML developers, and assumes neither knows or has interest in the other's job.  To me, however, this is just smoke and mirrors.  Instead of an HTML developer learning a little PHP or a PHP developer learning a little HTML, both have to learn a third-party substitute to act as an intermediary between them.  It puts yet another layer of abstraction in a place that does not need abstraction.  

What is the difference if I tell an HTML developer to use a Smarty {section} loop instead of a PHP foreach loop?  

1. The HTML developer will need to learn Smarty instead of PHP.  This knowledge is only portable to other sites that use Smarty.  Moving to another position will likely have the HTML developer learning PHP anyways.
2. Now the site has to run through the Smarty loop identification logic in addition to the PHP.  The site is slower as a result.

I've been writing my own CMS for awhile now, and I've come up against the same problems Smarty was intended to address.  Realistically, the only solution is that each side of the equation needs some knowledge from the other.  Adding more knowledge requirements on top of that makes no sense...it is not a simplification.  The PHP developer should leave a minimum of *documented* variables for the UI programmer.  Ideally, classes should follow MVC construction and return standardized, structured HTML and let CSS do the heavy lifting of presentation as it is meant to.

As a brief, shameless plug, my CMS (built with PHP) was designed in this manner.  If you are interested in seeing what it has to offer, you can contact me via the email in my profile.  It is still under development, though it can (and currently does) handle commercial sites easily.  We are in the process of rebuilding parts of the engine, and I look forward to stripping it down yet again for PHP 5.3 (finally!  late binding!  w0oT!) once the Suhosin patch is integrated.

As far as ASP.net goes, I am not really in a position to give much opinion.  I've worked with it only as a side-effect of my day job, but I can say I do not like how it mangles the end output.  I like to be able to see quickly and clearly what an application is doing, and ASP.net removes both of those aspects.  While it is a fine framework, and they did a wonderful job with it, it is not something I would use personally for any development in which I had a choice.  Because most SEO is worried about content rather than structure, I do not believe ASP.net presents much of a disadvantage there.  
0
 
LVL 19

Assisted Solution

by:v2Media
v2Media earned 400 total points
ID: 26280213
"Maybe its easier in the long run to leave Smarty out", this I agree with 100%. The whole purpose of Smarty is undermined by it's template syntax, which is every bit as complex as PHP. Another issue will arise if the webpages incorporate JSON; Smarty will conflict as it tries to parse JSON as php.

I agree with routinet in that the end user's interaction with the presentation layer should be limited to CSS. If the HTML doesn't suit a client's needs, it's the job of the php developer to tailor html to suit. Then it's the designer's job to style with gfx and css.

As far as php developers go, I believe there's 3 camps: 1) Programmers, with limited skills in html UI. 2) Developers, Model literate but specialising in VC development and proficient in html/css 3) Hacks, generalist skills - master of none. Programmers design and build the application for Developers, Developers integrate views and business logic and liase with the designers to meet UI requirements.

Insofar as ASP.NET vs PHP is concerned, I am biased. When I was in your shoes, about 8 years ago, I tried ASP and hated it. The community was small and intolerant of newbies. So I picked up PHP, fount it extremely easy to learn and backed up by a huge community for support.

About 3 years ago I tried my hand with ASP.NET using Visual Studio. I spent about 2 months trying to get VS working properly and both Microsoft and VS communities that I could find were not helpful. So long ASP.NET! If you need or want strong community support, forget ASP.NET.

0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:zorba111
ID: 26408154
I got the latest version of our script but haven't had a chance to tinker with it yet, apart from put it up and attach it to a domain.

The PHP is quite dense, and I would be using Smarty to go and seperate out the HTML from the application. Its not because I don't know HTML/CSS (or PHP), I know both, or at least I accept that I will be developing in both and will have to plug any knowledge gaps.

What Smarty promises is a division between the code and the UI, that will help me understand how the 1k+ files coordinate together.

I'd imagine I wouldn't templatise all 1000 files, just the top 10% say, the 10% that are used most often.

I'm wondering if the bother is going to be worth it, but then even doing the work of templatising will probably help me get familiar with the app and its functionality as I'll be doing a mini "code-walk-thru'" of each page as I templatize it, and then for future reference I'll have a templatised version that will be easier to understand.

Does that seem reasonable?
0
 
LVL 51

Assisted Solution

by:Steve Bink
Steve Bink earned 1000 total points
ID: 26414315
>>> I'd imagine I wouldn't templatise all 1000 files, just the top 10% say, the 10% that are used most often.

From my view, I equate that to "I'll take the most-used 10% of files, and put them on a platform that makes everything more difficult."

I am never against learning for the sake of learning.  I learned Smarty partially because I took on a job which already had it implemented, and partially because I wanted to know if had anything worthwhile.  I found that it did not.  I finished the job, and recommended to the owner that he migrate away from Smarty whenever it was feasible for him.  Once you have invested time and energy into Smarty, though, you are locked in.  Your site's content does not easily migrate away from it.

Again, kudos for wanting to separate code and UI.  That is how things should be.  But why inject a third-party app into what can be done easily and naturally with the tools you are already using?  What is there between PHP and HTML that prevents you from doing the same thing?  Yes, you will have a small amount of PHP in your HTML in order to reference variables.  Yes, you can have a bit of HTML in your PHP as you pre-build portions of your response.  What is the end result of using Smarty instead?  You have PHP mixed with Smarty conventions, and HTML mixed with Smarty conventions.  That is a complication, not a simplification.  More to the point, it is a complication with zero additional benefit.

By all means, learn Smarty, but do it on a test platform that is expendable.
0
 

Author Comment

by:zorba111
ID: 26445901
@ virmaior

>> There are a number of other presentation frameworks like smarty.   The big benefit is the theoretical separation between presentation and business logic.

..and what that creates is that when one is modifying the UI, all the UI code is in one place, and easier to understand, and hence get the modifications/experimentations right

>> I personally find the ASP model quite bothersome, but I'm mostly a PHP developer.  I don't think there's that big of a hit to SEO from using the ASP model.  Plenty of super-popular sites are run on ASP.

Do you mean classic ASP here, or ASP.net. I'm assuming you mean classic. Classic is not an option.

>> What would be need to have a better idea of what you should do is a better idea of what sort of application you're building.  "Commercial website" remains too vague to know whether the best tool is a jack-hammer, a swiss army knife, or a machette.

Its an online jobs database
0
 

Author Comment

by:zorba111
ID: 26445989
@routinet

>> Again, kudos for wanting to separate code and UI.  That is how things should be.  But why inject a third-party app into what can be done easily and naturally with the tools you are already using?  What is there between PHP and HTML that prevents you from doing the same thing?  Yes, you will have a small amount of PHP in your HTML in order to reference variables.  Yes, you can have a bit of HTML in your PHP as you pre-build portions of your response.  What is the end result of using Smarty instead?  You have PHP mixed with Smarty conventions, and HTML mixed with Smarty conventions.  That is a complication, not a simplification.  More to the point, it is a complication with zero additional benefit.

I can see that this is where you were leading in your first post! Sorry for being a bit thick, and needing you to repeat & elaborate. Yes, I can see now that I could do the disentangling myself, and seperate the code out to mainly HTML (with a tiny amount of PHP say just  <%php echo $string1; %> in place of the smarty variables, sections etc.) and mainly PHP.

If I do this well, and name the files well,
e.g.

banner_ui.php,
banner_code.php,
(all the HTML for this page in the one file)

about_whole_ui.php,
about_middle_ui.php
about_bottom_ui.php
about_code.php
(all the HTML for this page gets built up in  the container, of its constituent parts
- however only one "code-behind" page needed !!)

Name any "global variables" (i.e. those that are set in the *_code.php pages and accessed in the *_ui.php pages, the equivalent of the Smarty variables) in a special way, e.g. $G_login_name.

Mind you, OO PHP probably has a way to handle this last one, but I need to read up on OO PHP as I haven't used it ever (no probs with OO, just syntax and usage wrt PHP is fresh for me)

If I do all of the above, them effectively I'm doing what Smarty does. I have to weigh up the pros and cons. ON the flipside, Smarty does not seem hard to learn, another weekend at it should see me right.Its just a little bit terser and I can see that making a difference when trying to understand UI elements, if the any presentation logic, such as there is, is in Smarty.
0
 
LVL 20

Assisted Solution

by:virmaior
virmaior earned 600 total points
ID: 26446022
i would recommend using OO php.

if you wanted to you could still have it load separate parts.

also with the OO approach you can extend the class to overwrite unwanted sections or have layers of extensions.

on one project, i simply made a class that represents the page

class web_page
{
  private $js, $css;
 

   function __construct($js = false, $css = false)
  {
  $this->js = $js;
  $this->css = $css;
  }

  function show_top($title)
 {
 ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title><?php echo $title; ?></title>
<?php
  if (sizeof($this->js) > 0)  {
    foreach ($js as $js_file) {
  echo '<script type="text/javascript" src="' . $js_file . '" ></script>';
    }
  }
//  ... same th for css
 }
 
function show_body()
{
}
  ...
}
0
 

Author Comment

by:zorba111
ID: 26446109
hmmm.... interesting.. make everything an object

have a generic page, then derive from that, to the increasingly specific types of pages..

have classes for all the bits and bobs of a page... overload functions to handle different situations

NOW i'm starting to understand how OO can work for PHP !!
0
 

Author Comment

by:zorba111
ID: 26446117
for a 1000 file script, seperating the UI and code is one thing

making it fully OO is probably a 2yr stint :-)
0
 
LVL 51

Assisted Solution

by:Steve Bink
Steve Bink earned 1000 total points
ID: 26447252
I'm glad to hear you understand what I was trying to convey.  You can really test the difference by creating a page using Smarty, then creating the same page using abstraction techniques with just PHP and HTML.  You will find that a) the Smarty pages run slower because of how much overhead must be loaded, and b) creating the page with standard PHP is *much* easier than creating with Smarty.

Using OO significantly increases design time, true, but it yields massive benefits when working with future expansions.  Taking virmaior's example to its logical conclusion, your web_page class will have two properties - web_head and web_body - which are classes themselves.  The web_head class will have properties for title (simple string), meta (array of web_meta objects), scripts (array of web_script objects), and styles (array of web_style objects).  As you drill down, each class contains the methods necessary to render that particular item.  So to actually render a page, you might see something like the excerpt shown below.  Each object presents an API, of sorts, to the application as a whole.  

The CMS I mentioned in my earlier post is already using both of these strategies (separate files for code/html, and OO page model) and it is working like a charm.  Adding a new feature is as simple as either expanding a single class, or adding a new class as a property of an existing one.  The entire thing is plug-and-play.  And my UI guy (I'm no artist, and dislike users as a general rule) only had to learn how <?= and echo() work to create his side of the equation.

Now to teach him proper HTML.  ;)

<?
// main application level
echo $myCMSObject->Render();

// the class hierarchy
class web_page {
  public function Render() {
    $ret=$this->head->Render() . $this->body->Render();
    return $ret;
  }
}

class web_head {
  public function Render() {
    $ret="<head><title>{$this->title}</title>" . $this->RenderMetas() . $this->RenderScripts() . $this->RenderStyles() . "</head>";
    return $ret;
  }
  public function RenderScripts() {
    $ret='';
    foreach ($this->scripts as $val) { $ret.=$val->Render(); }
    return $ret;
  }
  // and so on...
}

class web_script {
  public function Render() {
    $ret="<script src=\"{$this->src}\" type=\"{$this->type}\"></script>";
    return $ret;
  }
}
?>

Open in new window

0
 

Author Comment

by:zorba111
ID: 26571013
stuck in ASP mode now, but will get back
0
 

Author Comment

by:zorba111
ID: 26829601
ok guys, not fair to keep u hanging on any longer -

some great answers and great thought provoking info here, so I'll close this off now
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question