include_once or include using variable from file name
I need to use variable in the filename. the file name is coming through the AJAX call.
Example: var FFile:String = WebRequest.ContentField['ClassFile'];
{$include_once FFile}
Tanks
Comments (6)
-
reporter -
repo owner The compiler switches are evaluated at compile time, variables are only available at runtime.
A solution in your case could be to have a single scripts, with all classes registered, you can make yourself a simple RegisterClass/FindClass combo like in Delphi (or may even do it directly, as they can be implemented in a single-line of code)
var vClasses : array [String] of TClass; procedure RegisterClass(aClass : TClass); begin vClasses[aClass.ClassName] := aClass; end; function FindClass(aName : String) : TClass; begin Result := vClasses[aName]; end;
then for each of your classes, in the "initialization" section of each unit implementing a class f.i.
RegisterClass(TMyClass);
It is also possible to do it statically
const cClasses : array [0..1] of TClass = [ TMyClass, TAnotherClass ]; function FindClass(aName : String) : TClass; begin for var c in cClasses do if c.ClassName = aName then exit c; end;
-
reporter Thanks for the answer. This response would be for the Issue # 141
-
reporter the temporary workaround I found to do include_where from a file based on a string is to do a single file and do test with IF THEN
files_content.pas
procedure include_by_name (aFileName: String); begin if aFileName = 'page_func' then {$ include_once 'view / page_func.pas' elseif aFileName = 'page_menu' then {$ include_once 'view / page_menu.pas' else if aFileName = 'page_product' then {$ include_once 'view / page_product.pas' endif; end;
engine.dws
{$ include_once 'controler / files_content.pas' include_by_name (WebRequest.ContentField ['ClassFile']);
I'm creating a componentized framework for use with delphiwebscript.
-
repo owner hmmmm, I'm not sure to understand, the initial post mentions an include, which is compile time, but the variable is runtime.
-
repo owner - changed status to invalid
Includes are compile time (without variables)
- Log in to comment
I'm trying to get the value of the variable, but I'm not getting it.