5. Writing Files

How to write a file using WebDNA

Let's go a little further, and learn about the [math] context: this context is so powerful that it will allow you to calculate dates, modulo or cosinus.

[math date]{[date]}-{00/07/0000}[/math]
will show today's date less 7 days
will show 1.66666666666667
will show "59" and will store it as "a"
You can now use [a] in any mathematical operation; it will be replaced by "59"

There is a lot to discover with [math].

Now let's look at the [writefile] context. This context allows you to create a new file, or page, on your server.
[writefile page.txt]Text[/writefile]

will create a new file on your server, with name "page.txt" and content "Text". This means you can recover the content of a form and create a hard copy of it on your server. The problem here would be that any new entry would overwrite "page.txt". As already said, there are a number of solutions for every case. In this one, how about adding a time stamp? Something that would show year-month-day-hour-minute-second? here it is:

[writefile [date %Y%m%d][time %H%M%S]page.txt]Text[/writefile]

Your file will be named 20090412132433page.txt

Now, let's imagine your page has to show the day's date.

[writefile page.txt]This is today's date: [date][/writefile]

Easy enough! But your page.txt is now a static file. It will show the date the page was created. Let's imagine you want to keep it dynamic...

First, do not forget the <!--HAS_WEBDNA_TAGS-->:
[writefile page.dna]
This is today's date: [raw][date][/raw]

Done! The page will be dynamic: what we did was to add [raw][/raw] on each side of the context we don't want WebDNA to interpret when it writes the file. Instead, it wrote the literal text with brackets, "[date]". Now page.dna includes a dynamic tag that will be interpreted each time WebDNA reads the page, instead of just the creation day's date.

>>> Next page... Christophe Billiottet


Top Articles:

Related Readings:

4. Password Protecting

How to password-protect a page using WebDNA...

5. Writing Files

How to write a file using WebDNA...

3. Passing Data from a Form

Passing data from a form and retrieving it using WebDNA...

Kendo UI

This is a Code Library built with WebDNA...

6. Working with Databases

Working with WebDNA database...

WebDNA video training - WebDNA Syntax video