4D v14.34D HTML Tags |
|||||||||||||||||||||||||||||||||||||||
|
4D v14.3
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:
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). 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:
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> 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, special HTML characters such as ">" are automatically escaped. 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:
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. 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 does not escape HTML special characters. For example, here are the insertion results of the 4D text variable myvar with the available tags:
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. 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 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. 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:
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. 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--> Syntax: <!--#4DBASE folderPath--> The <!--#4DBASE --> tag designates a working directory that is used by the <!--#4DINCLUDE--> tag. 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. 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--> 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--> 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-->). The two following codes are equivalent.
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--> The <!--#4DLOOP condition--> ... <!--#4DENDLOOP--> blocks can be nested. Like in 4D, each <!--#4DLOOP condition--> should match a <!--#4DENDLOOP-->. There are three kinds of conditions:
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])
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))
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) The my_method method can be as follow: C_LONGINT($1) 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:
|
PROPERTIES
Product: 4D SEE ALSO
Binding 4D objects with HTML objects TAGS 4DVAR, 4DTEXT, 4DHTMLVAR, 4DHTML, 4DSCRIPT, 4DINCLUDE, 4DIF, 4DLOOP, 4DELSEIF ARTICLE USAGE
4D Language Reference ( 4D v14 R2) Inherited from :
4D HTML Tags ( 4D v12.4) |
|||||||||||||||||||||||||||||||||||||