A few examples of "tag as script" in ColdFusion 11

This post is more than 2 years old.

Yesterday I posted a reminder that ColdFusion 11 developers can stop using the old "CFCs for Tags" feature that provided support for certain things in script. I mentioned that everything could be used in cfscript now and that the old CFCs were (sometimes, not always) a bit buggy.

Brad Wood suggested I actually provide an example of this and I thought it was a good idea. I wrote up two quick demos that demonstrate the new syntax. This isn't super deep or anything, but hopefully it gives you a basic idea of how this new syntax looks. First, an example of a few different tags:

cfhttp(url="http://www.cnn.com",result="cnnReq");
writeDump(cnnReq.responseHeader);

cfhttp(url="http://www.cnn.com",result="cnnReq",method="post") {
	cfhttpparam(type="formfield",name="something",value=createUUID());	
}
writeDump(cnnReq.responseHeader);

cfsearch(collection="test1", criteria="cat", name="searchResults");
writeDump(searchResults);

cffeed(action="read", source="http://feeds.feedburner.com/raymondcamdensblog", query="feed");
writeDump(feed);

Make note of how you can't use a result (ie, foo = goo()) in any of these calls and instead use an argument. That could be easily fixed and should be. Second, note how child tags are handled in the second cfhttp call. I don't have anything to add to it, but as I said, just note the syntax.

For the heck of it, I then wrote a second demo. This one takes an array of RSS URLs and uses cfthread to fetch them in parallel. It then merges the resultant queries and sorts them. A mini-aggregator if you will.

feeds = ["http://feeds.feedburner.com/raymondcamdensblog","http://www.cflib.org/rss","http://feeds.feedburner.com/ColdfusionbloggersorgFeed"];
threads = [];

//for each feed, use a thread to fetch rss
feeds.each(function(f,x) {
	var name = "get_#x#";
	threads.append(name);
	cfthread(name=name,feed=f) {
		cffeed(action="read", source=attributes.feed, query="feed");
		thread.feed = feed;
	}
});	

cfthread(action="join",name=threads.toList());
//copy thread queries out and create sql merge statement
sql = "";
feeds.each(function(f,x) {
	variables["feed#x#"] = cfthread["get_#x#"].feed;
	if(x > 1) {
		sql &= " union ";
	}
	sql &= "select * from feed#x#";
});
merged = queryExecute(sql,{},{dbtype:"query"});
//credit Vince Collins: https://vincentcollins.wordpress.com/2008/02/22/cffeed-dont-forget-you-can-cast-the-resulting-query-column/
merged = queryExecute("select content, title, rsslink, cast(publisheddate as date) as publisheddate from merged order by publisheddate desc", {}, {dbtype:"query"});
writedump(var=merged,top=20);

I hope this helps!

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Link Worx Seo posted on 5/3/2016 at 12:29 AM

What are you using here a function? I am trying to understand the syntax. Your syntax is: cffeed(action="read", source="http://feeds.feedburner.com...", query="feed");

writeDump(feed);

Mine: feedservice = new feed();

myFeed = feedservice.read(source='http://www.linkworxseo.word..., query='rssFeed');

Is your syntax because Railo, Ruby, Lucee or because it is CF!! or function?

I tried doing it with your syntax by placing the action, source, query but get no output. When I try using my method it actually works without errors but if I add the query attribute it does not produce results when doing a WriteDump(rssFeed);

Guess I am trying to understand why my syntax does not work properly since the example shows it as the proper way to set the query string.

Help!!!! :)

Comment 2 by Link Worx Seo posted on 5/3/2016 at 1:45 AM

I already figured out what it was lacking. The .Query appended to the end of the variable.

feedservice = new feed();

