Jump to content
  • Advertisement
Sign in to follow this  
future_man

View in PHP? Template in PHP?

This topic is 1829 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a question, no a rant even because there is absolutely no information about this topic. Sure I read a lot about templating engines and MVC frameworks already out there. But what if I am building a small php app and frameworks would be an overkill and template engines are just redundand for me... I want to make a nice organized app but so far my directory strucute is: models (backend, bussiness logic) and html, php mix (for displaying php results to the user).

 

Regarding views, what the hell is is? Is this just a mixed php file with html to display output? What is a template? What is the difference? Is a template purely in html? How does a template structure even look like? I really want to separate php from html but so far I have a bunch of <?php tags inside html files... and it really looks nasty. Lets say I want to make a template or a proper view, or only one of the two, whatever is needed...

 

Simply put: I want php in one place and html, css design in other... And in a really simple way, with nothing else but my own code. Templates, views?

Edited by future_man

Share this post


Link to post
Share on other sites
Advertisement

It's all just a matter of terminology; all the frameworks effectively do the same thing. They all achieve pretty much the same result: you imagine some kind of "clean separation" between a controller and a view, but in reality it's a lot more blurred, you'll definitely end up with presentation logic in your controller and possibly business logic in your view.

 

YES - you can use PHP as its own templating language. Create a separate directory, include the files at the bottom of your script. Maybe put variables into a dictionary for its use.

 

YES - you can use a templating engine such as Smarty etc,

 

YES - you can do whatever you want.

 

---

 

When I wrote PHP applications regularly of my own design (a long time ago), I normally put the "controller logic" and "view logic" in the same file, which would have a lot of variable declarations, function calls, form processing, etc, at the top. Then a comment, then the output

[source]

 

 

<?php

 require 'include/allmystuff.php';

 $db->dosomethingfunky();

 # Process form if necessary...

 # Populate some variables for use below

 $list_of_items = find_relevant_items(); ...

 

 # ----- HTML STARTS HERE #

 require 'html/header.php';

?>

<h1> <?php echo htmlspecialchars($myfunkyheading); ?> </h1>

etc...

 

 

[/source]

Share this post


Link to post
Share on other sites

Aha, so you are saying that the code you posted is called a view and above you included your bussiness/backend code  (require 'include/allmystuff.php';) however below you included your template (require 'html/header.php';)

Share this post


Link to post
Share on other sites

Aha, so you are saying that the code you posted is called a view and above you included your bussiness/backend code  (require 'include/allmystuff.php';) however below you included your template (require 'html/header.php';)

No, I'm not saying that.

 

In the above example, allmystuff.php was a *common* set of libraries etc, that we might use on multiple pages.

 

Likewise, html/header.php was a common part of the view, which might be used on all pages.

 

The actual page-specific parts of the "controller" and "view" would still be contained in one file. I'm not advocating repeating yourself.

 

I only talked about controllers and views; I had hoped that I *implied* that this meant keeping the models separately. However, I'll explain it - persistence logic etc, could be mostly kept in other modules not directly in a view/ controller.

 

What I try to avoid is this:

 

... some presentation logic (echo $page_title etc)

... some back-end logic

... some more presentation logic (for $row in $results ...)

... More back-end logic

 

Because it turns into spaghetti.

Share this post


Link to post
Share on other sites

But overall a template is just a mixture of html and php right? And a view is broader, its a set of variables, session variables or whatever and it includes templates at the bottom. Did I get this straight?

 

Is a view also used in a post-redirect-get pattern? The way I see it the view is a mediator between actual database input code in the back and it includes templates to present the output to the user

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!