0

It's really convenient how MVC patterns allow you to define view a and then load variables into it via the controller. For the sake of argument let us take CodeIgniter as an example:

Controller:

Class Example extends CI_controller(){
          function show_page(){
               $data = array('msg'=>'Hello World');
               echo $this->load->view('hello',$data);
          }
}

View (hello.php):

<h1><?php echo $msg; ?></h1>

I have taken over an old project written years ago where there are redundant html code everywhere. It has no pattern whatsoever just straight up poorly structured code.

I wanted to create a class that has a function that will fetch all HTML code from a file in one folder, feed variables to them and show the result. Like so:

Folder structure:

View_folder
     - hello.php
Class
 - view_class.php`

Main:

<?php
 $data['msg'] = 'Hello World!';
 echo $view_class->get_view('hello.php',$data); 
?> 

Is it possible to achieve this? Can someone give an example function on how to do this. Thanks.

tereško
  • 58,060
  • 25
  • 98
  • 150
Elymentree
  • 823
  • 10
  • 22
  • 1
    It is, of course, but existing frameworks (like CI) have already tackled this and the numerous bugs and use cases. If you go down this road you will only be doing what the coders who worked on such frameworks already did. Is there a reason you can't use an existing framework? – Technoh Mar 24 '16 at 18:46
  • Understood. In fact I work everyday shaking my head when looking at how this system was put together. The 2.0 version will definitely implement a framework. But the existing system would at least still have a year or two of life expectancy before ver 2.0 is implemented. Plus there is this client mentality of 'if its not broken why fix it? just add new features to it.' – Elymentree Mar 24 '16 at 18:53
  • I can empathize! I have shaken my head a lot over the years. I still get the exact same requests from clients and I agree that it can be a pain in the ass to work with older or thrown together code. However if go down the road of creating a controller / view system by yourself I think you will find it is a slippery slope. – Technoh Mar 24 '16 at 18:56
  • This is very opiniated. But I usually just suggest using an existing framework like Symfony. If you create your own you are basically just creating yet another framework. Depending on your skills it may be better than what already exists, but as a total package including extendability, testing, documentation and community - you have no chance of competing. – JimL Mar 24 '16 at 19:05
  • When you load view on controller you don't need to do this `echo $this->load->view('hello',$data);` just do this `$this->load->view('hello',$data);` –  Mar 24 '16 at 19:06
  • Can you give us a real example from your current code base? – cmorrissey Mar 24 '16 at 19:07
  • 1
    Views guide http://www.codeigniter.com/user_guide/general/views.html and controllers guide http://www.codeigniter.com/user_guide/general/controllers.html –  Mar 24 '16 at 19:07
  • @JimL, totally agree with using another framework. Please see my comment re this, its the 2nd one on the list. – Elymentree Mar 24 '16 at 19:10
  • If you want a 'standalone' view component then the ['AURA Project' - view](https://github.com/auraphp/Aura.View) may be worth a look? – Ryan Vincent Mar 24 '16 at 19:12
  • Try to read some books on refractoring subject. Specifically for php like "Modernizing Legacy Applications" by Paul Jones , and something more wide like "Clean Code" by Robert C. Martin. – cssBlaster21895 Mar 24 '16 at 19:46
  • It is possible to make method in controller that will return whole string constisting all data, than pass it to view with other controller. Check [this](https://codeigniter.com/userguide3/general/views.html#returning-views-as-data) section in docs. – Tpojka Mar 24 '16 at 20:27
  • You seem to be confusing views and templates: http://stackoverflow.com/a/16596704/727208 – tereško Mar 25 '16 at 02:46

1 Answers1

0

Sure thing, that's what the frameworks are doing. Here's a really basic overview of how I'd expect that to work:

function view($template, $data){
    extract($data);       // this pulls all of the first-level array keys out as their own separate variables
    ob_start();           // this turns on **output buffering** which is the method we'll use to "capture" the contents of the view (there are surely other ways)
    require $template;    // you should prepend some sort of fixed path to this where all of your templates will reside
    echo ob_get_flush();  // turns off output buffering, returning the buffered content as a string which we then send to the browser.
}
Aaron Cicali
  • 1,496
  • 13
  • 24