Skip to content

Commit

Permalink
Updated samples to TemplatePro 0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Sep 2, 2024
1 parent b289b2f commit 0c80ec0
Show file tree
Hide file tree
Showing 18 changed files with 107 additions and 51 deletions.
18 changes: 9 additions & 9 deletions samples/htmx_templatepro/bin/templates/editperson.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{{if(!ishtmx)}}
{{include("partials/header.html")}}
{{if !ishtmx}}
{{include "partials/header.html"}}
{{endif}}
<form class="form form-horizontal" id="myForm" name="myForm" hx-post="/people" hx-target="#main">
<input type="hidden" value="{{:person.guid}}" name="guid">
<div class="row">
<div class="col">
{{if(!person)}}
{{if !person}}
<h3>New Person</h3>
{{else}}
<h3>Edit Person</h3>
Expand Down Expand Up @@ -40,9 +40,9 @@ <h3>Edit Person</h3>
<div class="form-group">
<label for="items" class="col-sm-10 control-label">Devices</label>
<select id="items" name="items" multiple class="form-control">
{{loop(deviceslist) as device}}
<option value="{{:device.devicename}}" {{if(device.selected)}}selected{{endif}}>{{:device.devicename|capitalize}}</option>
{{endloop}}
{{for device in deviceslist}}
<option value="{{:device.devicename}}" {{if device.selected}}selected{{endif}}>{{:device.devicename|capitalize}}</option>
{{endfor}}
</select>
<span style="font-size: 80%">(Ctrl+Click to select multiple devices)</span>
</div>
Expand All @@ -59,7 +59,7 @@ <h3>Edit Person</h3>
<button type="submit" class="btn btn-primary btn-block w-100">Save</button>
</div>
<div class="col">
{{if(person)}}
{{if person}}
<button
type="button"
hx-confirm="Are you sure you wish to delete user?"
Expand All @@ -74,6 +74,6 @@ <h3>Edit Person</h3>
</div>

</form>
{{if(!ishtmx)}}
{{include("partials/footer.html")}}
{{if !ishtmx}}
{{include "partials/footer.html"}}
{{endif}}
4 changes: 2 additions & 2 deletions samples/htmx_templatepro/bin/templates/people_list.csv.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
guid;first_name;last_name;age
{{loop(people) as person}}
{{for person in people}}
{{:person.guid}};"{{:person.firstname}}";"{{:person.lastname}}";{{:person.age}}
{{endloop}}
{{endfor}}
14 changes: 7 additions & 7 deletions samples/htmx_templatepro/bin/templates/people_list.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{{if(!ishtmx)}}
{{include("partials/header.html")}}
{{if !ishtmx}}
{{include "partials/header.html"}}
{{endif}}
{{include("people_list_search.html")}}
{{include "people_list_search.html"}}
<div class="row" id="people_list" hx-target="#main">
<div class="col">
{{include("people_table.html")}}
{{include "people_table.html"}}
</div>
</div>
{{include("people_list_bottom.html")}}
{{if(!ishtmx)}}
{{include("partials/footer.html")}}
{{include "people_list_bottom.html"}}
{{if !ishtmx}}
{{include "partials/footer.html"}}
{{endif}}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
</div>
</div>

