From 0c80ec022d4721224bb7cbe6b5d0f283e49e0f16 Mon Sep 17 00:00:00 2001 From: Daniele Teti Date: Mon, 2 Sep 2024 20:04:44 +0200 Subject: [PATCH] Updated samples to TemplatePro 0.5 --- .../bin/templates/editperson.html | 18 ++++---- .../bin/templates/people_list.csv.html | 4 +- .../bin/templates/people_list.html | 14 +++---- .../bin/templates/people_list_bottom.html | 2 +- .../bin/templates/people_table.html | 8 ++-- .../htmx_website/bin/templates/_header.html | 2 +- .../bin/templates/pages/customers.html | 4 +- .../bin/templates/pages/home.html | 5 ++- .../bin/templates/pages/posts.html | 4 +- .../bin/templates/pages/users.html | 4 +- .../bin/templates/partials/sidenav.html | 1 - samples/htmx_website/htmx_website.dpr | 1 - .../instant_search_with_htmx/BooksSearch.dpr | 2 + .../BooksSearch.dproj | 1 - .../bin/templates/index.html | 4 +- .../bin/templates/search_results.html | 10 ++--- ...VCFramework.View.Renderers.TemplatePro.pas | 33 ++++++++++++++- sources/TemplatePro.pas | 41 +++++++++++++++---- 18 files changed, 107 insertions(+), 51 deletions(-) diff --git a/samples/htmx_templatepro/bin/templates/editperson.html b/samples/htmx_templatepro/bin/templates/editperson.html index ebdbb226..86207747 100644 --- a/samples/htmx_templatepro/bin/templates/editperson.html +++ b/samples/htmx_templatepro/bin/templates/editperson.html @@ -1,11 +1,11 @@ -{{if(!ishtmx)}} -{{include("partials/header.html")}} +{{if !ishtmx}} +{{include "partials/header.html"}} {{endif}}
- {{if(!person)}} + {{if !person}}

New Person

{{else}}

Edit Person

@@ -40,9 +40,9 @@

Edit Person

(Ctrl+Click to select multiple devices)
@@ -59,7 +59,7 @@

Edit Person

- {{if(person)}} + {{if person}}
-{{if(!ishtmx)}} -{{include("partials/footer.html")}} +{{if !ishtmx}} +{{include "partials/footer.html"}} {{endif}} diff --git a/samples/htmx_templatepro/bin/templates/people_list.csv.html b/samples/htmx_templatepro/bin/templates/people_list.csv.html index 95ee3eae..16a2a465 100644 --- a/samples/htmx_templatepro/bin/templates/people_list.csv.html +++ b/samples/htmx_templatepro/bin/templates/people_list.csv.html @@ -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}} \ No newline at end of file +{{endfor}} \ No newline at end of file diff --git a/samples/htmx_templatepro/bin/templates/people_list.html b/samples/htmx_templatepro/bin/templates/people_list.html index b3e8b025..52dfabc6 100644 --- a/samples/htmx_templatepro/bin/templates/people_list.html +++ b/samples/htmx_templatepro/bin/templates/people_list.html @@ -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"}}
- {{include("people_table.html")}} + {{include "people_table.html"}}
-{{include("people_list_bottom.html")}} -{{if(!ishtmx)}} -{{include("partials/footer.html")}} +{{include "people_list_bottom.html"}} +{{if !ishtmx}} +{{include "partials/footer.html"}} {{endif}} diff --git a/samples/htmx_templatepro/bin/templates/people_list_bottom.html b/samples/htmx_templatepro/bin/templates/people_list_bottom.html index d98947f1..8db951bd 100644 --- a/samples/htmx_templatepro/bin/templates/people_list_bottom.html +++ b/samples/htmx_templatepro/bin/templates/people_list_bottom.html @@ -8,4 +8,4 @@
-{{include("partials/modal_placeholder.html")}} \ No newline at end of file +{{include "partials/modal_placeholder.html"}} \ No newline at end of file diff --git a/samples/htmx_templatepro/bin/templates/people_table.html b/samples/htmx_templatepro/bin/templates/people_table.html index 74f558d4..ea25170d 100644 --- a/samples/htmx_templatepro/bin/templates/people_table.html +++ b/samples/htmx_templatepro/bin/templates/people_table.html @@ -9,20 +9,20 @@ - {{if(!people)}} + {{if !people}} <<No People Found>> {{else}} - {{loop(people) as person}} + {{for person in people}} {{:person.@@index}} {{:person.FirstName|capitalize}} {{:person.LastName|capitalize}} {{:person.Age}} - {{include("delete_person_link.html")}} | {{include("view_person_link.html")}} + {{include "delete_person_link.html"}} | {{include "view_person_link.html"}} - {{endloop}} + {{endfor}} {{endif}} diff --git a/samples/htmx_website/bin/templates/_header.html b/samples/htmx_website/bin/templates/_header.html index 1e923319..08503baa 100644 --- a/samples/htmx_website/bin/templates/_header.html +++ b/samples/htmx_website/bin/templates/_header.html @@ -62,6 +62,6 @@
- {{include("partials/sidenav.html")}} + {{include "partials/sidenav.html"}}
\ No newline at end of file diff --git a/samples/htmx_website/bin/templates/pages/customers.html b/samples/htmx_website/bin/templates/pages/customers.html index f3754462..8d6640fc 100644 --- a/samples/htmx_website/bin/templates/pages/customers.html +++ b/samples/htmx_website/bin/templates/pages/customers.html @@ -1,3 +1,3 @@ -{{if(ispage)}}{{include("../_header.html")}}{{endif}} +{{if ispage}}{{include "../_header.html"}}{{endif}}

