Skip to content

Commit 3942c21

Browse files
committed
add information on view stack view push
1 parent be1c080 commit 3942c21

File tree

1 file changed

+111
-1
lines changed

1 file changed

+111
-1
lines changed

working-with-views/index.md

+111-1
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,23 @@ end;
298298
function THomeTemplate.mainContent() :string;
299299
begin
300300
//return main content of home template
301-
result := '<p>Home template</p>';
301+
result := '<p>Homepage content</p>';
302302
end;
303303
```
304+
THomeTemplate will output
305+
306+
```
307+
<html><head><title>Homepage</title>
308+
<style>.container { color : red }</style>
309+
<style>.home { font-size:1em; }</style>
310+
</head>
311+
<body>
312+
<div class="container"><p>Homepage content</p></div>
313+
<script src="jquery.js"></script>
314+
<scrip></script>
315+
</body></html>
316+
```
317+
304318
For other page that share similar look and feel, just create another view class inherites from `TBaseTemplate`.
305319

306320
## Compose view from one or more view partials
@@ -473,6 +487,102 @@ Fano Framework provides two `IViewPartial` interface implementation
473487
- [`TViewPartial`](https://github.com/fanoframework/fano/blob/master/src/Mvc/Views/ViewPartialImpl.pas), this class loads template from file, replace any variable placeholders and output it as string.
474488
- [`TStrViewPartial`](https://github.com/fanoframework/fano/blob/master/src/Mvc/Views/StrViewPartialImpl.pas), it is similar as above but loads template from string.
475489

490+
## Compose view using view stack
491+
492+
View stack is similar to view partial except that you can push string as many as you need and view stack will concat and render pushed string. It is inspired by Laravel Blade `@stack` and `@push` directive. However, in Fano Framework, we use `IViewStack` and `IViewPush` interface.
493+
494+
To explain view stack, let use `TBaseTemplate` class in example above
495+
496+
```
497+
TBaseTemplate = class abstract (TInterfacedObject, IView)
498+
private
499+
fViewStack : IViewStack;
500+
protected
501+
fViewPush : IViewPush;
502+
function pageTitle() : string; virtual; abstract;
503+
function headCss() : string; virtual; abstract;
504+
function scriptJs() : string; virtual; abstract;
505+
function mainContent() : string; virtual; abstract;
506+
public
507+
constructor create();
508+
function render(
509+
const viewParams : IViewParameters;
510+
const response : IResponse
511+
) : IResponse;
512+
end
513+
514+
...
515+
constructor TBaseTemplate.create();
516+
begin
517+
fViewStack := TViewStack.create(TTemplateParser.create());
518+
fViewPush := fViewStack as IViewPush;
519+
end;
520+
521+
function TBaseTemplate.render(
522+
const viewParams : IViewParameters;
523+
const response : IResponse
524+
) : IResponse;
525+
begin
526+
response.body().write(
527+
'<html>' +
528+
'<head>' +
529+
'<title> ' + pageTitle() +' </title>' +
530+
'<style>.container { color : red }</style>' +
531+
headCss() +
532+
'</head>' +
533+
'<body>' +
534+
'<div class="container">' +
535+
mainContent() +
536+
'</div>' +
537+
'<script src="jquery.js"></script>' +
538+
scriptJss() +
539+
fViewStack.stack('js', fViewParams) +
540+
'</body>' +
541+
'<html>'
542+
);
543+
result := response;
544+
end;
545+
```
546+
Please note that now we have view stack created and we setup `js` name where any pushed string will go. Let us use example `THomeTemplate` class above and use view push instance
547+
548+
```
549+
THomeTemplate = class (TBaseTemplate)
550+
protected
551+
...
552+
function pageTitle() : string; override;
553+
function mainContent() : string; override;
554+
end
555+
...
556+
function THomeTemplate.pageTitle() : string;
557+
begin
558+
result := 'Homepage';
559+
fViewPush.push('js', '<script src="hello.js"></script>');
560+
end;
561+
562+
function THomeTemplate.mainContent() : string;
563+
begin
564+
fViewPush.push('js', '<script src="thirdparty.js"></script>');
565+
result := '<p>Homepage content</p>';
566+
fViewPush.push('js', '<script src="myapp.js"></script>');
567+
end;
568+
```
569+
will result tags at bottom of HTML document.
570+
571+
```
572+
<html><head><title>Homepage</title>
573+
<style>.container { color : red }</style>
574+
<style>.home { font-size:1em; }</style>
575+
</head>
576+
<body>
577+
<div class="container"><p>Homepage content</p></div>' +
578+
<script src="jquery.js"></script>
579+
<scrip></script>
580+
<script src="hello.js"></script>
581+
<script src="thirdparty.js"></script>
582+
<script src="myapp.js"></script>
583+
</body></html>
584+
```
585+
476586
## View for non HTML presentation
477587

478588
While many built-in implementations of `IView` interface are related to HTML presentation, you can use it to generate other presentation such as JSON or PDF document.

0 commit comments

Comments
 (0)