{{include("partials/modal_placeholder.html")}}
{{include "partials/modal_placeholder.html"}}
8 changes: 4 additions & 4 deletions samples/htmx_templatepro/bin/templates/people_table.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
</tr>
</thead>
<tbody>
{{if(!people)}}
{{if !people}}
<tr>
<td class="text-center" colspan="5">&lt;&lt;No People Found&gt;&gt;</td>
</tr>
{{else}}
{{loop(people) as person}}
{{for person in people}}
<tr>
<td>{{:person.@@index}}</td>
<td>{{:person.FirstName|capitalize}}</td>
<td>{{:person.LastName|capitalize}}</td>
<td>{{:person.Age}}</td>
<td class="text-end">{{include("delete_person_link.html")}} | {{include("view_person_link.html")}}</td>
<td class="text-end">{{include "delete_person_link.html"}} | {{include "view_person_link.html"}}</td>
</tr>
{{endloop}}
{{endfor}}
{{endif}}
</tbody>
</table>
2 changes: 1 addition & 1 deletion samples/htmx_website/bin/templates/_header.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@
<main class="container-fluid">
<div class="parent">
<!-- Side Navigation -->
{{include("partials/sidenav.html")}}
{{include "partials/sidenav.html"}}
<!-- Main Content Area -->
<div class="main" id="main">
4 changes: 2 additions & 2 deletions samples/htmx_website/bin/templates/pages/customers.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{if(ispage)}}{{include("../_header.html")}}{{endif}}
{{if ispage}}{{include "../_header.html"}}{{endif}}
<h1 class="text-2xl font-bold mb-4">Customers</h1>
{{if(ispage)}}{{include("../_footer.html")}}{{endif}}
{{if ispage}}{{include "../_footer.html"}}{{endif}}
5 changes: 3 additions & 2 deletions samples/htmx_website/bin/templates/pages/home.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{if(ispage)}}{{include("../_header.html")}}{{endif}}
{{if ispage}}{{include "../_header.html"}}{{endif}}
<h1 class="text-2xl font-bold mb-4">HTMX WebSite</h1>
{{if(ispage)}}{{include("../_footer.html")}}{{endif}}
<p>Powered by DMVCFramework, TemplatePro and HTMX</p>
{{if ispage}}{{include "../_footer.html"}}{{endif}}
4 changes: 2 additions & 2 deletions samples/htmx_website/bin/templates/pages/posts.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{if(ispage)}}{{include("../_header.html")}}{{endif}}
{{if ispage}}{{include "../_header.html"}}{{endif}}
<h1 class="text-2xl font-bold mb-4">Posts</h1>
{{if(ispage)}}{{include("../_footer.html")}}{{endif}}
{{if ispage}}{{include "../_footer.html"}}{{endif}}
4 changes: 2 additions & 2 deletions samples/htmx_website/bin/templates/pages/users.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{if(ispage)}}{{include("../_header.html")}}{{endif}}
{{if ispage}}{{include "../_header.html"}}{{endif}}
<h1 class="text-2xl font-bold mb-4">Users</h1>
{{if(ispage)}}{{include("../_footer.html")}}{{endif}}
{{if ispage}}{{include "../_footer.html"}}{{endif}}
1 change: 0 additions & 1 deletion samples/htmx_website/bin/templates/partials/sidenav.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
const highlightMenuItem = (evt) => {
let menuItems = document.querySelectorAll('#' + evt.srcElement.parentElement.id + ' > a');
menuItems.forEach((item) => {
console.log(item);
item.classList.remove("selected");
});
evt.srcElement.classList.add("selected");
Expand Down
1 change: 0 additions & 1 deletion samples/htmx_website/htmx_website.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ uses
MVCFramework.Logger,
MVCFramework.DotEnv,
MVCFramework.Commons,
MVCFramework.View.Renderers.Mustache,
mormot.core.mustache,
MVCFramework.Signal,
ControllerU in 'ControllerU.pas',
Expand Down
2 changes: 2 additions & 0 deletions samples/instant_search_with_htmx/BooksSearch.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ begin
// UseConsoleLogger defines if logs must be emitted to also the console (if available).
UseConsoleLogger := True;

MVCUseTemplatesCache := False;

LogI('** DMVCFramework Server ** build ' + DMVCFRAMEWORK_VERSION);

CreateSqlitePrivateConnDef(True);
Expand Down
1 change: 0 additions & 1 deletion samples/instant_search_with_htmx/BooksSearch.dproj
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@
<DCCReference Include="Controllers.BooksU.pas"/>
<DCCReference Include="WebModuleU.pas">
<Form>MyWebModule</Form>
<FormType>dfm</FormType>
<DesignClass>TWebModule</DesignClass>
</DCCReference>
<DCCReference Include="FDConnectionConfigU.pas"/>
Expand Down
4 changes: 2 additions & 2 deletions samples/instant_search_with_htmx/bin/templates/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
Expand All @@ -18,7 +18,7 @@
<p class="subtitle is-6">⭐ DMVCFramework + HTMX :: Instant Search Demo ⭐</p>
</div>
</div>
<div id="results">{{include("search_results.html")}}</div>
<div id="results">{{include "search_results.html"}}</div>
</section>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<p>{{:books_count}} book/s found</p>
<p>{{:books|count}} book/s found</p>
<table class="table is-fullwidth is-striped is-hoverable">
<thead>
<tr>
Expand All @@ -9,20 +9,20 @@
</tr>
</thead>
<tbody>
{{if(!books)}}
{{if !books}}
<tr>
<td colspan="4">
<span>No books found</span>
</td>
</tr>
{{endif}}
{{loop(books) as book}}
{{for book in books}}
<tr>
<td>{{:book.id|lpad:8:"0"}}</td>
<td>{{:book.id|lpad,8,"0"}}</td>
<td>{{:book.book_name}}</td>
<td>{{:book.author_name}}</td>
<td>{{:book.genre|uppercase}}</td>
</tr>
{{endloop}}
{{endfor}}
</tbody>
</table>
33 changes: 31 additions & 2 deletions sources/MVCFramework.View.Renderers.TemplatePro.pas
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,37 @@ implementation
{$WARNINGS OFF}

function GetDataSetOrObjectListCount(const aValue: TValue; const aParameters: TArray<string>): TValue;
var
lWrappedList: IMVCList;
begin
// todo
if not aValue.IsObject then
begin
Result := False;
end;

if aValue.AsObject is TDataSet then
begin
Result := TDataSet(aValue.AsObject).RecordCount;
end
else if aValue.AsObject is TJsonArray then
begin
Result := TJsonArray(aValue.AsObject).Count;
end
else if aValue.AsObject is TJsonObject then
begin
Result := TJsonObject(aValue.AsObject).Count;
end
else
begin
if TDuckTypedList.CanBeWrappedAsList(aValue.AsObject, lWrappedList) then
begin
Result := lWrappedList.Count;
end
else
begin
Result := False;
end;
end;
end;

function DumpAsJSONString(const aValue: TValue; const aParameters: TArray<string>): TValue;
Expand Down Expand Up @@ -82,7 +111,7 @@ procedure TMVCTemplateProViewEngine.Execute(const ViewName: string; const Builde
var
lTP: TTProCompiler;
lViewFileName: string;
lViewTemplate: UTF8String;
lViewTemplate: String;
lCompiledTemplate: ITProCompiledTemplate;
lPair: TPair<String, TValue>;
lActualFileTimeStamp: TDateTime;
Expand Down
41 changes: 34 additions & 7 deletions sources/TemplatePro.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1218,12 +1218,6 @@ function HTMLEncode(s: string): string;
end;

function HTMLSpecialCharsEncode(s: string): string;
procedure repl(var s: string; r: string; posi: Integer);
begin
Delete(s, posi, 1);
Insert(r, s, posi);
end;

var
I: Integer;
r: string;
Expand Down Expand Up @@ -1432,7 +1426,8 @@ function HTMLSpecialCharsEncode(s: string): string;
end;
if r <> '' then
begin
repl(s, '&' + r + ';', I);
s := s.Replace(s[I], '&' + r + ';');
Inc(I, Length(r) + 1);
end;
Inc(I)
end;
Expand Down Expand Up @@ -2127,12 +2122,44 @@ function TTProCompiledTemplate.IsAnIterator(const VarName: String; out DataSourc
function TTProCompiledTemplate.IsTruthy(const Value: TValue): Boolean;
var
lStrValue: String;
lWrappedList: ITProWrappedList;
begin
if Value.IsEmpty then
begin
Exit(False);
end;
lStrValue := Value.ToString;
if Value.IsObjectInstance then
begin
if Value.AsObject = nil then
begin
lStrValue := '';
end
else if Value.AsObject is TDataSet then
begin
lStrValue := TDataSet(Value.AsObject).RecordCount.ToString;
end
else if Value.AsObject is TJsonArray then
begin
lStrValue := TJsonArray(Value.AsObject).Count.ToString;
end
else if Value.AsObject is TJsonObject then
begin
lStrValue := TJsonObject(Value.AsObject).Count.ToString;
end
else
begin
lWrappedList := TTProDuckTypedList.Wrap(Value.AsObject);
if lWrappedList = nil then
begin
lStrValue := '';
end
else
begin
lStrValue := lWrappedList.Count.ToString;
end;
end;
end;
Result := not (SameText(lStrValue,'false') or SameText(lStrValue,'0') or SameText(lStrValue,''));
end;

Expand Down

0 comments on commit 0c80ec0

Please sign in to comment.