Skip navigation links
Expressively and easily automate your text documents with TExpressive!
Expressively and easily automate your text documents with TExpressive!

Creating script fragments

When a TExpressive script becomes more complex, or when a script fragment is used more than once, you can split off parts of your script and reference them where necessary.  This can be done by putting the script fragment into an external template file and/or by making a function out of it.

When you place scripts in external template files, you can save these file just like you would save any other TExpressive file.  You can then in turn use the "Process" function to process these files; this way the script in the external file can be used by multiple other template files.  The possibilities within the external template file are identical to those of script fragments in the original template.  All parameters and custom functions are available in the external file, and the external file can modify parameters and custom functions itself; these modifications are available after the external file has been processed.  When you create an external template file however, you have no control over how it will be called (prerequisites like which parameters must be supplied).  If this is important, a better option is to make your own custom functions.

When making your own functions you can use the "MakeFunction" function.  This way you get to control what arguments must be supplied to your script fragment when it is called.  The source of your script will also be more readable when using custom functions.  All parameters and other custom functions that have been defined before calling a custom function are fully available within the custom function, unless one of it's arguments has the same name as a parameter - in that case the argument hides the parameter.  All changes to parameters and other custom functions are also preserved after the custom function has been called.  It is however not possible to have so-called "output" arguments (change arguments inside the custom function, after which the changes get propagated to the parameters that were used to supply the argument values when calling the custom function).

Of course it is also perfectly possible to define a custom function in an external template file; this way you get the best of both worlds.

If you want parts of your script to be processed without influencing the parameters and custom functions in the rest of the surrounding or calling script, you can place a "Gate" function around that script part.  This way you can control which parameter and custom function changes will be seen by the rest of the script.  This is especially useful when you write external template files that are used by lots of other templates; when you surround the content of the external file with a single "Gate" function, you get to control which parameter and function changes are allowed to be passed on to the calling script.  This way you e.g. cannot inadvertedly change the value of a parameter in the calling script because you happen to use the same parameter name internally in the external script.