Inline Functions

New feature iconThis feature is new for adTempus version 4.

Inline Functions allow you to use calls to script functions anywhere in adTempus that you can use Job Variables. For example, just as you can insert the value of a Job Variable in the command-line parameters for a program, you can call a script function and insert the result.

Defining Inline Functions

All inline functions are defined in a special Script Library named "InlineFunctions." This library defaults to VB.NET as its language, but you can change the language to C# if you prefer (make this change before you start adding functions, or you will have to convert them all to the new language).

To create an inline function, simply add a public method to this library. The library initially contains a few example methods.

Each method must return a value, but the value can be of any type you wish. adTempus calls the returned object's ToString method to get the value to insert.

Calling Inline Functions

You can insert a reference to an inline function anywhere in adTempus where Job Variables are supported. The syntax for inserting an Inline Function is similar to that for inserting a Job Variable. Suppose you have created a function called "GetDate" that returns the current date. You can insert that function in a text field using this syntax:

%=GetDate()%

If the return type of your function is something other than a string, you can use .NET formatting patterns to format the result when it is inserted, using the same syntax as for Job Variables. For example, if your GetDate function returns a DateTime object, you can format the date when you insert it:

%=GetDate(){yyyy-MM-dd}%

However, you could also call the ToString method to do the same thing:

%=GetDate().ToString("yyyy-MM-dd")%

Inline functions can accept parameters as well. For example, you can create a function that returns only the path part of a filename:

Public Shared Function GetPath(filename as string) as String
	return System.IO.Path.GetDirectoryName(filename)
End Function

You can then call that function as follows:

%=GetPath("c:\myfiles\somefile.txt")%

You can pass the value of a Job Variable to a function. Suppose you have a variable named "TargetFile" that is set to a file name. You can get the path of the current value of the variable using this function call:

%=GetPath(TargetFile)%

Function calls can also be nested within one another:

%=SomeFunction(GetPath("c:\myfiles\somefile.txt"))%

Inline Function Evaluation Language

Each inline function call is treated as a block of code that is evaluated in the same language that you have selected for the InlineFunctions library. That is, if the library uses VB.NET, your inline function call must conform to VB.NET language syntax. If the library uses C#, your function calls must use C# syntax.

For simple function calls this difference generally will not matter. However, watch out for details such as string literal syntax.

For example, the samples above all use VB.NET syntax. If your function library is in C#, though, this call will fail:

%=GetPath("c:\myfiles\somefile.txt")%

It will fail because of the unescaped backslashes in the string, which are not valid in C#. Instead you would need to use one of these formats for the string:

%=GetPath(@"c:\myfiles\somefile.txt")%

%=GetPath("c:\\myfiles\\somefile.txt")%

Advanced Inline Scripting

An inline script block is not limited to a single function call: you can include additional script code as well. (Remember that your code must use the language selected for the InlineFunctions library.) 

For example, you can combine the results of two functions to produce a single string (this example uses VB.NET) :

%=GetPath(TargetFile) & SomeFunction(GetDate().ToString("yyyy-MM-dd"))%

You can also call some .NET Framework classes directly. For example:

%=System.IO.Path.GetFileNameWithoutExtension(TargetFile)%

The following assemblies are supported for direct calls to .NET classes:

Variable Treatment

When you use a Job Variable within a script block, you can use a variable reference or a variable token.

To use a variable reference, you insert the name of the variable just as you would if the variable were defined in your code:

%=SomeFunction(TargetFile)%

To combine variables, or to combine variables with literals, you use the appropriate syntax for the chosen scripting language:

%=SomeFunction(TargetFile & " - " & FileTag)%

(Both "TargetFile" and "FileTag" are Job Variables defined in adTempus.)

You can instead use variable token expansion, just like you would do outside a script block:

%=SomeFunction("%TargetFile% - %FileTag%")%

When you use this approach, all variable tokens are expanded (replaced with their value) in a preprocessing step, before the script code is evaluated, and the value is inserted directly into the code. Therefore, you must take care that the result is still valid code. For example, if the "FileTag" Job Variable has a line break in it, the generated code would be invalid, and you would get a script compilation error.