Use AttributeCollection for Custom Tags to Reduce Coding and Errors

I've recently started using the attributeCollection attribute for passing attributes to a custom tag instead of the name=value approach. I've found that this approach is much easier to read, to change and is less error prone, especially when your code will require you to use different custom tags with that use the same attributes, depending on the if statement match.

Here is an example of some code that used the name=value approach:


<cfif qryGetCampaignTypeInfo.templateID neq 0>
    <!--- Render eNewsletter Template --->                                
    <cf_renderTemplate                                     
    agency_id="#clientAgencyID#" templateID="#qryGetCampaignTypeInfo.templateID#"
    emcFlag="1" client_id="#getclientinfo.client_id#"campaign_id="#campaign_id#"
    writeSignature="1" useAgentInfo="#FORM.useagentinfo#"
    returnVariable="sTempFileContents">

<cfelseif qryGetCampaignTypeInfo.cruiseTemplateID neq 0>
    <!--- Render eNews Cruise Template --->
    <cf_renderCruiseTemplate     
    agency_id="#clientAgencyID#" templateID="#qryGetCampaignTypeInfo.cruiseTemplateID#"     
    emcFlag="1" client_id="#getclientinfo.client_id#" campaign_id="#campaign_id#"         
    writeSignature="1" useAgentInfo="#FORM.useagentinfo#"
    returnVariable="sTempFileContents">

<cfelseif qryGetCampaignTypeInfo.landTemplateID neq 0>
    <!--- Render eNews Cruise Template --->
    <cf_renderLandTemplate     agency_id="#clientAgencyID#"
    templateID="#qryGetCampaignTypeInfo.landTemplateID#" emcFlag="1"
    client_id="#getclientinfo.client_id#" campaign_id="#campaign_id#"
    writeSignature="1" useAgentInfo="#FORM.useagentinfo#"
    returnVariable="sTempFileContents">

<cfelseif qryGetCampaignTypeInfo.customTemplateID neq 0>
    <!--- Render custom Template --->
    <cf_customTemplates agency_id="#clientAgencyID#"                         
    templateID="#qryGetCampaignTypeInfo.customTemplateID#" emcFlag="1"
    client_id="#getclientinfo.client_id#" campaign_id="#campaign_id#"                 
    writeSignature="1" useAgentInfo="#FORM.useagentinfo#"                 
    returnVariable="sTempFileContents">

</cfif>

As you can see, I'm just calling different custom tags based on the type of campaign, but each of them use the same attributes. This is a little messy and difficult to read. It is also an easy way for errors to occur because if anything needs to change, it needs to change in every custom tag call. A better way to write this, and the way that I have started using, is to use the attributeCollection attribute. The same code would be written like this:


<cfscript>
    client_agency_id = clientAgencyID;
    client_id = getclientinfo.client_id;
    campaign_id = 12611;
    if(qryGetCampaignTypeInfo.templateID neq 0){
        templateID = qryGetCampaignTypeInfo.templateID;
    } else if (qryGetCampaignTypeInfo.cruiseTemplateID neq 0){
        templateID = qryGetCampaignTypeInfo.cruiseTemplateID;
    } else if (qryGetCampaignTypeInfo.landTemplateID neq 0){
        templateID = qryGetCampaignTypeInfo.landTemplateID;
    } else {
        templateID = qryGetCampaignTypeInfo.customTemplateID;
    }
    useAgentInfo = 1;
    structAttributes = StructNew();
    StructInsert(structAttributes, "agency_id", client_agency_id);
    StructInsert(structAttributes, "templateID", templateID);
    StructInsert(structAttributes, "emcFlag", 1);
    StructInsert(structAttributes, "client_id", client_id);
    StructInsert(structAttributes, "campaign_id", campaign_id);
    StructInsert(structAttributes, "writeSignature", 1);
    StructInsert(structAttributes, "useAgentInfo", useAgentInfo);
    StructInsert(structAttributes, "returnVariable", "sTempFileContents");
</cfscript>

<cfif qryGetCampaignTypeInfo.templateID neq 0>
    <!--- Render eNewsletter Template --->                                

    <cf_renderTemplate attributeCollection="#structAttributes#">

<cfelseif qryGetCampaignTypeInfo.cruiseTemplateID neq 0>
    <!--- Render eNews Cruise Template --->
    <cf_renderCruiseTemplate attributeCollection="#structAttributes#">

<cfelseif qryGetCampaignTypeInfo.landTemplateID neq 0>
    <!--- Render eNews Cruise Template --->
    <cf_renderLandTemplate attributeCollection="#structAttributes#">

<cfelseif qryGetCampaignTypeInfo.customTemplateID neq 0>
    <!--- Render custom Template --->
    <cf_customTemplates attributeCollection="#structAttributes#">

</cfif>

Now, when we need to make changes to the attributes, we change them in one place. And it's much easier to read the code.

Comments
Bridget's Gravatar Thanks for this Cindi! I am using the attributeCollection attribute for a custom tag that I'm building now and its so much easier than adding each attribute separately when implementing the tag!
# Posted By Bridget | 5/22/09 11:37 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.1.002. Contact Blog Owner