Lasso Soft Inc. > Home

[serve_csv]

Linkserve_csv
AuthorJason Huck
CategoryFile
Version8.x
Licensehttp://opensource.org/licenses/artistic-license.php
Posted14 Oct 2005
Updated28 Sep 2012
More by this author...

Description

Serves a CSV file. Optionally accepts an array of field names and an array of arrays, where each nested array represents one line of data.

If called within an inline without those parameters, the tag will use [field_names] and [rows_array].

Sample Usage

inline(
	$connection,
	-sql=$sql
);
	found_count > 0 ? serve_csv;
/inline;

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.

define_tag(
	'csv',
	-namespace='serve_',
	-optional='filename',
	-type='string',
	-optional='fields',
	-type='array',
	-optional='rows',
	-type='array',
	-priority='replace',
	-description='Serves a CSV file.'
);		
	fail_if(
		!local_defined('fields') && !field_names->size,
		-1,
		'[serve_csv] must be called with an array of field names or be within an inline where[field_names] contains values.'
	);

	fail_if(
		!local_defined('rows') && !rows_array->size,
		-1,
		'[serve_csv] must be called with an array of row data or be within an inline where [rows_array] contains values.'
	);

	!local_defined('fields') ? local('fields' = field_names);
	!local_defined('rows') ? local('rows' = rows_array);

	local('out' = string);
	
	iterate(#fields, local('f'));
		#out += #f;			
		loop_count == #fields->size ? #out += '\r\n' | #out += ',';
	/iterate;

	iterate(#rows, local('r'));
		iterate(#r, #f);
			#f = string(#f);
			#f->replace('"','""')&replace('\r\n','\n')&replace('\r','\n');
			#out += '"' + #f + '"';				
			loop_count == #r->size ? #out += '\r\n' | #out += ',';
		/iterate;
	/iterate;

	file_serve(
		#out,
		-file=(local_defined('filename') ? #filename | 'results.csv'),
		-type='text/csv'
	);
/define_tag;

Comments

28 Oct 2009, Samuel Cohen

Line 64?

Looks like there's a wee li'l error on line 64. Seems a '>' (closing angle bracket) got replaced with '>'

Line 64 should read:
loop_count == #fields->size ? #out += '\r\n' | #out += ',';

Regardless... this tag has made my life easier on quite a few occasions. Thanks!

20 Apr 2008, Asle Benoni

Much work to change this to "serve_excel"?

Great tag. I tried to change this to serve Excel files but not sure I did it correctly. Changed line 32 "..#out += ','.. " to "..#out += '\t'.. " and line 38 to #f->replace('"','') and line 47 to "-type='text/excel' "

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft