Skip to content

Commit be1c080

Browse files
committed
improve content on view template
1 parent 409b939 commit be1c080

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

working-with-views/index.md

+79
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,85 @@ var view : IView;
224224
view := TGroupView.create([headerView, contentView, footerView]);
225225
```
226226

227+
## Compose view with object inheritance
228+
Because instance of `IView` is just ordinary class. You can use Free Pascal object-oriented programming support to create complex view structure. For example if you have following base template
229+
230+
```
231+
TBaseTemplate = class abstract (TInterfacedObject, IView)
232+
protected
233+
function pageTitle() : string; virtual; abstract;
234+
function headCss() : string; virtual; abstract;
235+
function scriptJs() : string; virtual; abstract;
236+
function mainContent() : string; virtual; abstract;
237+
public
238+
function render(
239+
const viewParams : IViewParameters;
240+
const response : IResponse
241+
) : IResponse;
242+
end
243+
244+
...
245+
function TBaseTemplate.render(
246+
const viewParams : IViewParameters;
247+
const response : IResponse
248+
) : IResponse;
249+
begin
250+
response.body().write(
251+
'<html>' +
252+
'<head>' +
253+
'<title> ' + pageTitle() +' </title>' +
254+
'<style>.container { color : red }</style>' +
255+
headCss() +
256+
'</head>' +
257+
'<body>' +
258+
'<div class="container">' +
259+
mainContent() +
260+
'</div>' +
261+
'<script src="jquery.js"></script>' +
262+
scriptJss() +
263+
'</body>' +
264+
'<html>'
265+
);
266+
result := response;
267+
end;
268+
```
269+
For home template view, you can just inherit from TBaseTemplate
270+
271+
```
272+
THomeTemplate = class (TBaseTemplate)
273+
protected
274+
function pageTitle() : string; override;
275+
function headCss() : string; override;
276+
function scriptJs() : string; override;
277+
function mainContent() : string; override;
278+
end
279+
...
280+
281+
function THomeTemplate.pageTitle() : string;
282+
begin
283+
result := 'Homepage';
284+
end;
285+
286+
function THomeTemplate.headCss() : string;
287+
begin
288+
//set any CSS required for home template
289+
result := '<style>.home { font-size:1em; }</style>';
290+
end;
291+
292+
function THomeTemplate.scriptJs() : string;
293+
begin
294+
//set any JavaScript code required for home template
295+
result := '<script></script>';
296+
end;
297+
298+
function THomeTemplate.mainContent() :string;
299+
begin
300+
//return main content of home template
301+
result := '<p>Home template</p>';
302+
end;
303+
```
304+
For other page that share similar look and feel, just create another view class inherites from `TBaseTemplate`.
305+
227306
## Compose view from one or more view partials
228307

229308
View partial is basically any class which implements `IViewPartial` interface. This interface has only one method `partial()` that implementor must provide.

0 commit comments

Comments
 (0)