Comments (6)
Is this to be able to handle some of the helper functionality that is defined by the underlying library ?
I notice that by default nil is passed through to the Mustache rendering engine for the helper collection
procedure TMVCMustacheViewEngine.Execute(const ViewName: string; const OutputStream: TStream);
// snip
lSW.Write(UTF8Tostring(lViewEngine.RenderJSON(FJSONModel, lPartials, nil, nil)));
// snip
A simple change to
lSW.Write(UTF8Tostring(lViewEngine.RenderJSON(FJSONModel, lPartials, TSynMustache.HelpersGetStandardList, nil)));
allows the use of inbuilt formatting functions etc so the following template will output a formatted date rather than an ISODateTime string
{{#Conditions}}
<tr class="c-table__row">
<td>{{OnsetDateTime}}</td>
<td>{{DateFmt OnsetDateTime, "dd mmm yyyy"}}</td>
</tr>
{{/Conditions}}
from delphimvcframework.
On a related note there is the ability to pass a translate callback to the TSynMustache renderJSON method. This too is not exposed.
Also on a more general note the TSynMustache class has the ability to register other helpers (aka Lamdba in the mustache world).
So the ability to configure the Mustache Engine before rendering templates would be very useful. I can have a go at some changes, as I have a new project where the ability to format ISO DateTimes via the template rather than via the json data would be extremely useful.
from delphimvcframework.
From an initial examination it looks like some type of dependency injection is required for the TMVCBaseViewEngine.
Exactly where is not at all clear - my normal patterns are:
- At the constructor level but this assumes you know what type of configuration helper to add for any given rendering engine at the moment it is constructed, which is unlikely given the class type is registered, not an instance.
- As an extra parameter on the method that does the action (Execute in this case). At render time we have an instance of the ViewEngine so would need to pass a handle out to the MVCEngine (some type of callback maybe) so this could be set in the web module immediately after SetViewEngine.
- As an additional property on the rendering engine class that can be assigned to run a callback at the appropriate time. This would need to be checked inside of the Execute method after the instance of the rendering view engine was created.
I'm probably going to subclass the TMVCMustacheViewEngine to see if I can work something into here.
from delphimvcframework.
My initial work is to add the following to the existing TMVCMustacheViewEngine class
TMVCMustacheViewEngine = class(TMVCBaseViewEngine)
protected
function RenderJSON(lViewEngine: TSynMustache; const JSON: RawUTF8; Partials: TSynMustachePartials;
Helpers: TSynMustacheHelpers; OnTranslate: TOnStringTranslate; EscapeInvert: boolean): RawUTF8; virtual;
and then change the Execute method to prepopulate the Helpers and make the call to the new method
procedure TMVCMustacheViewEngine.Execute(const ViewName: string; const OutputStream: TStream);
var
// snip
lPartials: TSynMustachePartials;
lHelpers: TSynMustacheHelpers;
begin
// snip
lHelpers := TSynMustacheAccess.HelpersGetStandardList;
lSW.Write(UTF8Tostring(RenderJSON(lViewEngine, FJSONModel, lPartials, lHelpers, nil, false)));
finally
//snip
end;
end;
// and a base implementation of the new virtual method
function TMVCMustacheViewEngine.RenderJSON(lViewEngine: TSynMustache; const JSON: RawUTF8; Partials: TSynMustachePartials;
Helpers: TSynMustacheHelpers; OnTranslate: TOnStringTranslate; EscapeInvert: boolean): RawUTF8;
begin
Result := lViewEngine.RenderJSON(JSON, Partials, Helpers, OnTranslate, EscapeInvert);
end;
from delphimvcframework.
And I can create a new subclass if I wanted to add additional features to the Mustache syntax e.g.
type
TMVCMustacheViewEnginePlus = class(TMVCMustacheViewEngine)
procedure Translate(var English: string);
procedure Test(const Value: variant; out result: variant);
protected
function RenderJSON(lViewEngine: TSynMustache; const JSON: RawUTF8; Partials: TSynMustachePartials;
Helpers: TSynMustacheHelpers; OnTranslate: TOnStringTranslate; EscapeInvert: boolean): RawUTF8; override;
end;
function TMVCMustacheViewEnginePlus.RenderJSON(lViewEngine: TSynMustache; const JSON: RawUTF8; Partials: TSynMustachePartials;
Helpers: TSynMustacheHelpers; OnTranslate: TOnStringTranslate; EscapeInvert: boolean): RawUTF8;
begin
TSynMustacheAccess.HelperAdd(Helpers, 'Test', Test);
OnTranslate := Translate;
result := inherited;
end;
// make the text Italien i.e. {{"class}} is ouput as clazz LOL
procedure TMVCMustacheViewEnginePlus.Translate(var English: string);
begin
English := English.Replace('s', 'z', [rfReplaceAll, rfIgnoreCase]);
end;
from delphimvcframework.
Just added some proposed code changes. I see you've been busy on the Mustache Rendering logic too.
from delphimvcframework.
Related Issues (20)
- Helpers for HTMX HOT 7
- Using Spring4D Collections in ViewData HOT 18
- TMVCActiveRecordMiddleware error - Object factory for class is missing HOT 1
- SelectByNamedQuery created exception when using with Microsoft SQL Server HOT 2
- TMVCActiveRecordController - refresh=true
- Packages for older versions contains bad includes section
- [HOW TO] Running on another port - via commandline switch. HOT 2
- Character % doesn't work in URL anymore HOT 6
- Request for crud sample using ADO HOT 2
- [htmx-sample] Rename all the class helper method with an "HX" suffix to avoid confusion
- Add "MVCFromContentField" attribute
- Embeded e-Mail Sending System feature request
- JSON Serializing floating point properties. HOT 8
- Nested Mustache Partials Rendering HOT 4
- Error: No mapping for the Unicode character exists in the target multi-byte code HOT 4
- The TUserPasswordChecker class does not exist HOT 1
- Error when compile to Linux HOT 5
- Model with multiple Primary Key. HOT 4
- Swagger Param Body Array HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from delphimvcframework.