Sometimes it is required to write data into text files e.g. some time back I had to create a very large XML file using data from Couch (for exporting into an accounting package).
To help with that, I created a simple tag - <cms:write>. I am sharing it here as it could come in handy to others tackling similar use-cases.
Please add the following code in your 'couch/addons/kfunctions.php' file (if this file is not found, rename the 'kfunctions.example.php' to this name) -
Usage:
The usage of this tag is simple. All content enclosed within this tag will be written into the file specified as its first parameter (always considered relative to the site's root) e.g.
The code above will write "Hello world!" into a file named 'my.txt' present in the site's root (i.e. the parent folder of 'couch').
If the file is not present, the tag will create it. The created file should now contain the following -
Now suppose we used the same statement twice e.g. as follows -
Assuming the 'my.txt' file does not exist to begin with, when the first statement executes it creates the file and writes "Hello world!" into it. When the second statement executes, the 'my.txt' file is already present so it uses the same file and writes "Hello world!" into it appending it to the "Hello world!" written by the first statement. The 'my.txt' file will now contain the following data -
Please notice above that there are no 'newlines' between the two writes because the data we specified each time did not contain any newlines. That is to say, the <cms:write> tag, by default, writes into the file verbatim whatever data is given to it.
If newlines were required between the two writes, you can either -
1. Add the newline as part of the data e.g. as follows
2. Or, set 'add_newline' parameter to '1' e.g.as follows
One last point -
As mentioned above, if the specified file already exists, this tag by default appends data to that file's existing contents.
If you wish the tag to discard any existing file and create a new file, set its 'truncate' parameter to '1'.
For example, the following will result in the target file always containing only one "Hello world!" because each statement discards the existing file and starts a new file before writing its content into it -
In practice you'll normally want to use 'truncate' only once somewhere at the beginning of your script so that it starts with a blank slate. Rest of the write statements then append their data into this new file.
Addendum:
For a real-life example using this tag, please see the CSV exporter example discussed here -
viewtopic.php?f=8&t=11378
Hope this helps.
To help with that, I created a simple tag - <cms:write>. I am sharing it here as it could come in handy to others tackling similar use-cases.
Please add the following code in your 'couch/addons/kfunctions.php' file (if this file is not found, rename the 'kfunctions.example.php' to this name) -
- Code: Select all
// Tag <cms:write />
// writes the enclosed contents into file
$FUNCS->register_tag( 'write', 'my_write_handler' );
function my_write_handler( $params, $node ){
global $FUNCS;
extract( $FUNCS->get_named_vars(
array(
'file'=>'', /* file name if provided needs to be relative to the site directory */
'truncate'=>'0', /* will begin afresh */
'add_newline'=>'0', /* appends newline character to the content */
),
$params)
);
// sanitize params
$file = trim( $file );
if( !$file ){
$file = 'my_log.txt';
}
$file = K_SITE_DIR . $file;
$truncate = ( $truncate==1 ) ? 1 : 0;
$add_newline = ( $add_newline==1 ) ? 1 : 0;
$content='';
foreach( $node->children as $child ){
$content .= $child->get_HTML();
}
if( $add_newline ){
$content .= "\r\n";
}
$fp = @fopen( $file,'a' );
if( $fp ){
@flock( $fp, LOCK_EX );
if( $truncate ){
ftruncate( $fp, 0 );
rewind( $fp );
}
@fwrite( $fp, $content );
@flock( $fp, LOCK_UN );
@fclose( $fp );
}
return;
}
Usage:
The usage of this tag is simple. All content enclosed within this tag will be written into the file specified as its first parameter (always considered relative to the site's root) e.g.
- Code: Select all
<cms:write 'my.txt' >Hello world!</cms:write>
The code above will write "Hello world!" into a file named 'my.txt' present in the site's root (i.e. the parent folder of 'couch').
If the file is not present, the tag will create it. The created file should now contain the following -
Hello world!
Now suppose we used the same statement twice e.g. as follows -
- Code: Select all
<cms:write 'my.txt' >Hello world!</cms:write>
<cms:write 'my.txt' >Hello world!</cms:write>
Assuming the 'my.txt' file does not exist to begin with, when the first statement executes it creates the file and writes "Hello world!" into it. When the second statement executes, the 'my.txt' file is already present so it uses the same file and writes "Hello world!" into it appending it to the "Hello world!" written by the first statement. The 'my.txt' file will now contain the following data -
Hello world!Hello world!
Please notice above that there are no 'newlines' between the two writes because the data we specified each time did not contain any newlines. That is to say, the <cms:write> tag, by default, writes into the file verbatim whatever data is given to it.
If newlines were required between the two writes, you can either -
1. Add the newline as part of the data e.g. as follows
- Code: Select all
<cms:write 'my.txt' >
Hello world!
</cms:write>
2. Or, set 'add_newline' parameter to '1' e.g.as follows
- Code: Select all
<cms:write 'my.txt' add_newline='1'>Hello world!</cms:write>
One last point -
As mentioned above, if the specified file already exists, this tag by default appends data to that file's existing contents.
If you wish the tag to discard any existing file and create a new file, set its 'truncate' parameter to '1'.
For example, the following will result in the target file always containing only one "Hello world!" because each statement discards the existing file and starts a new file before writing its content into it -
- Code: Select all
<cms:write 'my.txt' add_newline='1' truncate='1'>Hello world!</cms:write>
<cms:write 'my.txt' add_newline='1' truncate='1'>Hello world!</cms:write>
In practice you'll normally want to use 'truncate' only once somewhere at the beginning of your script so that it starts with a blank slate. Rest of the write statements then append their data into this new file.
Addendum:
For a real-life example using this tag, please see the CSV exporter example discussed here -
viewtopic.php?f=8&t=11378
Hope this helps.