Customers

-{{if(ispage)}}{{include("../_footer.html")}}{{endif}} \ No newline at end of file +{{if ispage}}{{include "../_footer.html"}}{{endif}} \ No newline at end of file diff --git a/samples/htmx_website/bin/templates/pages/home.html b/samples/htmx_website/bin/templates/pages/home.html index 65fe83b3..d028c799 100644 --- a/samples/htmx_website/bin/templates/pages/home.html +++ b/samples/htmx_website/bin/templates/pages/home.html @@ -1,3 +1,4 @@ -{{if(ispage)}}{{include("../_header.html")}}{{endif}} +{{if ispage}}{{include "../_header.html"}}{{endif}}

HTMX WebSite

-{{if(ispage)}}{{include("../_footer.html")}}{{endif}} \ No newline at end of file +

Powered by DMVCFramework, TemplatePro and HTMX

+{{if ispage}}{{include "../_footer.html"}}{{endif}} \ No newline at end of file diff --git a/samples/htmx_website/bin/templates/pages/posts.html b/samples/htmx_website/bin/templates/pages/posts.html index ca28176f..9cd7d08b 100644 --- a/samples/htmx_website/bin/templates/pages/posts.html +++ b/samples/htmx_website/bin/templates/pages/posts.html @@ -1,3 +1,3 @@ -{{if(ispage)}}{{include("../_header.html")}}{{endif}} +{{if ispage}}{{include "../_header.html"}}{{endif}}

Posts

-{{if(ispage)}}{{include("../_footer.html")}}{{endif}} \ No newline at end of file +{{if ispage}}{{include "../_footer.html"}}{{endif}} \ No newline at end of file diff --git a/samples/htmx_website/bin/templates/pages/users.html b/samples/htmx_website/bin/templates/pages/users.html index 2fb5b971..cd587386 100644 --- a/samples/htmx_website/bin/templates/pages/users.html +++ b/samples/htmx_website/bin/templates/pages/users.html @@ -1,3 +1,3 @@ -{{if(ispage)}}{{include("../_header.html")}}{{endif}} +{{if ispage}}{{include "../_header.html"}}{{endif}}

Users

-{{if(ispage)}}{{include("../_footer.html")}}{{endif}} \ No newline at end of file +{{if ispage}}{{include "../_footer.html"}}{{endif}} \ No newline at end of file diff --git a/samples/htmx_website/bin/templates/partials/sidenav.html b/samples/htmx_website/bin/templates/partials/sidenav.html index b6899ce7..f01d3cc4 100644 --- a/samples/htmx_website/bin/templates/partials/sidenav.html +++ b/samples/htmx_website/bin/templates/partials/sidenav.html @@ -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"); diff --git a/samples/htmx_website/htmx_website.dpr b/samples/htmx_website/htmx_website.dpr index c64d1c98..0fcb47ff 100644 --- a/samples/htmx_website/htmx_website.dpr +++ b/samples/htmx_website/htmx_website.dpr @@ -13,7 +13,6 @@ uses MVCFramework.Logger, MVCFramework.DotEnv, MVCFramework.Commons, - MVCFramework.View.Renderers.Mustache, mormot.core.mustache, MVCFramework.Signal, ControllerU in 'ControllerU.pas', diff --git a/samples/instant_search_with_htmx/BooksSearch.dpr b/samples/instant_search_with_htmx/BooksSearch.dpr index e2c4f075..ca6f685f 100644 --- a/samples/instant_search_with_htmx/BooksSearch.dpr +++ b/samples/instant_search_with_htmx/BooksSearch.dpr @@ -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); diff --git a/samples/instant_search_with_htmx/BooksSearch.dproj b/samples/instant_search_with_htmx/BooksSearch.dproj index efd3a634..644a4d36 100644 --- a/samples/instant_search_with_htmx/BooksSearch.dproj +++ b/samples/instant_search_with_htmx/BooksSearch.dproj @@ -155,7 +155,6 @@
MyWebModule
- dfm TWebModule
diff --git a/samples/instant_search_with_htmx/bin/templates/index.html b/samples/instant_search_with_htmx/bin/templates/index.html index e67fb0d7..65ac11cb 100644 --- a/samples/instant_search_with_htmx/bin/templates/index.html +++ b/samples/instant_search_with_htmx/bin/templates/index.html @@ -1,4 +1,4 @@ - + @@ -18,7 +18,7 @@

⭐ DMVCFramework + HTMX :: Instant Search Demo ⭐

-
{{include("search_results.html")}}
+
{{include "search_results.html"}}
\ No newline at end of file diff --git a/samples/instant_search_with_htmx/bin/templates/search_results.html b/samples/instant_search_with_htmx/bin/templates/search_results.html index 61dd20a9..93defa9e 100644 --- a/samples/instant_search_with_htmx/bin/templates/search_results.html +++ b/samples/instant_search_with_htmx/bin/templates/search_results.html @@ -1,4 +1,4 @@ -

{{:books_count}} book/s found

+

{{:books|count}} book/s found

@@ -9,20 +9,20 @@ - {{if(!books)}} + {{if !books}} {{endif}} - {{loop(books) as book}} + {{for book in books}} - + - {{endloop}} + {{endfor}}
No books found
{{:book.id|lpad:8:"0"}}{{:book.id|lpad,8,"0"}} {{:book.book_name}} {{:book.author_name}} {{:book.genre|uppercase}}
\ No newline at end of file diff --git a/sources/MVCFramework.View.Renderers.TemplatePro.pas b/sources/MVCFramework.View.Renderers.TemplatePro.pas index 4fa1a744..c36c1e4c 100644 --- a/sources/MVCFramework.View.Renderers.TemplatePro.pas +++ b/sources/MVCFramework.View.Renderers.TemplatePro.pas @@ -52,8 +52,37 @@ implementation {$WARNINGS OFF} function GetDataSetOrObjectListCount(const aValue: TValue; const aParameters: TArray): 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): TValue; @@ -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; lActualFileTimeStamp: TDateTime; diff --git a/sources/TemplatePro.pas b/sources/TemplatePro.pas index b503261d..e1e033b5 100644 --- a/sources/TemplatePro.pas +++ b/sources/TemplatePro.pas @@ -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; @@ -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; @@ -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;