ExperShop - DynHtmlHome Contents Please email any bug reports, comments or suggestions to ExperLog's Online Support OverviewDynHtml is a technology dedicated to include database and other dynamic content in HTML pages.DynHtml pages are expanded on-the-fly by ExperSHOP, and database query results are included in the HTML text (as well as other possible dynamic information, such as HTTP parameters for example). DynHtml templates are ascii files that mix HTML with specific DynHtml tags: they are expanded at run time, to display dynamic information.
How to write a DynHtml templateHere's an example of a DynHtml template:
<html><body> <b>Customer List</b><p> $DefineSql GetCustomers SELECT FirstName, LastName FROM Customer $IfPresent AgeMin $AppendSql GetCustomers WHERE Age>AgeMin $Endif $ExecSql GetCustomers $IfNoResult GetCustomers There's no Customer. $Else $LoopOnResults GetCustomers cust Customer Name: $cust:FirstName$, $cust:LastName$<br> $EndLoop $Endif </body></html> The values of FirstName and LastName returned in the query results will
appear in the browser in place of $cust:FirstName$ and $cust:LastName$; Customer List
Customer Name: Paul, Smith Note the "$IfPresent... $Endif" statement: if the DynHtml template is invoked with an HTTP parameter named "AgeMin", a WHERE clause is appended to the SQL query.
How to expand a DynHtml pageDynHtml pages are expanded by the ExperSHOP servlets: the user has nothing to do to expand the pages.
DynHtml statements$# $Action $Add $Append $AppendSql $AppendSql2 $Assign $Case $CheckNumber $ClearError $Cookie $Debug $Default $Defvar $Div $Else (see If... statements) $EndCase $Endif (see If... statements) $EndLoop (see LoopOnEnum, LoopOnFiles, LoopOnResults) $EndSwitch $ExecSql $Exit $ExitLoop $ExpandFile $ExpandSql $Forward $FetchProduct $If $IfEe $IfEqual $IfError $IfFilePresent $IfGe $IfGt $IfLe $IfLt $IfNoResult $IfPresent $Include $Invoke $LoopOnEnum $LoopOnFiles $LoopOnResults $Mul $NewObject $SetCookie $Sub $Switch $System
Some formatting information can be specified, for example to format
numeric values: The format should be enclosed in parentheses, just after
the "$" character that delimits the variable (for example,
Numeric formats:
Any format supported by the java.text.DecimalFormat class. $(.00)Price$ $(##.##)TotalPrice$ Note that numeric formats can have unspecified effects when rounding numbers to integer values: particularly, (0) or (00) when applied to something else than integers.
If this is what you want to do, first round the values by using (Round),
(Ceil) or (Floor) formats.
String.sub - Substring. For example, $(String.sub5,8)astring$ will
return characters between the 5th position (included) and the 8th position
(excluded), and $(String.sub5)astring$ will return all the characters
after the 5th position (included). Note that the 1st position is 0,
not 1.
Date formats: Other formats:
Upper - Convert to upper case (example:
DynHtml predefined variables$COOKIE$
The ExperShop session information, to be included in ExperSHOP URLs
(in HTTP links, using the <A HREF=...> tag). See also: $Cookie statement, for URLs invoked as HTML form actions. $\$
Displays a "$" character.
Data sets and data objectsA data object is equivalent to a database tuple, with attributes equivalent to database columns.
A data set is a set of data objects: for example, the result of a
database query, or the shopping cart seen as a set of items. There are two kinds of data sets: the expershop built-in data sets (like the Shopping Cart or the Shop's configuration file), and the run-time data sets (transient data sets, like database query results or string enumerations).
For example, ExperShop provides a built-in data object called
The Shopping Cart is also a data set: you can loop on cart items,
then display each item's characteristics (each item being a data object).
Of course, SQL queries are data sets, as soon as they have been
executed:
A parameter is a variable that will be considered an HTTP parameter in
the current page: it will be passed to actions if $Action is used.
(note: parameters won't be transmitted furter if the current page is
submitted as a form.
To append text to a previously defined variable or parameter, use the
A variable may be computed : $DefVar day (24 * 60 * 60) EnumerationsAn enumeration is a list of values separated by commas, or a continuous series of integers, or both; for example, you can define enumerations like this:$DefVar months Jan,Feb,Mar,Apr,May,Jun,Jul,Sep,Oct,Nov,Dec
$DefVar weekdays 1->7
The first one lists the months, the 2nd is equivalent to 1,2,3,4,5,6,7. It is valid to combine the 2 constructs: for example, $DefVar intervals 1->5,10->20
It is possible to loop on enumeration values, using the
$LoopOnEnum months mm Current Month is: $mm:value$ $EndLoop $LoopOnEnum weekdays d Current day is: $d:value$ $EndLoopIn fact, an enumeration value can be considered as a data set, with tuples that contain only one column, called "value" (the reason for mm:value in the loop above).
It is possible to handle runtime errors using the
Example (most HTML forms should begin as follows in a DynHtml template):
<FORM METHOD=POST ACTION="com.expershop.lite.ExperSHOP"> $Cookie
Note that ExperShop does not use REAL cookies: in this document, "cookie" refers to session information stored on the server and associated to a session id. It is possible to store variables in the "cookie" information:
Define SQL request $DefineSql requestName sql-statement Append text to SQL request $AppendSql requestName text $AppendSql2 requestName text$AppendSql and $AppendSql2 can be used to append text to an SQL request. $AppendSql2 immediately evaluates variables that can be resolved (expands the query string), $AppendSql does not (so there may be variables in the request, that will be resolved by $ExecSql). $AppendSql can be used in conjunction with "If... Else" statements, to complete an SQL request text. Note: If text begins with "AND" or "OR", the AND or OR string will be removed if text is the first condition of a WHERE clause. For example, appending "AND Price>10" to a query equal to "SELECT * FROM Items WHERE" will result in "SELECT * FROM Items WHERE Price>10". Then, appending "AND Price<100" will result in "SELECT * FROM Items WHERE Price>10 AND Price<100".
Execute SQL $ExecSql requestName [baseName]Evaluates all variables in the request string, then executes it. The name of the database is not required when it is the database defined in the configuration file of the application, else, the database must be defined by a datasource in the configuration file of the application.
$LoopOnResults requestName tupleName [i j] ... $EndLoopIt is possible to add to the tuple name, the boundaries of the request, to loop from i to j. Attention, this option does not guaranty the order of the elements coming from the database. exemple : $LoopOnResults MyREQ myTuple 11 20 ... $EndLoop Display a column value (in a loop)
Handle no result case $IfNoResult requestName ... $Else ... $Endif Handle error $IfError ... $Else ... $Endif
Retrieve an SQL request into a variable $ExpandSql varname requestName ActionsYou can execute an ExperSHOP Action from a DynHtml template, using the$Action statement (to learn more about ExperSHOP Actions,
see the DynHtml actions web page).
Syntax: Example: empty the shopping cart $Action com.expershop.actions.ESEmptyCart $IfPresent varname ... $Else ... $EndifExamples: $IfPresent FirstName $IfPresent customer:FirstName (in a loop)
$IfPresent filename ... $Else ... $EndifExamples: $IfFilePresent localFile.txt (in the current directory)$IfFilePresent /tmp/afile.txt (absolute path)$IfFilePresent f:FileName (in a loop)
$LoopOnFiles directory file Name: $file:name$, Extension: $file:ext$, Date: $(DateFormat.dd/MM/yyyy)file:lastmodified$, IsFile: $file:isfile$ <br> ... $EndLoopThe directory may be relative to ESRootDir, or absolute (for example, if it starts with / on a Unix system). The "isfile" field is set to 1 for normal files, 0 for others (directories, symlinks...)
$DefVar x 2.3 $CheckNumber $x$ $IfError ERROR: $x$ is not a number<br> $Else Ok<br> $Endif $ClearError
$DefVar a 1 $Add a 1 $DefVar b 2 $Div a $b$In this example, a=1, then we add 1 (a=2), then divide by the value of b (at the end, a=1).
$IfEqual varname value ... $Else ... $EndifExamples: $IfEqual City New-York $IfEqual FirstName "" $IfEqual FirstName $Name: $ $IfEqual customer:FirstName John (in a loop)Number comparisons
$IfEe varname 0 Zero! $Endif $IfLe varname 10 $varname$ <= 10 $Endif $IfLt varname $anumber$ $varname$ < $anumber$ $Endif $IfGe varname 10 $varname$ >= 10 $Endif $IfGt varname $anumber$ $varname$ > $anumber$ $Endif
$If ( expression operator expression) ... $Else ... $Endif"expression" can be : ($var1$ + 14)*3 + $var2$ "operator" can be a comparison operator, or a logical operator. Comparison operators are: < > <= >= = <> !=Logical operators are: && (logical and) || (logical or) ! (logical not)Parentheses can be used to group expressions. For example, the following construct is valid: $If $current$ >= $min$ && ! ($current$ > $max$) Current value looks ok. $Else Current value is out of range. $Endif Exit$ExitLeave the page immediately. $ExitLoopLeave the current loop immediately.
$Switch var $Case val_1 ... ... $EndCase $Case val_2 ... ... $EndCase $Default ... ... $EndCase $EndSwitchDepends on the expression value, the appropriate case is executed. If no expression matches with the value, it is possible to add a catch-all tag ("$Default").
$ExpandFile filepath $ExpandFile is different from $Include: for example, a variable or an SQL query defined in the expanded file will no longer be accessible when back in the original file. filepath is relative to the directory where the current template is located.
$Include filepath $Include is different from $ExpandFile: for example, a variable or an SQL query defined in the expanded file will be accessible when back in the original file. filepath is relative to the directory where the current template is located.
$Forward URL URL may be absolute or relative. $Forward will be ignored if it is not considered relevant (eg. in a $ExpandFile or in a SendMail action...) Only the original HTTP request will be forwarded (additional parameters like those set using $Assign will be lost). Execute a system command$System command $System executes a system command: the command output, if any, is displayed on the output HTML stream. For example, to list the files in /tmp on a Unix system: <pre> $System ls -l /tmp </pre> $Debug textDisplays text on the standard output, for debug purposes. Examples: $Debug Hello World! $Debug LastName = $LastName$
$# Comment Fetch a product according to its reference$FetchProduct prodId varnameFetch a product from the products database (default: EProduct table), according to the product reference (default: ProdId column). The result is a "data object", named after the specified variable name, with at least 3 fields: ProdId, Name, Price. That statement is useful to encapsulate products, and avoid extensive use of complex SQL requests in the templates (eg. to compute a product price according to a customer category or a currency...) |