1

I have some html code with some php variables in it which describes the content of a single entry in my search results(e.g. picture, name, number of mutual friends etc) - this is referred to as search_results_v.php in my code below. I have an array which contains several records from a database, and for each one of these I make a search result entry. The way I have structured my program is an MVC, so the html code is the view and I have a php controller.

However I have a template for my website and would like to include my search results in this template. This could be solved easily by calling my controller in the appropriate part of this webpage, but I want to make my application so that the controller controls everything! In the case I just mentioned the view was calling the controller, but I want to have a setup like I have below, where I simply "include" the webpage in my controller.

How can I render my search results in the webpage while minimising the amount of php logic in it? One way I was considering doing this was to store all of the html code for all of the search results in a php string variable, and then just make a reference to this variable in my webpage. How could store the html code in that variable?

// Generates my search result box - Fill in the profile picture, the name and the number of mutual friends

    foreach ($resultsInfo as $entry) {
        $uid      = $entry['uid'];
        $pic_link = $entry['pic_square'];
        $name     = $entry['name'];
        $mfCount  = $entry['mutual_friend_count'];
        include('searchresults_v.php');
    }

EDIT: Ok, maybe what I was going for isn't an MVC exactly. I was trying to separate out the actual backend coding from the interface as much as I could so that someone far more adept could come in and design an interface. I basically wanted to generate all the dynamic data and just have some placeholders in my html files so that the content could just slip in. I didn't want there to be any logic in there to confuse someone who may only have experience with web design.

Let me know what I should change my title to.

user1058210
  • 1,639
  • 7
  • 29
  • 49
  • In MVC, HTML code is *not* the view, and the controller does *not* control everything – orourkek Aug 17 '12 at 19:23
  • @orourkek why do you say that? CodeIgniter suggests you put your HTML code in the view, where else would you put it? http://codeigniter.com/user_guide/general/views.html – Doug Molineux Aug 17 '12 at 19:25
  • 2
    @PeteHerbertPenito Because CodeIgniter is not an MVC framework? A proper MVC view is **not** a template. A template can't make requests to the model or manage templates, like a view is actually supposed to do... Also he never mentioned CodeIgniter, so I don't know why you would even bring it up – orourkek Aug 17 '12 at 19:28
  • @orourkek lol sounds like you need to read a bit: http://phpmaster.com/untangling-mvc-with-codeigniter/ – Doug Molineux Aug 17 '12 at 19:30
  • 1
    @PeteHerbertPenito I seriously hope you're trolling. If not, you should read up on what [MVC](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) actually is - start [here](http://stackoverflow.com/questions/5863870/how-should-a-model-be-structured-in-mvc/5864000#5864000). CodeIgniter is definitively one of the worst frameworks in terms of **not** conforming to the MVC pattern (plus tons of SOLID violations...) – orourkek Aug 17 '12 at 19:33
  • @orourkek Sorry if what I am doing is not an MVC - I have tried to clarify what I am doing in my edit :D – user1058210 Aug 17 '12 at 19:48

5 Answers5

4

In an MVC, the view can always contain looping and conditional statements. The basic idea is to NOT have queries/business logic implemented in the view, which should be done by Models

In your case, what you should architect, IMHO, is something like this:

  1. Controller invokes a model, that generates all the required info and formats it as an array, json string, or a class object and passes it back to the controller.

  2. Controllers hooks up this data into a view. The view loops through the array/json/object and displays content in whatever format the page needs to be shown

raidenace
  • 12,789
  • 1
  • 32
  • 35
  • 1
    That is your interpretation my friend. I am not a purist. Also, I was telling the OP what in my opinion he should create. NOT what a controller does. There are as many interpretations of the controller as users of MVC. And there is no like "standard" on what a controller can or should do. There are only good practices and bad practices. Also, saying that is not what the controller does, and then not saying what, according to you it really does, doesn't clarify anything. – raidenace Aug 18 '12 at 13:15
0

Can you include your view inside of your template? You could pass the filename of your view to your template and have it include it in the middle of your template.

Jordan
  • 183
  • 1
  • 10
  • Thanks for the reply - yeah I wanted to do that, but the search result view only contains code for a single entry, and so to generate all of the results in there I would need a loop in there - this is something that I had hoped to avoid (see edit)! – user1058210 Aug 17 '12 at 19:46
  • You could put that code inside of a view too... So you could have a view that would call another view. In the end you would have your controller including a template or layout, that would include a view, and then that view would do your loop that would include your smaller view. – Jordan Aug 17 '12 at 19:56
0

If you'd like to move forward without a framework like CodeIgniter, CakePHP, Symfony, etc., you can check out Smarty. It's super easy to use, and makes templates very readable. You can minimize logic, but it gives you enough leg-room to put logic in there if you need it.

http://www.smarty.net/

http://www.smarty.net/syntax_comparison

With smarty, you can treat the file that generates the template as your controller/view. Do all of your logic, gather all your data, etc., then pass it to your smarty template.

Matthew Blancarte
  • 8,251
  • 2
  • 25
  • 34
0

Hy

You can create a HTML-Page without any Logic. For a Table with users you create only one example row of a user. See example below:

<table>
     <tr>
        <td>Firstname</td>
        <td>Lastname</td>
        <td>Age</td>
     </tr>
     <tr class='user'>
        <td class='firstname'></td>
        <td class='lastname'><td>
        <td class='age'><td>
     </tr>
</table>

After that you parse the HTML-File with PHP (Lib ex. Simple HTML Dom Parser) into Objects.

At this point you can generate the View with the PHP, like cloning, deleting, renaming, filling elements in the view with selecting the elements like jQuery does "find(.firstname)". After you edit the view you generate the HTML from the Objects.

I prefer to this methode because the Webdesigner has only to create the View with HTML and CSS and should not learn how does a template works(ex. PHPTal)

Hope this help

Kevin
  • 489
  • 2
  • 5
  • 15
-2

I would use jquery(or any other Js framework) get my data in JSON and add all client side effects that php can't give

Femi Oni
  • 796
  • 2
  • 9
  • 25