Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert to cfscript (example, help) 1

Status
Not open for further replies.

imstillatwork

IS-IT--Management
Joined
Sep 26, 2001
Messages
1,605
Location
US
I want to run this peice of code as cfscript:

<cfif IsDefined('attributes.paramlist')>
<cfloop index=&quot;i&quot; list=&quot;#attributes.paramlist#&quot;>
<cfif IsDefined('attributes.#i#')>
<cfset paramlist = Insert(paramlist,&quot;&amp;#i#=#evaluate('URLEncodedFormat(attributes.#i#)')#&quot;,0)>
</cfif>
</cfloop>
</cfif>

Its the last peice in the templete that is NOT cfscript, and i want ti to be, cant figure it out though. especialy this part:<cfif IsDefined('attributes.#i#')>

Also, is there another more efficient way to do this? It is in a custom tag.

Thanks!

 
Looks fairly straightforward to convert, actually:
Code:
  if (IsDefined(&quot;attributes.paramlist&quot;)){
      for (i = 1; i LTE ListLen(&quot;#attributes.paramlist#&quot;); i = i + 1){
         sListItem = ListGetAt(&quot;#attributes.paramlist#&quot;, i); 
         if (IsDefined(&quot;attributes.#sListItem#&quot;)){
                lstParamlist = Insert(lstParamlist,&quot;&#sListItem#=#URLEncodedFormat(attributes[sListItem])#&quot;,0);
         }
      }
  }

would be more or less the direct translation.

Since attributes is a structure (when within a custom tag), you can access the value of the element by referencing the keyname... this means you don't have to use Evaluate (which is very slow).


But, since attributes is a structure, you can also dynamically determine the elements (by using StructKeyList)... rather than have to pass in the paramlist as an attribute. This elliminates the need for all the IsDefined as well... since the loop will only iterate over elements that exist:
Code:
  lstQueryString = &quot;&quot;;
  if (NOT StructIsEmpty(attributes)){
      for (i IN attributes){
         lstQueryString = ListAppend(&quot;#lstQueryString#&quot;,&quot;#i#=#URLEncodedFormat(attributes[i])#&quot;,&quot;&&quot;);
      }
  }
  WriteOutput(link is: &quot;somepage.cfm?#lstQueryString#&quot;);


As for whether it belongs in a custom tag or not... it's hard to say without knowing how you're actually using it. If you're running CF 5.0, you could certainly put it in a User Defined Function... but then the attributes structure wouldn't be available, so you'd have to figure out some way to solve that (probably use the &quot;arguments&quot; array instead).

A custom tag will probably be a bit slower than a UDF, or coding it directly in the page... but it's probably just fine as a tag.


-Carl
 
it was easy to move to cfscript. I found my error. I will definatly work with what you sugestted also. i always try not to use evaluate. this was an old bit of code where i thought i needed to. change is good!

thansk again.

 
actually, I'm going to leave the paramlist alone, I only want parameters on the list passed form page to page, not all of the other vars in attributes need to be thrown around.

definatly going to get rid of the evaluates though.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top