Recommend this page to a friend! |
Classes of Saro Carvello | PHP Web MVC Framework | wiki/View.md | Download |
|
![]() IntroductionWe are now introducing the concrete class Using the ViewIn MVC Design pattern the View layer has the prior responsibility to manage and show data in graphical structures like HTML page. With WebMVC, this responsibility is managed by a View that operates together with a Template file containing the HTML static design. To use a View you must:
So, when you having the need to show an HTML page rather than a simple message (like we made with the EchoController) you must create a template file containing the HTML. Later you also need to manage the template by creating an object of the concrete class "frameworks\View.php". You also need to build a controller in a way to enabling the execution of the view object previously introduced to produce the output. This because the controller is the only MVC entity that you are able to instantiate and run (by typing an HTTP request). Pratically: Create the template
Then create the Hello controller
The following figure shows the WebMVC directory tree with files location: Finally, run the controller by typing the following address into your web browser:
You should see the page: Explanation: Controller, View and Template cooperationWe create an HTML static content into a file and save it in the directory _templates_. The file must have a .tpl
extension in order to be accepted by WebMVC. Note that the template file must be written only by using client-side
technologies like _HTML_, _JavaScript_ or _CSS_ programming languages. This is a constraint specially designed in WebMVC
to avoid mixing between server-side, like PHP, and client-side technologies within a single source code file.
Then, in order to manage and render out the static HTML design of Insights: Best practices of using OOP and autorun for extending the GUI designThe purpose of this section is to give you another example regarding the advantages deriving from using OOP and autorun.
Supposing we want to have a "Bootstrap" mobile version of the previous page. We can do this job simply by extending
the
Now we use Bootstrap for the mobile template of GUI. Note that it is ineffective on the server-side code developed so far thanks to the features of WebMVC that isolate client-side technologies in external template files
Now run by typing:
You should see the bootstrap mobile version of the homepage SummaryThis example shows you how WebMVC, by separating GUI Design, View and Controller and by managing their cooperation realizes an efficient implementation of the "Separation of Concern". We will discuss here in depth about this benefit. Right now consider only the advantage deriving from avoid to mix programming languages into a single source code when you need to show the static content of a web page rather than simple string messages. We also focused on how OOP and even more the controller autorun behavior give you effective ways to specialize your code. The figure below shows the View usage Whats nextIn the next example, we expose how to manage dynamic content. For dynamic content, we define a content inside a web page that is evaluated and generated at runtime, rather than static content we can design at development time inside a template. For this purpose, we can adopt a better practice to using and instantiating the View. |