delphimvcframework
delphimvcframework copied to clipboard
Mustache path issue for ISAPI modules (and fix)
Changes since Sodium to increase the efficiency of loading Mustache templates has broken ISAPI implementations with a path error for locating templates.
The cause is in the LoadPartials method, where the following line uses the directory that the IIS Worker Process (c:\windows\system32\inetsrv\w3wp.exe) as the root for resolving relative paths rather than the directory where the ISAPI dll is located.
procedure TMVCMustacheViewEngine.LoadPartials;
<snip>
lPartialFileNames := TDirectory.GetFiles(lViewPath, '*.' + lViewsExtension, TSearchOption.soAllDirectories);
As the directory does not exist under the w3wp directory, the Delphi runtime will return an error
'The specified path was not found [c:\windows\system32\inetsrv\templates]'.
Previous functionality in Sodium and earlier did not try to get a list of all mustache partials and only resolved this at view time with a call to GetRealFileName. This internally makes a call (through several layers of code) to GetModuleName which will return the full path and name of either a native EXE or the DLL that the code is running in.
The proposed fix is to change the code in LoadPartials
procedure TMVCMustacheViewEngine.LoadPartials;
<snip>
if not gPartialsLoaded then
begin
lViewsExtension := Config[TMVCConfigKey.DefaultViewFileExtension];
lViewPath := TMVCBase.GetApplicationFileNamePath + Config.Value[TMVCConfigKey.ViewPath];
<snip>
This requires that the class function TMVCBase.GetApplicationFileNamePath is made public.
I've also noted that the Execute method will always make a disk access when called, even though all of the templates have been previously loaded. Mustache caches these, but unfortunately uses the raw text as the key to retrieve the parsed and cached template object. This is extremely inefficient and creates a bottleneck with relatively slow disk access compared to fetching from memory. I have some code which would make this more efficient i.e. create a dictionary to cache the text content of the raw templates, using the name of the template as the key.
Hi David, I currently don't use Mustache so frequently anymore (I switched to TemplatePro which is more powerful and more Delphi friendly) so feel free to PR any improvements you think. I'll review and merge them.
@fastbike any news about this? Can the issue be closed?