Web Connection supports MVC (Model, View, Controller) development using built-in HTML scripting that mixes HTML syntax and Foxpro code. Scripts and Templates provide two mechanisms for rendering HTML using two methods - Response.ExpandScript() and Response.ExpandTemplate() respectively - to render FoxPro expressions and values into a merged HTML template.
The Controller in MVC is typically the coordinator that ties together the Model - your data or business objects - and then renders a View of that model.
In MVC the controller retrieves model data from a database or business object, constructs a model (or uses the data directly) and then hands of the model to be rendered by the view.
That sounds a lot more complicated than it is - Web Connection applications use Process methods to implement the controller and HTML templates or scripts for the View. Any model data retrieved can be fed into the script template via PRIVATE or PUBLIC variables, open cursors etc. that are visible further down the FoxPro call stack.
Here's a simple example of a Process class method (controller) method that retrieves a Customer object (model) and then renders it with a script (view):
FUNCTION GetCustomer lcId = Request.QueryString("id") LOCAL loCustBus loCustBus = CREATEOBJECT("cCustomer") poCustBus.Load(lcId) PRIVATE poCust && Important: Not local so it's visible poCust = poCust.oData && Customer data instance Response.ExpandScript()
or you can use Expand template and optionally pass in the explicit script name (~/ refers to the Web root folder):
to offload HTML generation to a script or template.
Templates can then access any FoxPro PRIVATE or PUBLIC variables, UDF()s or classes that are in scope and use those to render their output.
Markup typically looks like this for expressions:
<label>Company:</label> <b><%= poCust.Company %></b>
This works well for both scripts and templates. If you want to use code blocks you can use scripts:
<h3>Customer List</h3> <% SELECT * from Customers INTO CURSOR TCustomers SCAN %> <div class="customer-item"> <div class="customer-header"><%= TCustomers.Company %></div> <div class="customer-body"> <%= DisplayMemo(TCustomers.Address) %> </div> </div> <% ENDSCAN %>
Templates and scripts also support Partial Pages, Layout Pages and Sections to allow easy reuse of common content. Layout pages allow common content to be stored in a 'master' page, into which content pages can be loaded. Content pages then reference the layout page. Partials allow you to create reusable components that can be dropped into other pages. Use Partials for reusable components or simply to break complex pages into smaller and easier to manage partials.
I highly recommend reviewing these topics as they make use of Scripts and Templates much easier, reducing the amount of HTML you have to type repeatedly.
Find out more about Layout, Content and Partial pages.
The goal of scripts and templates is to separate your controller logic in the method code of your wwProcess class and the HTML generation. The controller code is responsible for setting up the variables, data or other state that the View (script or template) then uses to render the HTML result.
The benefit of the external file over hardcoding the HTML into the Controller method with say
Response.Write() is that you can change the markup simply by changing the text. Changing code requires recompilation and redeployment. It's also much easier to edit HTML as text and you can take advantage of powerful HTML editors like Visual Studio, WebStorm, Sublime or various other editors that support plug-ins for editing HTML.
- Allows editing in rich HTML editors
- Allows modification without recompilation
- Allows non-programmers to edit your HTML (designers)
If you are building applications using Web Connection wwProcess methods using script or templates is the preferred way to generate HTML output.
Comment or report problem with topic