How to change an image based on page url in ColdFusion? - coldfusion

I have a header which I'm calling through a cfinclude. However, I want to be able to change an image on the header based off the page url. So if you are in index.html, use this image a and if not, use this image b. I know nothing about ColdFusion, so any help would be greatly appreciated. Thank you.

check CGI scope, and use basic <cfif>
by default, only *.cfm is mapped to Coldfusion. So your index.html should really be index.cfm.

Look at the cgi variables
<cfif cgi.script_name eq 'index.cfm'>
<img scr="imga.jpg">
<cfelse>
<img src="imgb.jpg">
</cfif>

Related

how can I use cfdirectory to make a list of documents downloadable using ColdFusion/Lucee?

In my Application.cfc, I setup a mapping
this.mappings["/downloads"]="J:\Downloads\documents";
In my template, I have
<cfdirectory action="list" directory="#expandpath("/downloads")#" filter="*.zip|*.docx" name="downloads" recurse="yes">
<!--- <cfdump var="#expandpath("/software")#"> --->
<cfdump var="#downloads#">
<ul>
<cfoutput query="#downloads#">
<li>#downloads.name#</li>
</cfoutput>
</ul>
I'm trying to make the documents downloadable but when the link is clicked, nothing is happening which makes me think my links are not correct however when I mouse over the link, I see the full path which is correct.
What am I missing to make the list of documents clickable?
Here is the URL displayed when mouseover the 3rd document for example.
Since the files are outside of your webroot you will need to have ColdFusion read the file and send it back to the browser.
You will need to create a page, like download.cfm, that can accept a URL parameter to know which file to access. Once you have selected the file you can use something like the following to stream the file.
<cfheader name="Content-disposition" value="attachment;filename=#datafile#">
<cfcontent file="#datafile#" type="application/pdf">
The above code was pulled from https://www.raymondcamden.com/2006/03/10/Ask-a-Jedi-Using-ColdFusion-to-serve-files
WARNING:
Reading URL parameters in this way and giving people access to the filesystem is extremely unsafe. Safer alternatives should be considered before moving something like this into a production environment.
All I needed to do for this exercise is to setup a mapping in my Application.cfc. As others have stated, there is zero security here but for the purpose of this exercise of understanding virtual directories (IIS) and aliases (CommandBox), this is sufficient.
this.mappings["/guides"]="J:\guides";
Then I can use cfdirectory to build my query object
<cfdirectory action="list" directory="j:\guides" recurse="false" name="nameofqry" type="file" sort="datelastmodified desc" filter="*.docx">
Next, perform a cfoutput using my alias as the a href link
<cfoutput query="nameofqry" maxrows="40">
<li>#nameofqry.name#</li>
</cfoutput>

Why do CFM templates being included with cfinclude need their own <cfoutput> wrappers?

I am using <cfinclude> to include various pages within a master page. In my master page all the body content is wrapped in <cfoutput> tags. However I noticed that after testing, the included .cfm page don't see the <cfoutput> tags at all and hence don't display the dynamic data.
<body>
<cfoutput>
<cfinclude template="page1.cfm" />
<cfinclude template="page2.cfm" />
<cfinclude template="page3.cfm" />
</cfoutput>
</body>
In the above example, the included templates that have dynamic data in them will not display properly. To solve this I have to add <cfoutput> tags within each of the CFM files. How come they can't use the <cfoutput> tags that are already there within the body?
Because each CFML file is compiled separately, and whether or not to output something is determined at compile time, not runtime.
Set aside how ColdFusion works, you want to do this. The point of using something like CFINCLUDE is that you're able to write a chunk of code once and use it in multiple situations.
Think of it as a poor mans' encapsulation. Someone should be able to use that included template without getting bogged down in the mundane details of the template that is being included.

Cfdocument being served by server despite being in cfsavecontent

It seems that when I use the <cfsavecontent> tag, the output of that is being served by the server (without the variable being outputted), which, to me, kind of defeats the purpose of <cfsavecontent>.
If this is important: my application uses ColdSpring, ModelGlue and Transfer ORM.
Here's my example code in a function:
<cfsavecontent variable="testvar">
<cfinclude template="test.cfm" />
</cfsavecontent>
<cfreturn testvar>
And the template:
<cfdocument format="PDF" pagetype="A4" orientation="portrait" unit="cm">
<cfoutput>
<!--- PDF content here --->
</cfoutput>
</cfdocument>
The PDF content is being parsed by my browser (Google Chrome), while the view hasn't even been loaded in. How can I best prevent this from happening?
Just to clarify: I am not outputting the #testvar# variable yet in this code, though it seems it loads the template in the browser anyways.
To achieve what you're trying to do, should you not simply be using the name attribute of <cfdocument> to put the PDF data into a variable, instead of trying to <cfsavecontent> it?
Disclosure: I've never used <cfdocument> for anything other than proof-of-concept code and testing, but that's what I'm inferring from the docs.
As I also needed to make multiple PDF documents merge, I ended up doing the following. Many thanks to Adam Cameron for providing the solution to my initial issue.
In the template file, I use the <cfdocument> tag with the name attribute to save the PDF in a variable (thanks to Adam Cameron for this)
Then, I store all the PDF documents in an array in their binary format
In my view, I merge the PDF documents together by using <cfpdf>'s merge action, and using a cfloop, to loop over the array, inside it.
Finally, I display the content by using <cfcontent> and using the variable attribute with toBinary(myPdf)
This got me to where I am.
cfinclude will process the test.cfm page, and the way you configured cfdocument will cause "opening" of pdf document in your browser.
You can prevent openning of this file by saving file on the disc:
<cfdocument format="PDF" pagetype="A4" orientation="portrait" unit="cm" filename ="test.pdf" overwrite ="yes">
But this will not prevent execution of cfinclude in the cfcontent tag, it will just prevent opening in the browser.
You can observe cfinclude as request to the server, it will always be executed.
The solution would be to invoke request on test.cfm file which contains cfdocument in the moment that you actually want to generate pdf.
Example: Use javascript on client to invoke report service which will generate and pop out the screen with pdf report.
Hope this helps.

CFinclude issue

I am trying to cfinclude a file and also pass some values to that specific file. I don't know if it is possible so any reply is appreciated.
This is my code:
<cfoutput>
<cfset path = "?id=#email_id#&contactid=#email_contactid#&ownerid=#email_ownerid#">
<cfinclude template="/email_results#path#">
</cfoutput>
My mapping goes something like this:
E:\sites\exampleCom\cf_modules\reports\resultsTemplate.cfm
I did a #expandPath("/email_results#path#")# and it is giving me the correct output
(E:\sites\exampleCom\cf_modules\reports\resultsTemplate.cfm?id=123&contactid=123&ownerid=123)
but when I am trying to include the file it bugs out. Can I pass in variables through the cfinclude? Is this what it bugs out my code?
No, you do not pass variables to an include page. The include will be execute in the same context, so it can directly see the email_id and other variables.

ColdFusion include

I'm currently learning ColdFusion. I have a background in PHP and I am a bit confused by this.
I have a select menu and I want the options to be saved in a different file. (For example options.cfm) When I call the file I want to include the options inside the select menu.
Now I realize I could probably do it with something like this:
<select>
<cfinclude template="options.cfm">
</select>
Although what I really want to do is a bit more complicated. I want to have the cfinclude saved inside a variable. I realize this won't work but it is basically what I want to accomplish:
<cfset options=<cfinclude template="options.cfm">>
Is there anyway to do that? Or at least a better way to accomplish what I am doing.
Take a look at the cfsavecontent tag, It allows you to capture what would otherwise have been output to the response :
<cfsavecontent variable="options">
<cfinclude template="options.cfm">
</cfsavecontent>
UPDATE: Instead of using cfsavecontent every time you need those options saved to a variable, you could instead do it once inside of the options.cfm file. Then, anytime you include the file, it will create the variable.
<!--- Inside options.cfm --->
<cfsavecontent variable="options">
<option value="val1">Value 1</option>
<option value="val2">Value 2</option>
<option value="val3">Value 3</option>
</cfsavecontent>
Then where ever you needed that variable to exist you would simply need to cfinclude that file.
<cfinclude template="options.cfm">
i know this is a bit late but one issue i see is if this is site wide or just per client.
if site wide then great but if it is different on each client it could cause some issues.
my solution as i don't use cookies or sessions is to create a temp table and write the variables to it. each page that loads and needs that data queries and/or writes to the table.
a client id variable is created when the client visits the site and the table is named it.
just a thought.