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.

12
  • 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? Commented Mar 24, 2016 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.' Commented Mar 24, 2016 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. Commented Mar 24, 2016 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. Commented Mar 24, 2016 at 19:05
  • 1
    Views guide codeigniter.com/user_guide/general/views.html and controllers guide codeigniter.com/user_guide/general/controllers.html Commented Mar 24, 2016 at 19:07

1 Answer 1

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. } 
Sign up to request clarification or add additional context in comments.

3 Comments

Great! This helped me a lot. Actually addresses the question and not give an opinion about MVC framework.
That's a function for rendering a template, not a view.
@tereško, understood. That's why my response say's 'this helped me a lot', needed to tweak it to suit my needs. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.