include_once or include using variable from file name

Issue #142 invalid
0r0 created an issue

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)

  1. 0r0 reporter

    I'm trying to get the value of the variable, but I'm not getting it.

    unit dwsCompiler;
    
    function TdwsCompiler.ReadInstrSwitch(const switchName : String) : Boolean;
    var
    ...
       FProgramInfo : TProgramInfo;
    begin
       ......
    {expresion get file name from variable}
                if (name[1]='_') then
                begin
                  FProgramInfo := TProgramInfo.Create;
                  FProgramInfo.Table := CurrentProg.Table;
                  FProgramInfo.Execution := TdwsProgramExecution(Self.CompileTimeExecution);
                  name:=FProgramInfo.Vars[name].ValueAsString;
                  FProgramInfo.Free;
                end;
    
    ....
    
  2. Eric Grange 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;
    
  3. 0r0 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.

  4. Eric Grange repo owner

    hmmmm, I'm not sure to understand, the initial post mentions an include, which is compile time, but the variable is runtime.

  5. Log in to comment