Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In CF, can I call a custom tag using a variable for its name?

I would like to call a custom tag using a variable in it's name. Like this

<cfset slist = 'product_categories'>
<cf_cu_show_#slist#>

This gives me an error on the #. The custom tag cu_show_product_categories is present and working when I call it the conventional way. The idea is to build a list to loop through, calling several custom tags.

<cfset slist = 'product_categories'>
<cfif a = 'blogs'>
    <cfset slist = listAppend(slist,"blogs")>
</cfif>
<cfif b = 'posts'> 
    <cfset s_list = listAppend(slist,"last_posts")>
</cfif>
<cfloop list="#slist#" index="i">
    <cf_cu_show_#i#>
</cfloop>

I tried to google, but cannot find anything useful. Any help would be appreciated.

like image 483
Veni Avatar asked Jan 26 '23 17:01

Veni


1 Answers

As you already discovered, using a variable name in calling the custom tag is invalid. The way around this is to call the custom tag using the <cfmodule> syntax instead. In your first scenario, you would call it like this.

<cfset slist = 'product_categories'>
<cfmodule template="cu_show_#slist#.cfm">

In the lower example, you would modify your code as such.

<cfset slist = 'product_categories'>
<cfif a = 'blogs'>
    <cfset slist = listAppend(slist,"blogs")>
</cfif>
<cfif b = 'posts'> 
    <cfset s_list = listAppend(slist,"last_posts")>
</cfif>
<cfloop list="#slist#" index="i">
    <cfmodule template="cu_show_#i#.cfm">
</cfloop>

Here's the documentation link on how to use <cfmodule>. https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-tags/tags-m-o/cfmodule.html

I also found another decent link where they demonstrate your scenario in which you need to supply the tag name dynamically as seen here at https://flylib.com/books/en/2.375.1.420/1/

like image 77
user12031119 Avatar answered Feb 23 '23 22:02

user12031119