Well... I'm out of ideas on what to write about. Do you have any ideas? Please, write me in any comment section you find, and I will do my best to tell you about that particular subject

Fat-Free Framework – 7. Enter the Views. How to separate logic from presentation in F3

Created at: October 21, 2016; Last update: October 21, 2016

Up until now we’ve done our tests by echoing to the browser window the variables. Of course, this was only to do our tests and to see if what we do is ending well or not.

But now we should talk about the Views in MVC. Do you remember the global variables that Fat-Free Framework is using extensively in what it names the Hive? Well… Among these global variables there exists one named UI (from user-interface?). This variable tells the framework where it can find the views in case these views are called.

So, let’s create a directory named “Views” inside our “App” directory, and then call the setup.cfg (“App/Config”) and add a new line where we define this UI variable:

App/Config/setup.cfg

PHP

1

2

3

4

[globals]

DEBUG=3

AUTOLOAD=App/Controllers/

UI=App/Views/

Now let’s create a view inside our new Views directory, named HomepageView.php, and try to output something:

App/Views/HomepageView.php

PHP

1

2

<?php

echo'hello from index';

Now, going back to “App/Controllers/Homepage.php“, we do the following:

App/Controllers/Homepage.php

PHP

1

2

3

4

5

6

7

8

9

<?php

classHomepage{

functionindex(){

$view=\View::instance();

echo$view->render('HomepageView.php');

}

}

Now if we visit the homepage of our site, everything looks ok. But how do we pass variables to the view? We pass them as parameters inside our render() method, inside an array. As a test, let’s return to Homepage.php and write: