Thread: Newbie Question

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    3
    Rep Power
    0

    Newbie Question


    Hi There,

    I am not a Programmer, however for the small functionalities I usually need I am always able to find a workaround... this time I am having no luck... hopefully someone can help...

    My question is... Is there a way to include a dynamic variable based on a predetermined section of my site?... lets say Page Title, an H1 or something along those line...

    For Example...I have an include file that loads in every page with a testimonial and randomly rotates the content... however i would like to include a relevant keyword to whatever page the user is visiting on that testimonial... for instance

    User is visiting a Page Related to Red Shirts.

    The include file should look like:

    I had the opportunity to purchase a (INCLUDE PAGE TITLE ' RED SHIRTS')back in 2011 and I love it.

    Will a simple function work? or do I need Data Base connectivity?

    Thank you for your help...
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    84
    Rep Power
    10
    Not sure I fully understand the question but I'll take a stab anyway:

    The <cfinclude> tag can accept a variable in its "template" argument. For example, you can statically call the include as:

    Code:
    <cfinclude template="someTemplate.cfm">
    Or you can dynamically call the include:

    Code:
    <cfinclude template="#stringVariableOfIncludeFilename#">
    From there, all you have to do is define some logic for what to store in the variable. For example, you might say if the page content contains the keyword "Red shirt", you set the string variable to the include for the red shirt content.

    To sum it up, you might could use a function like this:

    Code:
    <cffunction name="getIncludeFileName" returntype="string">
    	<cfargument name="content" type="string" required="true">
    	<cfset var filename = "default.cfm">
    
    	<cfif arguments.content contains "red shirt">
    		<cfset filename = "redShirtInclude.cfm">
    	<cfelseif arguments.content contains "blue shirt">
    		<cfset filename = "blueShirtInclude.cfm">
    	</cfif>
    
    	<cfreturn filename>
    </cffunction>
    You can also use a <cfswitch> block in place of a series of <cfif>'s.

    Then on your display code, you can do something like:

    Code:
    <cfinclude template="#getIncludeFileName(thisPageContent)#">
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    3
    Rep Power
    0

    Thank You


    Thank You,

    I will give it a Try - Sorry If I didn't explain it correctly but i think you got it.
  6. #4
  7. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,273
    Rep Power
    968
    Fairly sure you just want to use a variable, e.g.:

    <cfset titleName = "Red Shirt" />
    <cfoutput><h1>The title is: #titleName#</h1></cfoutput>
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    3
    Rep Power
    0

    Thank You


    This is way easier for someone like me... where would you recommend I place the <cfset titleName = "Red Shirt" />

    Right now I have it right above the Include File - but I was wondering if it will make more sense if it was inside the <head>

    THANKS!!!!!
  10. #6
  11. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,273
    Rep Power
    968
    As long as it is declared before it is used, it doesn't technically matter where you declare it. That said, the concepts and tradeoffs involved in structuring software can (and do) fill entire books and college curricula. What "makes sense" depends on many factors (readability vs. speed vs. size vs. likelihood of change, and so on).

    Honestly, the best thing you can probably do is sit down and experiment/learn. I'd recommend something like Learn CF in a Week.

IMN logo majestic logo threadwatch logo seochat tools logo