Lasso Soft Inc. > Home

[js_flattenparams]

Linkjs_flattenparams
AuthorJohan Solve
CategoryTechnical
Version8.x
LicensePublic Domain
Posted08 Feb 2008
Updated08 Feb 2008
More by this author...

Description

This custom type is for developers of custom tags or types. Custom tags (or custom type member tags) don't automatically recognize an array as a prebuilt set of parameters. The way inlines allow this is a special case. Using this custom type it is possible to have arrays (or maps or tree maps) passed to a tag be recognized as regular parameters. Unfortunately for values that are passed by reference in an array, the parameter value reference is broken and the value will be copied instead.

Sample Usage

define_tag: 'testtag';
	// flatten arrays passed as params
	local: 'flattenedparams' = js_flattenparams: params;
	// replace the current params with the flattened params
	params = #flattenedparams -> params;
	// autocreate locals from params
	locals = #flattenedparams -> locals;

	// custom tag code comes here

	// show what params we saw
	return: @(params -> (join: '
')); /define_tag; testtag: -name='Nisse Hult', (array: -address='home', -phone='124'), -more='even more', (array: -page=1, -table='test', -sort), -mode='sql', -sort2, ;

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_type: 'flattenparams',
	-namespace='js_',
	-prototype;
	local: 'params'=array,
		'createlocals'=map;
	define_tag: 'oncreate';
		params = params -> first;
		local: 'param', 'element', 'convertparam';
		// loop until we reach the last param element (the params array will possibly grow while looping so we can't just iterate)
		while: loop_count <= (params -> size);
			#param -> detachreference;
			#param = @(params -> (get: loop_count));
			if: #param -> isa('array') || #param -> isa('map') || #param -> isa('treemap');
				// convert non-array parameters to arrays
				if: !(#param -> isa('array'));
					#convertparam=array;
					iterate: #param, #element;
						if: #element -> isa('pair') && #element -> value === null;
							// plain keyword without value
							#convertparam -> insert(#element -> name);
						else;
							#convertparam -> insert(#element);
						/if;
					/iterate;
					// replace existing param non-array structure with converted param array
					#param=#convertparam;
				/if;
				// we want to merge the array with the rest of the params
				// but first we want to remember what locals need to be auto-assigned from the array.
				iterate: #param, #element;
					if: #element -> isa('pair') 
						&& #element -> name -> isa('string') && #element -> name -> beginswith('-') 
						// there is no point in creating references here since they will be copied when the array is merged later
						(self -> 'createlocals') -> insert((#element->name - '-') = (#element->value));
					else: #element -> isa('string')
						&& #element -> beginswith('-') 
						// plain keyword without value
						(self -> 'createlocals') -> insert((#element - '-') = null);
					/if;
				/iterate;
				// merge the array into the params at the right position (this will kill any reference)
				params -> (merge: #param, loop_count);
				
				// replace the array with null so we know we can delete it later
				#param=null;
			/if;
		/while;
		// remove all ex arrays that we have merged into the params array
		params -> (removeall: null);
		// return the params we saw
		(self -> 'params') = params;
	/define_tag;
	
	define_tag: 'params';
		return: @(self -> 'params');
	/define_tag;

	define_tag: 'locals';
		return: @(self -> 'createlocals');
	/define_tag;
/define_type;

Related Tags

Comments

No comments

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