Jump to content

View in PHP? Template in PHP?


Matic

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 Matic
Link to comment
Share on other sites

A view can be a template, but can also be a snippet of HTML where there is some or no PHP. The view is usually called with an include function, but after output buffering is started, and then output when desired by echoing the output buffer or just using ob_end_flush.

 

What all this does is allow you to set variables that are used in the view, but at the same time keep the view (which tends to be mostly HTML) out of the controller or model.

 

Views can be nested inside other views, and if you are doing this then the nested view is usually saved as a variable instead of being output. That variable is then inserted into the parent view, which is then output to the browser.

 

Some templating systems make use of special delimiters that designate a variable. For instance, instead of using php in a view to insert a variable named $something, some templating systems would allow you to write {{ something }}. I like my framework to be as simple as possible, so I don't use a templating system.

 

Hope this helps.

Link to comment
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?

 

PS 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

Edited by Matic
Link to comment
Share on other sites

In MVC your Model would be DB and/or business logic stuff, the Controller would get data from the Model, load the View, pass vars to the view etc.  For the most part the view is just a template, a mix of HTML and only PHP needed to display data.  PHP is a template language.

Link to comment
Share on other sites

There's only one difference between a template and a view, and that would be that a template is usually the parent of some nested views. A template is just another view though.

 

It sounds like you need a clear understanding of MVC, and devs have opinions on how MVC is to implemented, so if I describe MVC to you then keep in mind that this is how I understand it.

 

MVC =

----------

Request is handed to a controller.

Controller decides what to do with request.

Controller asks for data from Model.

Model may process data, including data validation.

Model may set view variables, or hand data back to controller which injects them into views.

Views use data to build HTML

HTML is output to the browser.

Link to comment
Share on other sites

What would be this structure I use for my project?

<body>
        <?php if(!$_POST and $poskodbe != '0') { ?>
        <p>Presenetil<?php text($spol); ?> te je <?php text($monster_ime); ?> !</p>
	<form action='../igra/gozd.php' method='post'>
		<input type='submit' name='action' value='Napadi' /> ali 
		<input type='submit' name='action' value='Pobegni' />
                <input type='hidden' name='monster' value= '<?php text($monster_ime); ?>' />
	</form>

Would this be template or a view?

Link to comment
Share on other sites

A template and a view are the same thing. In MVC the View is the part of the application that displays the results of a user's action and the application's processing.

 

How you make up that View is entirely up to you. MVC is an idea and thus not bound to any technical implementation's, the same applies to views.

 

For example:

 

// the file is the controller (binds model and view)
if ($_POST) {
   // mail is part of our model
   mail('foo@bar.bat', $_POST['subject'], $_POST['message']);
   // thank-you.html is part of our views
   include 'thank-you.html';
   exit;
}

// contact.html is part of our views
include 'contact.html';

// all concerns have been separated
This is an MVC implementation. Edited by ignace
Link to comment
Share on other sites

A template and a view are the same thing. In MVC the View is the part of the application that displays the results of a user's action and the application's processing.

 

How you make up that View is entirely up to you.

 

Hmmm so where do I put my variables that display data to user etc.. in my template?

Link to comment
Share on other sites

A template and a view are the same thing. In MVC the View is the part of the application that displays the results of a user's action and the application's processing.

 

How you make up that View is entirely up to you. MVC is an idea and thus not bound to any technical implementation's, the same applies to views.

 

For example:

 

// the file is the controller (binds model and view)
if ($_POST) {
   // mail is part of our model
   mail('foo@bar.bat', $_POST['subject'], $_POST['message']);
   // thank-you.html is part of our views
   include 'thank-you.html';
   exit;
}

// contact.html is part of our views
include 'contact.html';

// all concerns have been separated
This is an MVC implementation.

 

 

I think I understand more clear now what is what! Can you confirm that this page I made is indeed a simple controller. My model redirected to this page and now on this page I handle all sort of variables and on the bottom I included the view/template:

session_start();

require_once 'includes/save.php';
require_once 'includes/stats.php';

$igralec_ime = $_SESSION['username'];


if (isset($_SESSION['monster_ime']) or
    isset($_SESSION['monster_curhp']) or
    isset($_SESSION['moznost']) or
    isset($_SESSION['poskodbe']) or
    isset($_SESSION['spol']) or  
    isset($_SESSION['combat']) or
    isset($_SESSION['turns']) or  
    isset($_SESSION['zmaga']) or
    isset($_SESSION['zguba']) or
    isset($_SESSION['cekini']) or
    isset($_SESSION['post']) )
 {
    $monster_ime = $_SESSION['monster_ime'];
    $monster['curhp'] = $_SESSION['monster_curhp']; 
    $moznost = $_SESSION['moznost'];
    $poskodbe = $_SESSION['poskodbe'];
    $spol = $_SESSION['spol'];
    $combat = $_SESSION['combat'];
    $turns = $_SESSION['turns'];
    $zmaga = $_SESSION['zmaga'];
    $zguba = $_SESSION['zguba'];
    $cekini = $_SESSION['cekini'];
    $_POST = $_SESSION['post'];
    if ($monster['curhp'] <=0 and isset($_SESSION['monster_curhp']))
    {
        update_save($igralec_ime, 'gozd', '');
    }  
    else if (prikazi_stat('curhp', $igralec_ime) != 0)
    {
        update_save($igralec_ime, 'gozd', $monster_ime);
    }
 }

 else
{
    $poskodbe = prikazi_stat('curhp', $igralec_ime);
    $moznost = 'Tvoje zdravje je resno ogroženo, vrni se domov!';
    $monster_ime = prikazi_borba($igralec_ime);
    $spol = sprintf("SELECT spol FROM monsters WHERE ime = '%s'",
            mysql_real_escape_string($monster_ime));
            $result = mysql_query($spol);
            list($spol) = mysql_fetch_row($result);

    if (prikazi_stat('curhp', $igralec_ime) <= 0)
    {
        update_save($igralec_ime, 'gozd', '');
    }
}

include '../public/gozd.html';
Link to comment
Share on other sites

Yes. It separates the 3 responsibilities. Though a controller should not execute queries as only your model should be capable to do this. Also your update_save function should receive it's data from the controller and not use $_POST or $_GET internally since only your controller knows about $_POST and $_GET.

 

The controller should be a firewall of some sort, between the client and your controller HTTP exists, but the model should be agnostic of HTTP.

 

           HTTP              CALLS
{ client }<---->{ controller}---->{ model }
The advantage here is that you can change HTTP to for example CLI or SOAP or whatever without having to change the underlying model.

 

Since $_POST, $_GET, $_COOKIE, $_SESSION are part of HTTP it's therefor that your model should be agnostic of these variables.

 

My model redirected to this page

A model should not issue redirects since redirect's are the responsibility of the controller.

Edited by ignace
Link to comment
Share on other sites

 Also your update_save function should receive it's data from the controller and not use $_POST or $_GET internally since only your controller knows about $_POST and $_GET.

 

A model should not issue redirects since redirect's are the responsibility of the controller.

 

 

1. How else am I supposed to tell the controller and the view to only show information if the user didn't or did click post button on my form?

 

2. Model issued a redirect because it is wise to implement post-redirect-get pattern, without it my user could just click refresh endlesly and the database would have duplicate entries... Is there any way around this?

Edited by Matic
Link to comment
Share on other sites

1. How else am I supposed to tell the controller and the view to only show information if the user didn't or did click post button on my form?

You understood me wrong in your controller you can use this however you want, but not inside your update_save function. You can pass these like so:

 

update_save($_POST['foo'], $_POST['bar']);
If you need a redirect you just add it:

 

update_save(..);
header('Location: ..');
exit;
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.