rssFeed = feedservice.read(source='http://www.linkworxseo.word..., query='rssFeed').Query;

Although I am still wanting to know about what your syntax is about?

Comment 3 (In reply to #1) by Raymond Camden posted on 5/3/2016 at 11:01 AM

Link, the syntax, x = new feed(), comes from the fact that Adobe built CFCs to do Feed, Queries, HTTP, and something else too, I can't remember. This was done *before* they built a solution to allow all tags to be used in script. It is the old solution, and was pretty buggy. Hence me saying you should be used the tags as function stuff now. I believe Lucee supports this too.

If you tried my cffeed call and got not output, that's expected. cffeed doesn't generate output - it generates variables. Try dumping feed.

Comment 4 (In reply to #2) by Raymond Camden posted on 5/3/2016 at 11:01 AM

Again - see my other note. You should move away from this feature as soon as possible.

Comment 5 (In reply to #4) by Link Worx Seo posted on 5/4/2016 at 4:19 PM

Okay, right now I got past that portion of it, but I now have been using a do while loop to fetch the category label column and trying to find a way to loop through the items which are a complex string. I am trying to create a separate link for each category in the list.

I managed to find a way to get any category/tag from the fourth column, but I need it to be able to get each one from the column in a loop method without manually coding it to listGetAt() for each one.

I can get the entire list of the categories, but I need those to be separate because they need to be a separate link for each one so the user can select the link to the blog by category/tag.

Any suggestions?

Comment 6 (In reply to #5) by Raymond Camden posted on 5/4/2016 at 7:10 PM

I honestly can't tell what you are asking now. Are you asking how to loop over a list? Because that's pretty simple, right?

Comment 7 (In reply to #6) by Link Worx Seo posted on 5/4/2016 at 7:53 PM

No, looping a list is not the problem. As long as I have been doing this, you should know that by now. I am trying to fetch each label from the categorylabel column for an RSS Feed. Each label has to be separate so it can be established as a href link.

Comment 8 (In reply to #7) by Raymond Camden posted on 5/4/2016 at 8:03 PM

Ok, so what does categorylabel look like? Give me an example.

Comment 9 (In reply to #8) by Link Worx Seo posted on 5/4/2016 at 8:09 PM

This is the output for the column categorylabel:
BLOGGING,blogging,review,seo,stats,wordpress

Each one of these needs to be a single entity to be placed as a link. Another words each one would need to be queried separately for purpose of making each one a link.

Comment 10 (In reply to #9) by Raymond Camden posted on 5/4/2016 at 8:11 PM

Ouch - I asked you to tell me just the one thing - not share all that code. ;) Please rephrase this into something simpler and one line. I want you to show me what the value of categorylabel is (one sample value) and explain what you want to do with it.

Comment 11 (In reply to #10) by Link Worx Seo posted on 5/4/2016 at 8:57 PM

Here is what I can come up with after talking to you and finding a different form of loop to dot it. Except each one would be set separately. I would have to manually code each one. So if there were more than the number of records/labels on the blog post, those would not get displayed/output because the count would not match.

catQuery = queryNew("category");

queryAddRow(catQuery);

querySetCell(catQuery, 'category', '#ListGetAt(rssFeed.CATEGORYLABEL[1],1)#');

queryAddRow(catQuery);

querySetCell(catQuery, 'category', '#ListGetAt(rssFeed.CATEGORYLABEL[2],1)#');

queryAddRow(catQuery);

querySetCell(catQuery, 'category', '#ListGetAt(rssFeed.CATEGORYLABEL[3],1)#');

for ( row in catQuery ) {

writeOutput("a itemprop='url' href='#metadata.link#/category/#lCase(ReReplace(row.category, ' ', '-', 'all'))#' title='#lCase(row.category)#' class='feedBlock'>#lCase(row.category)# a>");

}

Comment 12 (In reply to #11) by Raymond Camden posted on 5/4/2016 at 9:05 PM

So is it working then? I keep asking you to show me an example of the data and what you want to do with it but you don't want to do that I think. :)

Comment 13 (In reply to #12) by Link Worx Seo posted on 5/4/2016 at 9:11 PM

I get it, you want a link to what is going on?
http://www.linkworxseo.info

Yes, I managed to get it with the method shown in the previous post.

Comment 14 (In reply to #13) by Raymond Camden posted on 5/4/2016 at 9:11 PM

If you got it working - then I'm good. :)

Comment 15 (In reply to #14) by Link Worx Seo posted on 5/5/2016 at 8:30 AM

Yes, after four day's I got it finished. You always seem to spark some kind of programming idea when I start to ask you for help. Not sure what it is, but on just about every occasion I start to request help from you I end up coming to an answer and or solution. It must just be from inspiration and I know your style fairly well along with the fact the more I write about it the better. Writing it out seems to help usually. :) Big Smiles - Thank you.

Comment 16 (In reply to #15) by Raymond Camden posted on 5/5/2016 at 1:39 PM

Glad to have been a help!