views:

44

answers:

7

I'm just trying to understand the Templating(system). If you use MVC in your web application then you don't need to use something like Smarty(template engine) as you are already separating application code from presentation code anyway by using MVC right? please correct me?

So am i correct in thinking it's MVC OR Templating or do you use both in your apps?If any one could explain this in detail it would be great.

Thank you in advance;-)

A: 

Templating is a tool you can use in the View part of MVC (Model-View-Controller).

  • Model represents your data
  • Controller "maps" user interface actions to operations on the data
  • View presents the data to the user (this is where a tempating tool can be useful)
Dawie Strauss
A: 

It's OK to use both. Templating only pertains to the views in your application.

Your data still comes from the model and controller, which pass values and such to the template engine, which includes your view files and renders them, passing your data along so it can be displayed in your views.

BoltClock
A: 

Well in my cases, the template system is my "view". Its completely dumb and talks to the "controller" which validates input and generates the output and such things.

XnS
A: 

You've got to keep in mind that MVC is just a pattern. It is, in my opinion, it is a way of thinking. It is all up to you if you use a template parser in your MVC, as long as you keep the logic and the view of your application seperate.

I prefer a parser that just parses PHP. It has a few advantages (no specific template language needed to learn, a bit faster because nothing actually need to be parsed).

Martijn Dwars
A: 

The MVC pattern focuses on the responsibility of data and flow control. Who can handle the data and the the flow path redirect to. No matter how to implement the view layer (using Smarty or not), your view sholudn't modify the data or control the work flow directly.

qrtt1
+1  A: 

MVC has three main components. Model - View and Controller. Views camunicate with controller and model, that's the way it has to be done, if you're view isn't talking with the other two components, then you have something called PAC (Presentation-abstraction-control).

That means that the view has more functionality that you may think, and that's way you can use several techniques to acomplish the generation of views.

Going straight to the answer, you may (and you should) use a template engine for your views. Here is a short example, you can see how it's handled by CakePHP (PHP MVC framework)

Supose you go to http://www.example.com/books/get/2

That is handled by the controller, who knows that the user is requesting for the action "get" (with parameter 2, that might be the id), of the model "Books". There, it has to look for the book with the id=2, and retrieve it.

The controller performs a database lookup, get the corresponding book, and populate a variable. Short code:

function get($id){
  $book = $this->Book->find($id);
  $this->set('bookVariable',$book);
}

The last line (%this->set) will assign a variable to the view. More precisely, the "get" view will have a variable called bookVariable with the contents of the book. Short code for the view:

<html>
...
<body>
<ul>
<?
  if($bookVariable){
    echo "<li>$bookVariable->name</li>"
    echo "<li>$bookVariable->price</li>"
  }
?>
</ul>

Then you can see how this two components interact, you can or cannot use a template system, it has nothing to do with MVC. Again, i recommend you to use it.

santiagobasulto
A: 

Smarty (much like other third-party templating engines) is one of the possible implementations of the View part of MVC. You don't have to use it, although you can if you want.

MVC separates data from presentation, but not completely. Unless you're producing completely static html you'll have to place some dynamically generated data in your template. The whole idea is that your controller calculates this data and places it in one or more PHP variables which are then used by your template.

Let's say you want to greet a user whose name you've fetched from somewhere (database, session, whatever). You controller will do its thing and finally pass the name of the user to your View:

$view->set('name', 'Bob');

$view is an object representing your View. Whether it's implemented using Smarty, vanilla PHP or some other templating system is irrelevant at this point. The syntax might vary, but the logic is the same.

At this point you want your template to use that variable. How it will do so depends on how your View works. With plain PHP you might have $view do the following internally:

$name='Bob';

and have your template fetch this like so:

Hello <?php echo $name ?>. Welcome back.

Or you could use Smarty in which case your template will look like this:

Hello {$name}. Welcome back.

The logic is the same, the implementation is what changes.

Finally if you are considering whether you should use Smarty in your View, I'd personally recommend against it as it does nothing that plain PHP can't do, which effectively makes it excess baggage. It's one more thing to worry about, not to mention you'll have to make sure it's installed it on every server that your app will need to run on.

Manos Dilaverakis