Is there a way to use in cfscript? I can't find a simple example online. I have a .cfc file built using cfscript and I'm trying to use cfxml.
I have a XMLContent variable and I tried the code below.
cfxml variable="myXML" {
WriteOutPut(#XMLContent#);
}
It is returning: function keyword is missing in FUNCTION declaration. in my cfxml declaration.
You could do this.
cfxml (variable="myXML") {
WriteOutPut(XMLContent);
}
But I suspect you are trying to
<cfscript>
myXML = XMLParse(XMLContent);
</cfscript>
Related
I am doing a cfhttp call using a cfx_http5 tag as it is faster and better than cfhttp. so the links are coming as:
sort A
so i add the following script using the replace
http://mysubdomain.domain.com/http://mysubdomain.domain.com/e9.asp?rpttype=298&sortBy=1&sortOrder=2
<cfset lnk = ReplaceNoCase(objget, 'href="', 'href="http://mysubdomain.domain.com/', 'all')>
in few of the links, it is coming as correct but in few of the links it is coming as above appending one more to the already existing one,
so i want to make it conditional, if it exists, do not append or add or else add if there is no link
any ideas?
You can use regular expressions with negative lookahead like this:
<cfset lnk = reReplaceNoCase(objget, 'href=\"(?!http\:\/\/)','href="http://mysubdomain.domain.com', 'ALL')>
This will work for both type of links.
Simply create a conditional if statement
lnk = 'http://mysubdomain.domain.com/e9.asp?rpttype=298&sortBy=1&sortOrder=2';
if (!findNoCase('mysubdomain.domain.com', lnk)) {
lnk = ReplaceNoCase(objget, 'href="', 'href="http://mysubdomain.domain.com/', 'all');
}
I'm attempting to build a method call from strings that have been passed into an object that refer to another object.
normally when calling an object we write the code like this:
application.stObj.oNewsBusiness.getNews(argumentCollection=local.stArgs);
However what I have done is created an array that contains the object name, the method name and the argument collection.
<cfscript>
local.stArgs = {};
local.stArgs.nNewsID = 19;
local.stArgs.sAuthor = "John";
local.aData = [];
local.aData[1] = local.stArgs;
local.aData[2] = "stObj.oNewsBusiness";
local.aData[3] = "getNews";
</cfscript>
however i am struggling to recombine all this to be a method call.
UPDATE using suggestion but still with issue
While cfinvoke seems to work for:
<cfinvoke component="#application.stObj.oNewsBusiness#" method="#local.sMethod#" argumentcollection="#local.stArgs#" returnvariable="local.qData"></cfinvoke>
it doesn't work when doing something like:
<cfscript>
local.stArgs = local.aData[1];
local.sObject = local.aData[2];
local.sMethod = local.aData[3];
</cfscript>
<cfinvoke component="application.#local.sObject#" method="#local.sMethod#" argumentCollection="#local.stArgs#" returnvariable="local.qData"></cfinvoke>
it generates an error:
Could not find the ColdFusion component or interface application.stObj.oNewsBusiness
CFInvoke is generally used to handle dynamic method calls.
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7e0a.html
CFInvoke has an argumentcollection attribute so you can pass your arguments in the way you are used to.
Dan is correct CFInvoke is the way to go
<cfinvoke component="#mycomponentname#" method="get" arg1="#arg1#" arg2="#arg2#" arg3=..>
<cfinvoke component="application.#local.sObject#" method="#local.sMethod#"argumentCollection="#local.stArgs#" returnvariable="local.qData"></cfinvoke>
from your update won't work because there are no # signs around the component variable.
You could do
<cfset local.componentName = "application." & local.sObject>
<cfinvoke component="#local.componentName#" method="#local.sMethod#"argumentCollection="#local.stArgs#" returnvariable="local.qData"></cfinvoke>
There's probably an inline way of combining application. with the variable on the cfinvoke call, but I don't know off the top of my head.
Edit: Dan Wilson's comment does it better in an inline way.
I am trying to set a variable in a cffunction.
The result is this:
<cfset local.layouts.appLayout = '../../app/layouts' & local.appController.new()>
The above code works. In the local.layouts.appLayout structure it assigns the return of the new method in the appControler. That is what I need it to do.
My problem is that I need to dynamically assign the method portion of that statement. I have another variable coreRoute.action that equals "new" in that function but I cannot seem to get the syntax right.
I have tried this:
<cfset local.layouts.appLayout = '../../app/layouts' & local.appController.coreRoute.action()>
That does not work and I can see why. I have also tried this:
<cfset local.layouts.appLayout = '../../app/layouts' & local.appController & #coreRoute.action# & '()'>
I have tried many variations of this syntax and I just cannot get it right.
Anyone have any ideas about how to do this. I am stuck.
Thanks in advance for any help.
UPDATE: With Todd Sharp's help I ended up using this and it worked great:
<cfinvoke component="#local.appController#" method="#coreRoute.action#" returnvariable="local.act">
<cfset local.layouts.appLayout = '../../app/layouts' & local.act>
You should look into using <cfinvoke> for dynamic method invocation. Try a Google search for "coldfusion dynamic method invocation" - here's one of the top results:
http://www.bennadel.com/blog/1320-ColdFusion-CFInvoke-Eliminates-The-Need-For-Evaluate-When-Dynamically-Executing-User-Defined-Functions.htm
In addition, if you want to do it entirely in script, you can, using this approach:
dynFn = this["foo" & bar];
dynFn(stuff);
This is in a cfc, if you're doing it from outside the cfc or not using a cfc at all, just change "this" to wherever your method is.
I'm trying to learn the new cfscript syntax, as well as use cflogin.
I'm guessing I can't mix cf tags in cfscript, and I don't see a script equivalent to cflogin, cflogout, cfloginuser.
Should I call a component that is written in the CF8 syntax in order to use cflogin?
public void function onRequest(required string Page) output="true" {
if (StructKeyExists(url,"logout")) {
<cflogout>
}
<cflogin>
local.qryUsr = new Components.Usr.Login(form);
if (local.qryUsr.Recordcount) {
<cfloginuser name="#form.UsrName#" password="#form.UsrPassword#" roles="#local.qryUsr.Roles#">
} else {
request.errorMessage = "Incorrect login";
include login/login.cfm;
return;
}
</cflogin>
include arguments.Page;
}
You cannot directly mix tags and scripts. However, you can fake it by writing function wrappers around the tags:
<cffunction name="logout">
<cflogout />
</cffunction>
and call like:
logout();
Obviously, this is a trivial example. You'd want to specify your arguments, your return value, etc. in your actual code.
Note one: Do not do this for a generic query function that accepts user input, as you won't be able to use cfqueryparam.
Note two: I generally don't do this. If I'm writing code that depends on tag-only operations, I use the tag syntax.
as a side note, there is a small effort going on over at CFLib.org to create functions for all CF Tags.
check out CFMLLib for more details
How do I invoke a static method on a CFC without using cfinvoke? I know that I can do this:
<cfinvoke component="MyComponent" method="myStaticMethod' arg1="blah" returnvariable=myReturnVar>
I would like to be able to invoke this method the same way I would a UDF:
<cfset myReturnVar = MyComponent.myStaticMethod(blah)>
This, however, does not work. Is there syntax that I am messing up or is this just not possible?
not possible, since there's no "static method" in ColdFusion.
The <cfinvoke> line in your question is the same as:
myReturnVar = CreateObject("component", "MyComponent").myStaticMethod(arg1="blah");
You need to create the object first.
<cfset MyComponent = createObject("component","MyComponent") />
<cfset myReturnVar = MyComponent.myMethod(blah) />
I know this is a really old post but here's an updated answer for more modern CF/Lucee supporting static constructs for anyone who might stumble across this like I did :-)
based off of:
https://modern-cfml.ortusbooks.com/cfml-language/components/static-
component MyComponent{
static {
appendToArgValue : "text"
}
public static function myStaticMethod( arg1 ){
return arg1 & static.appendToArgValue;
};
}
MyComponent::myStaticMethod("blah")
Lucee documentation: https://docs.lucee.org/guides/lucee-5/component-static.html