version 2004.2 (Modified)
The 4D Web server provides you with different HTML tags specific to 4D, which allow you to insert references to 4D variables or expressions, or different types of processing, in static HTML pages sent by the Web server, for example using the SEND HTML FILE and SEND HTML BLOB commands. These pages are called semi-dynamic pages.
These tags are inserted as HTML comments (<!--#Tag Contents--> in HTML code). Most HTML editors offer editing facilities to insert comments.
The following 4D HTML tags are available:
4DVAR, to insert 4D variables and expressions,
4DHTMLVAR, similar to 4DVAR but inserting HTML code,
4DSCRIPT, to execute a 4D method,
4DINCLUDE, to include a page within another one,
4DIF, 4DELSE and 4DENDIF, to insert conditions in the HTML code,
4DLOOP and 4DENDLOOP, to make loops in the HTML code.
Compatibility Note: In version 6.0.x of 4D, the notation to use for inserting 4D variables in static pages was square brackets [VarName]. In a converted database, to be able to use the standard HTML syntax (<!--#4DVAR VarName-->), make sure that the option "Use 4DVAR Comments instead of Brackets" in the Preferences dialog box is checked (see section Web Server Settings).
About 4D HTML Tags
Parsing of the contents of semi-dynamic pages sent by 4D takes place when SEND HTML FILE (.htm, .html, .shtm, .shtml) or SEND HTML BLOB (text/html type BLOB) commands are called, as well as when sending pages called using URLs.
However, in non-contextual mode, for reasons of optimization, pages that are suffixed with ".htm" and ".html" are NOT parsed. In order to "force" the parsing of HTML pages in this case, you must add the suffix ".shtm" or ".shtml" (for example, http://www.server.com/dir/page.shtm).
An example of the use of this type of page is given in the description of the WEB CACHE STATISTICS command.
Below are the cases where 4D parses the tags contained in the HTML pages sent to the Web browsers:
Sending Conditions | Content analysis of the sent pages | |
Contextual mode | Non-contextual mode | |
Page extension (general case): | ||
.htm, .html, .shtm, .shtml (HTML pages) | X | X |
.xml, .xsl (XML pages) | X | X |
.wml (WML pages) | X | X |
Pages called via URLs | X | X, except for pages with ".htm" |
or ".html" extensions | ||
SEND HTML FILE command call | X | X |
SEND HTML BLOB command call (if the | X | X |
BLOB is "text/html" type) | ||
Inclusion by the <!--4DINCLUDE --> tag | X | X |
Inclusion by the {mypage.htm} tag | X | - |
In order to be processed by 4D, an HTML comment should have the following format <!--#4D...-->. Please note that some HTML editors automatically add other information within the comment; this can lead to some misinterpretation.
However, other HTML comments such as <!--Beginning of list--> are possible.
If a comment <!--#4D... does not end by -->, the following message "<!--#4D... : --> expected" will be inserted and the analysis will stop at this level (the page will be sent to indicate the error).
It is possible to mix several types of comments. For example, the following HTML syntax is possible:
<HTML> ... <BODY> <!--#4DSCRIPT/PRE_PROCESS--> (Method call) <!--#4DIF (myvar=1)--> (If condition) <!--#4DINCLUDE banner1.html--> (Subpage insertion) <!--#4DENDIF--> (End if) <!--#4DIF (myvar=2)--> <!--#4DINCLUDE banner2.html--> <!--#4DENDIF--> <!--#4DLOOP [TABLE]--> (loop on the current selection) <!--#4DIF ([TABLE]ValNum>10)--> (If [TABLE]ValNum>10) <!--#4DINCLUDE subpage.html--> (subpage insertion) <!--#4DELSE--> (Else) <B>Value: <!--#4DVAR [TABLE]ValNum--></B><BR> (Field display) <!--#4DENDIF--> <!--#4DENDLOOP--> (End for) </BODY> </HTML>
4DVAR
Syntax: <!--#4DVAR VarName--> or <!--#4DVAR 4DExpression-->
The tag <!--#4DVAR VarName--> allows you to insert a reference to the 4D variable or expression VarName anywhere in an HTML page. For example, if you write:
<P>Welcome to <!--#4DVAR vtSiteName-->!</P>
The value of the 4D variable vtSiteName will be inserted in the HTML page.
You can insert a 4D text variable in HTML code, provided its first character is code 1 (i.e., vtHTML:=Char(1)+"...HTML code..."). You can also use the tag 4DHTMLVAR.
You can also insert 4D expressions (not only variables) in 4D HTML comments with the 4DVAR tag. You can directly insert a field content (for example <!--#4DVAR [tableName]fieldName-->) or an item array content (for example <!--#4DVAR arr{1}-->) or a method returning a value (<!--#4DVAR mymethod-->).
The expression conversion follows the same rules as the variable ones. Moreover, the expression must comply with 4D syntax rules.
Note: Executing a 4D method with 4DVAR depends on the value of the "Available through 4DACTION, 4DMETHOD and 4DSCRIPT" attribute set in the Method properties. For more information about this, refer to the Connection Security section.
Although an expression can contain direct calls to 4D functions, this is not recommended for localization issues. For example, <!--#4DVAR Current date-->, although correctly interpreted with a 4D in English will not be understood by an French version. The same applies to real numbers (the decimal separator can be different according to the language). In both cases, we strongly advise you to assign a variable through programming.
In case of interpretation error, the inserted text will appear as "<!--#4DVAR myvar--> : ## error # error code".
Notes:
You work with process variables.
You can display a picture field content. In addition (in contextual mode only), you can also display a picture variable content. In both modes, it is not possible to display the content of a picture array item.
Since HTML is a word processing oriented application, you will usually work with Text variables. However, you can also use BLOB variables (which avoid the 32 000 characters limitation of text type variables). You just need to generate the BLOB in Text without length mode.
Examples of 4DVAR uses are given in the section Binding 4D objects with HTML objects.
4DHTMLVAR
Syntax: <!--#4DHTMLVAR VarName--> or <!--#4DHTMLVAR 4DExpression-->
This tag allows assessing a variable or a 4D expression and inserting it in a page as an HTML expression. In fact, this tag works exactly the same way as the <!--#4DVAR VarName--> tag when VarName starts with the code 1 (see above).
For example, here are the insertion results of the 4D text variable myvar with the available tags:
myvar Value | Tags | Web Page Insertion |
myvar:="<B>" | <!--#4DVAR myvar--> | <B> |
myvar:=Char(1)+"<B>" | <!--#4DVAR myvar--> | <B> |
myvar:="<B>" | <!--#4DHTMLVAR myvar--> | <B> |
In case of an interpretation error, the inserted text will be "<!--#4DHTMLVAR myvar--> : ## error # error code".
Note: Executing a 4D method with 4DHTMLVAR depends on the value of the "Available through 4DACTION, 4DMETHOD and 4DSCRIPT" attribute set in the Method properties. For more information about this, refer to the Connection Security section.
Note: The text variable should be expressed using the ISO Latin-1 character map (for more information, refer to the Mac to ISO command description).
4DSCRIPT/
Syntax: <!--#4DSCRIPT/MethodName/MyParam-->
The 4DSCRIPT tag allows you to execute 4D methods when sending static HTML pages. The presence of the <!--#4DSCRIPT/MyMethod/MyParam--> tag in a static page as an HTML comment forces the execution of the MyMethod method with the MyParam parameter as a string in $1. When loading the home page, 4D calls the On Web Authentication Database Method (if it exists). If it returns True, 4D executes the method.
The method returns text in $0. If the string starts with the code character 1, it is considered as HTML (the same principle is true for the variables).
Note: The execution of a method with 4DSCRIPT depends on the value of the "Available through 4DACTION, 4DMETHOD and 4DSCRIPT" attribute defined in the Method properties. For more information about this, refer to the Connection Security section.
The analysis of the contents of the page is done when either SEND HTML FILE (.htm, .html, .shtm, .shtml) or SEND HTML BLOB (blob of type text/html) is called.
Remember that in non-contextual mode, the analysis is also done when a URL points to a file that has either the ".shtm" or ".shtml" extension (for example http://www.server.com/dir/page.shtm).
Note: In contextual mode, the method is executed in the context.
For example, let's say that you insert the following comment "Today is <!--#4DSCRIPT/MYMETH/MYPARAM-->" into a static page. When loading the page, 4D calls the On Web Authentication Database Method (if it exists), then calls the MYMETH method and passes the string "/MYPARAM" as the parameter $1.
The method returns text in $0 (for example "12/31/03"); the expression "Today is <!--#4DSCRIPT/MYMETH/MYPARAM>" therefore becomes "Today is 12/31/03".
The MYMETH method is as follows:
C_TEXT($0) `This parameter must always be declared C_TEXT($1) `This parameter must always be declared $0:=String(Current date)
Warning: You must always declare the $0 and $1 parameters in the called method.
Note: A method called by 4DSCRIPT must not call interface elements (DIALOG, ALERT...).
As 4D executes methods in their order of appearance, it is absolutely possible to call a method that sets the value of many variables that are referenced further in the document, whichever mode you are using.
Note: You can insert as many <!--#4DSCRIPT...--> comments as you want in a static page.
Compatibility note: In 4D previous versions, the same tag, 4DACTION, could be used as a URL (for example, http://myserver/4DACTION/meth), or as an HTML comment in a static page (<!--#4DACTION/meth-->). As this could be misleading, starting with 4D version 6.7 the 4DSCRIPT tag replaces the 4DACTION tag. It is used only as an HTML comment (<!--#4DSCRIPT/meth-->) and has the same effect as <!--#4DACTION/meth-->. 4DACTION is now just used for URLs.
4DINCLUDE
Syntax: <!--#4DINCLUDE Path-->
This comment allows the body of another HTML page (indicated by the path parameter) to be included in an HTML page. An HTML page body is included within the <BODY> and </BODY> tags (the tags themselves are not included).
The <!--#4DINCLUDE --> comment is very useful for tests (<!--#4DIF-->) or loops (<!--#4DLOOP-->). It is very convenient to include tags according to a criteria or randomly.
When including, regardless of the mode and file name extension, 4D analyses the called page and then inserts the contents (modified or not) in the page originating the 4DINCLUDE call.
An included page with the <!--#4DINCLUDE --> comment is loaded in the Web server cache the same way as pages called via a URL or sent with the SEND HTML FILE command.
In path, put the path leading to the document to include. Warning: In the case of a 4DINCLUDE call, the path is relative to the document being analyzed, that is, the "parent" document. Use the slash character (/) as a folder separator and the two dots (..) to go up one level (HTML syntax).
The number of <!--#4DINCLUDE path--> within a page is unlimited. However, the <!--#4DINCLUDE path--> calls can be made only at one level. This means that, for example, you cannot insert <!--#4DINCLUDE mydoc3.html--> in the mydoc2.html body page, which is called by <!--#4DINCLUDE mydoc2--> inserted in mydoc1.html.
Furthermore, 4D verifies that inclusions are not recursive.
In case of error, the inserted text is "<!--#4DINCLUDE path--> :The document cannot be opened".
Note: In contextual mode, if a page is inserted in a form via a tag {mypage.html} inserted in a static text area, the 4DINCLUDE comments (if any) will be ignored.
Examples
<!--#4DINCLUDE subpage.html--> <!--#4DINCLUDE folder/subpage.html--> <!--#4DINCLUDE ../folder/subpage.html-->
4DIF, 4DELSE and 4DENDIF
Syntax: <!--#4DIF expression--> <!--#4DELSE--> <!--#4DENDIF-->
Used with the <!--#4DELSE--> (optional) and <!--#4DENDIF--> comments, the <!--#4DIF expression--> comment offers the possibility to execute HTML code conditionally.
The expression parameter can contain any valid 4D expression returning a Boolean value. It must be indicated within parenthesis and comply with the 4D syntax rules.
The <!--#4DIF expression--> ... <!--#4DENDIF--> blocks can be nested in several levels. Like in 4D, each <!--#4DIF expression--> should match a <!--#4DENDIF-->.
In case of an interpretation error, the text "<!--#4DIF expression-->: A boolean expression was expected" is inserted instead of the contents located between <!--#4DIF --> and <!--#4DENDIF-->.
Likewise, if there are not as many <!--#4DENDIF--> as <!--#4DIF -->, the text "<!--#4DIF expression-->: 4DENDIF expected" is inserted instead of the contents located between <!--#4DIF --> and <!--#4DENDIF-->.
Example
This example of code inserted in a static HTML page displays a different label according the vname#"" expression result:
<BODY> ... <!--#4DIF (vname#"")--> Names starting with <!--#4DVAR vname-->. <!--#4DELSE--> No name has been found. <!--#4DENDIF--> ... </BODY>
4DLOOP and 4DENDLOOP
Syntax: <!--#4DLOOP condition--> <!--#4DENDLOOP-->
This comment allows repetition of a portion of HTML code as long as the condition is fulfilled. The portion is delimited by <!--#4DLOOP--> and <!--#4DENDLOOP-->.
The <!--#4DLOOP condition--> ... <!--#4DENDLOOP--> blocks can be nested. Like in 4D, each <!--#4DLOOP condition--> should match a <!--#4DENDLOOP-->.
There are three kinds of conditions:
<!--#4DLOOP [table]-->
This syntax makes a loop for each record from the table current selection in the current process. The HTML code portion located between the two comments is repeated for each current selection record.
Note: When the 4DLOOP tag is used with a table, records are loaded in Read only mode.
The following HTML code:
<!--#4DLOOP [People]--> <!--#4DVAR [People]Name--> <!--#4DVAR [People]Surname--><BR> <!--#4DENDLOOP-->
... could be expressed in 4D language in the following way:
FIRST RECORD([People]) While(Not(End selection([People]))) ... NEXT RECORD([People]) End while
<!--#4DLOOP array-->
This syntax makes a loop for each item array. The array current item is increased when each HTML code portion is repeated.
Note: This syntax cannot be used with two dimension arrays. In this case, it is better to combine a method with nested loops.
The following HTML code example:
<!--#4DLOOP arr_names--> <!--#4DVAR arr_names{arr_names}--><BR> <!--#4DENDLOOP-->
... could be expressed in 4D language in the following way:
For ($Elem;1;Size of array(arr_names)) arr_names:=$Elem ... End for
<!--#4DLOOP method-->
This syntax makes a loop as long as the method returns True. The method takes a Long Integer parameter type. First it is called with the value 0 to allow an initialization stage (if necessary); it is then called with the values 1,then 2, then 3 and so on, as long as it returns True.
For security reasons, the On Web Authentication Database Method can be called once just before the initialization stage (method execution with 0 as parameter). If the authentication is OK, the initialization stage will proceed.
Warning: C_BOOLEAN($0) and C_LONGINT($1) MUST be declared within the method for compilation purposes.
The following HTML code example:
<!--#4DLOOP my_method--> <!--#4DVAR var--> <BR> <!--#4DENDLOOP-->
... could be expressed in 4D language in the following way:
If(AuthenticationWebOK) If(my_method(0)) $counter:=1 While(my_method($counter)) ... $counter:=$counter+1 End while End if End if
The my_method method can be as follow:
C_LONGINT($1) C_BOOLEAN($0) If($1=0) `Initialisation $0:=True Else If($1<50) ... var:= ... $0:=True Else $0:=False `Stops the loop End if End if
In case of an interpretation error, the text "<!--#4DLOOP expression-->: description" is inserted instead of the contents located between <!--#4DLOOP --> and <!--#4DENDLOOP-->.
The following messages can be displayed:
- Unexpected expression type (standard error);
- Incorrect table name (error on the table name);
- An array was expected (the variable is not an array or is a two dimension array);
- The method does not exist;
- Syntax error (when the method is executing);
- Access error (you do not have the appropriate access privileges to access the table or the method).
- 4DENDLOOP expected (the <!--#4DENDLOOP--> number does not match the <!--#4DLOOP -->).
See Also
Binding 4D objects with HTML objects, URLs and Form Actions, Using the Contextual Mode.