Using User Code To provide full code generation flexibility. It is allowed to override an existing code generating function or create your own custom code generating function to suit your needs. To do this you need to write your VBScript functions and place them in the User Code File for the changes to take effect. By default, the User Code File is named as "usercode.vbs" and can be found under the "src" subfolder of the installed directory. However, you can rename it or put it elsewhere if necessary. In that case, you need to modify the following registry key: HKEY_CURRENT_USER\Software\<product name>\<version>\Settings\General
I. User Code Function Names In the User Code File, you can override virtually all exposed functions. To override an existing function, you must write your own function in the user code. The function names for overriding functions begin with USER_. The User Code function name is:
Example You can refer to the System Functions list for functions that you can override.
II. Overriding System Function Although there are many system functions, in real applications you usually only need to customize a few of them, for example, Script_View - This function generates code to render the fields in list, view and delete pages. Of course, sometimes you may need to customize other system functions also. In general, you compare the generated codes with the corresponding page in the template, find out which tag generate the codes you want to customize. Then you can add the corresponding user function in the User Code File. The function is always of the same syntax, only the function name is different:
The function has only one argument - str, it is the original code generated by the code generator. You can manipulate this string to generate codes as you want. In most case, you just find and replace part of the code, you only need to use the most basic VBScript string functions: InStr - Find the start (and end) position of the code to be modified. Syntax: InStr([start, ]str, find[, compare]) Follow the following steps to override a system function. For example, if you want to override the function FieldView, Step 1 - View the original code first, make sure the codes you want to replace is indeed generated by the function. Example Function USER_FieldView(str)
Step 2 - Generate scripts, view the generated code, copy the code you want to replace, modify it as you need. Step 3 - Replace it. Example 1 - directly replace part of the code Function USER_FieldView(str)
Example 2 - find the part of code to be replaced and then modify and replace it Function USER_FieldView(str)
III. Creating your own Template Tag You are not bound by overriding existing system functions, you can create your own Template Tags. More accurately, you write your own code-generating functions, put them in the User Code File so it can be called by Template Tags in any of your templates. Syntax:
This is same as a normal VBScript function. "arglist" is optional, it is a list of comma-separated variables representing arguments that are passed to the function when it is called. To return your code from the function, assign it to the function name. Any number of such assignments can appear anywhere within the function. Remember to return your code, if no value is assigned to the function name, the function returns a default value: a zero-length string (""). That is, no codes will be generated. To call the function, use one of the following syntax: Function Block Function block output an object property as string or output a string return by a function. Syntax:
or Function Block with Indentation Syntax:
Example You can create a user function to check if a field type is numeric, and then use this function in other user function ' Function to check if field
type is numeric ' Function to override Script
IV. User functions used in Extensions The user functions used in Extensions must take the following form: It is a user class of class name <Ext> which should be the same as the extension name. It must also contain two methods "FieldEdit_Prefix" and "FieldEdit_Suffix". The "FieldEdit_Prefix" method returns a prefix value which is appended to the start of the output from the "FieldEdit" system function. Similarly, the "FieldEdit_Suffix" method returns a suffix value which is appended to the end of the output from the "FieldEdit" system function. Both methods take on a parameter "ctl" which contains the name of the field control to be created. (See usercode.vbs in the "jscalendar" and "fckeditor" extensions as examples.)
Also See: Customizing Template
|
||||||||||
©2004-2011 e.World Technology Ltd. All rights reserved. |