4D v13.4

4D HTML Tags

Home

 
4D v13.4
4D HTML Tags

4D HTML Tags  


 

 

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 WEB SEND FILE and WEB SEND 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:

  • 4DTEXT, to insert 4D variables and expressions as text,
  • 4DHTML, to insert HTML code,
  • 4DSCRIPT, to execute a 4D method,
  • 4DINCLUDE, to include a page within another one,
  • 4DBASE, to modify the default folder used by the 4DINCLUDE tag,
  • 4DIF, 4DELSE, 4DELSEIF and 4DENDIF, to insert conditions in the HTML code,
  • 4DLOOP and 4DENDLOOP, to make loops in the HTML code.

Parsing of the contents of semi-dynamic pages sent by 4D takes place when WEB SEND FILE (.htm, .html, .shtm, .shtml), WEB SEND BLOB (text/html type BLOB) or WEB SEND TEXT commands are called, as well as when sending pages called using URLs. In this last case, 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 GET STATISTICS command.

Internally, the parser works with UTF-16 strings, but the data to parse may have been encoded differently. When tags contain text  (for example, 4DHTML), 4D converts the data when necessary depending on its origin and the information available (charset). Below are the cases where 4D parses the tags contained in the HTML pages, as well as any conversions carried out:

Action Content analysis of the sent pagesCharacter set used for parsing tags
Page extension (general case):
.htm, .html, .shtm, .shtml (HTML pages)X
.xml, .xsl (XML pages)X
.wml (WML pages)X
Pages called via URLsX, except for pages with “.htm” or “.html” extensionsUse of charset passed as parameter of the "Content-Type" header of the page. If there is none, search for a META-HTTP EQUIV tag with a charset. Otherwise, use of default character set for the HTTP server
WEB SEND FILE command callXUse of charset passed as parameter of the "Content-Type" header of the page. If there is none, search for a META-HTTP EQUIV tag with a charset. Otherwise, use of default character set for the HTTP server
WEB SEND TEXT command callXNo conversion necessary
WEB SEND BLOB command call X, if BLOB is of the “text/html” typeUse of charset set in the "Content-Type" header of the response. Otherwise, use of default character set for the HTTP server
Inclusion by the <!--#4DINCLUDE--> tagXUse of charset passed as parameter of the "Content-Type" header of the page. If there is none, search for a META-HTTP EQUIV tag with a charset. Otherwise, use of default character set for the HTTP server
PROCESS 4D TAGS command callXText data: no conversion. BLOB data: automatic conversion from the Mac-Roman character set for compatibility

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: <!--#4DTEXT [TABLE]ValNum--></B><BR>
      (Field display)
<!--#4DENDIF-->
<!--#4DENDLOOP-->   (End for)
</BODY>
</HTML>

4DTEXT  

Syntax: <!--#4DTEXT VarName--> or <!--#4DTEXT 4DExpression-->

The tag <!--#4DTEXT 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 <!--#4DTEXT vtSiteName-->!</P>

The value of the 4D variable vtSiteName will be inserted in the HTML page when it is sent. This value is inserted as simple text.

You can also insert 4D expressions using the 4DTEXT tag. You can for example directly insert the contents of a field (<!--#4DTEXT [tableName]fieldName-->), an array element (<!--#4DTEXT tabarr{1}-->) or a method returning a value (<!--#4DTEXT 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 4DTEXT depends on the value of the “Available through 4D HTML tags and URLs (4DACTION...)” 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, <!--#4DTEXT 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 “<!--#4DTEXT myvar--> : ## error # error code”.

Notes:

  • You work with process variables.
  • You can display a picture field content. However, 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. You just need to generate the BLOB in Text without length mode.

Compatibility note: This more specialized tag replaces the generic 4DVAR tag starting with version 12.2 of 4D. The 4DVAR tag is still supported and works as in previous versions. However, it is now recommended to use the 4DTEXT tag to insert text.

4DHTML  

Syntax: <!--#4DHTML VarName--> or <!--#4DHTML 4DExpression-->

This tag lets you assess a variable or 4D expression and insert it into a page as an HTML expression. Unlike the 4DTEXT tag, this tag converts the characters to HTML code.

For example, here are the insertion results of the 4D text variable myvar with the available tags:

myvar ValueTagsWeb Page Insertion
myvar:="<B>"<!--#4DTEXT myvar-->&lt;B&gt;
myvar:="<B>"<!--#4DHTML myvar--><B>

In case of an interpretation error, the inserted text will be “<!--#4DHTML myvar--> : ## error # error code”.

Note: Executing a 4D method with 4DHTML depends on the value of the “Available through 4D HTML tags and URLs (4DACTION...)” attribute set in the Method properties. For more information about this, refer to the Connection Security section.

Compatibility note: This tag replaces the 4DHTMLVAR tag starting with version 12.2 of 4D. The 4DHTMLVAR tag is still supported but for better code readability, it is now recommended to use the 4DHTML tag for inserting HTML code.

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 4D HTML tags and URLs (4DACTION...)” 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 WEB SEND FILE (.htm, .html, .shtm, .shtml) or WEB SEND 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).

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/10”); the expression “Today is <!--#4DSCRIPT/MYMETH/MYPARAM––>” therefore becomes “Today is 12/31/10”.

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.

Syntax: <!--#4DINCLUDE Path-->

This comment allows another HTML page (indicated by the path parameter) to be included in an HTML page. By default, only the body of the HTML page that is specified is included, in other words, the contents found within the <body> and </body> tags (the tags themselves are not included). This lets you avoid conflicts related to meta tags present in the headers. However, if the HTML page specified does not contain <body></body> tags, the entire page is included. It is up to you to verify the consistency of the meta tags.
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 file name extension, 4D analyzes 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 WEB SEND 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).

