|
| 9.6 Formatting Output with fmt |
|
Inside our <SQL>
loop, we printed out the patent country,
number and date. We wanted to print the date in a specific format:
although Texis dates are accurate down to the second, we only want
to print the month, day and year. For this we used <fmt>
.
The <fmt>
function is used to specify the exact format
of output data. It closely follows the syntax of the C function printf()
, in that the first argument is a format string
containing percent codes that detail how to print the remaining
arguments.
| Code | Output |
|---|
<$x = "<B>">
<$y = "</B>">
<fmt "This is %sbold%s" $x $y>
<$x = 12345.5>
<fmt "The price is $$%1.2kf" $x>
<fmt "q=%U" "what?">
|
|
This is bold
The price is $12,345.50
q=what%3F
|
|
The first example simply prints out two strings with %s
,
<B>
and </B>
. Note that HTML escapement is not
done: this is a way to avoid the normal HTML escapement for a
variable.
The second example prints a floating-point number as a dollar
value. Note the escaped dollar sign in the format - $
is
not special to <fmt>
per se, but to Vortex it would
indicate a variable reference which we don't want (and isn't allowed
inside a string). So we escape the dollar to get a literal dollar
sign. Following it is a percent sign, introducing the format code.
The 1.2
says that the printed field should be at least 1
character wide overall, and have at most 2 decimal places. The k
is a flag - unavailable in standard printf
- that says
to insert commas every three digits. Finally, the f
says
we're printing a floating-point number.
The last example prints a string using %U
. This
URL-encodes the argument, and is very useful for hand-constructing
query strings in URLs. (However <submit>
is often
easier.)
In the patent example,
we used <fmt>
to print out the patent date $pdate
in the <search>
function:
<fmt " %at " "%b %e %Y" $pdate> <P>
|
Here the %t
code indicates a time/date is to be printed.
The a
flag before it says that we're using a specific
sub-format for the date, which will be the next argument.
This argument is a strftime()
-style format, similar to
printf()
but the codes are for specific parts of a date.
%b
prints the month name, %e
the day of the month,
%Y
the 4-digit year. Finally the date itself is the next
argument.
Automatic castingIf an argument is the wrong type for the corresponding format
code, it is automatically converted (cast) to the right type.
For example, we could give a string for the date, and Texis would
convert it to a date (provided it's in a format known to Texis):
<fmt "The current time is:\n%t\n" "now">
|
We gave the string "now
" for a time: Texis converts this
to a date, knowing that "now" means "the current time". Texis knows
other strings as dates, such as "start of last month", "-3 weeks",
"next Tuesday", as well as a standard format: "YYYY-MM-DD HH:MM:SS".
Escape sequences
Also note the backslash escape in our last example: in a <fmt>
format string, the standard C string escape sequences
such as \n
for a newline are recognized.
Other codes
There are more format codes for printing Roman numerals,
automatically highlighting query strings in text, HTML escapement, etc.
See the Vortex manual on <fmt>
.
strfmt
The <strfmt>
function is identical to <fmt>
except that instead of printing its output, it is returned in $ret
as a string.
|