Notes:

  • When you use the 4DINCLUDE tag with the PROCESS 4D TAGS command, the default folder is the folder containing the database structure file.
  • You can modify the default folder used by the 4DINCLUDE tag in the current page, using the <!--#4DBASE --> tag (see below).

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".

Examples

<!--#4DINCLUDE subpage.html-->
<!--#4DINCLUDE folder/subpage.html-->
<!--#4DINCLUDE ../folder/subpage.html-->

4DBASE  

Syntax: <!--#4DBASE folderPath-->

The <!--#4DBASE --> tag designates a working directory that is used by the <!--#4DINCLUDE--> tag.
When it is called in a Web page, the <!--#4DBASE --> tag modifies all subsequent <!--#4DINCLUDE--> calls on this page, until the next  <!--#4DBASE -->, if any. If the <!--#4DBASE --> folder is modified from within an included file, it retrieves its original value from the parent file. 

The folderPath parameter must contain a pathname relative to the current page and it must end with a slash (/). The designated folder must be located inside the Web folder.
Pass the WEBFOLDER keyword to restore the default path (relative to the page). 

Thus the following code (4D v12), which must specify a relative path for each call:

<!--#4DINCLUDE subpage.html--> 
<!--#4DINCLUDE folder/subpage1.html-->
<!--#4DINCLUDE folder/subpage2.html-->
<!--#4DINCLUDE folder/subpage3.html-->
<!--#4DINCLUDE ../folder/subpage.html-->

... can be rewritten using the <!--#4DBASE --> tag:

<!--#4DINCLUDE subpage.html--> 
<!--#4DBASE folder/-->
<!--#4DINCLUDE subpage1.html-->
<!--#4DINCLUDE subpage2.html-->
<!--#4DINCLUDE subpage3.html-->
<!--#4DBASE ../folder/-->
<!--#4DINCLUDE subpage.html-->
<!--#4DBASE WEBFOLDER-->

Example  

Setting a directory for the home page using the <!--#4DBASE --> tag:

/* Index.html */
<!--#4DIF LangFR=True-->
    <!--#4DBASE FR/-->
<!--#4DELSE-->
    <!--#4DBASE US/-->
<!--#4DENDIF-->
<!--#4DINCLUDE head.html-->
<!--#4DINCLUDE body.html-->
<!--#4DINCLUDE footer.html-->

In the head.html file, the current folder is modified through <!--#4DBASE -->, without this changing its value in Index.html:

/* Head.htm */
/* the working directory here is relative to the included file (FR/ or US/) */
<!--#4DBASE Styles/-->
<!--#4DINCLUDE main.css-->
<!--#4DINCLUDE product.css-->
<!--#4DBASE Scripts/-->
<!--#4DINCLUDE main.js-->
<!--#4DINCLUDE product.js-->

Syntax: <!--#4DIF expression--> {<!--#4DELSEIF expression2-->...<!--#4DELSEIF expressionN-->} {<!--#4DELSE-->} <!--#4DENDIF-->

Used with the <!--#4DELSEIF--> (optional), <!--#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-->.

Using the <!--#4DELSEIF--> tag, you can test an unlimited number of conditions. Only the code that follows the first condition evaluated as True is executed. If no conditions are true, no statement is executed (if there is no final <!--#4DELSE-->).
You can use a <!--#4DELSE--> tag after the last <!--#4DELSEIF-->. If all the conditions are false, the statements following the <!--#4DELSE--> are executed.

The two following codes are equivalent.

  • Code using 4DELSE only:
    <!--#4DIF Condition1-->
      /* Condition1 is true*/
    <!--#4DELSE-->
        <!--#4DIF Condition2-->
            /* Condition2 is true*/
        <!--#4DELSE-->
            <!--#4DIF Condition3-->
                /* Condition3 is true */
            <!--#4DELSE-->
                /*None of the conditions are true*/
            <!--#4DENDIF-->
        <!--#4DENDIF-->
    <!--#4DENDIF-->
  • Similar code using the 4DELSEIF tag:
    <!--#4DIF Condition1-->
         /* Condition1 is true*/
    <!--#4DELSEIF Condition2-->
         /* Condition2 is true*/
    <!--#4DELSEIF Condition3-->
        /* Condition3 is true */
    <!--#4DELSE-->
        /* None of the conditions are true*/
    <!--#4DENDIF-->

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 <!--#4DTEXT vname-->.
<!--#4DELSE-->
No name has been found.
<!--#4DENDIF-->
...
</BODY>

This example inserts different pages depending on which user is connected:

<!--#4DIF LoggedIn=False-->
    <!--#4DINCLUDE Login.htm -->
<!--#4DELSEIF User="Admin" -->
    <!--#4DINCLUDE AdminPanel.htm -->
<!--#4DELSEIF User="Manager" -->
    <!--#4DINCLUDE SalesDashboard.htm -->
<!--#4DELSE-->
    <!--#4DINCLUDE ItemList.htm -->
<!--#4DENDIF-->

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]-->
<!--#4DTEXT [People]Name--> <!--#4DTEXT [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-->
<!--#4DTEXT 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-->
<!--#4DTEXT 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 -->).

 
PROPERTIES 

Product: 4D
Theme: Web Server

 
SEE ALSO 

Binding 4D objects with HTML objects
PROCESS 4D TAGS
URLs and Form Actions

 
TAGS 

4DVAR, 4DTEXT, 4DHTMLVAR, 4DHTML, 4DSCRIPT, 4DINCLUDE, 4DIF, 4DLOOP, 4DELSEIF***