• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Using <cfif> within <cfdocument>

New Here ,
Jul 09, 2007 Jul 09, 2007

Copy link to clipboard

Copied

Using <cfif> within a <cfdocument> works ok unless it is within a variable. The following code fragment:

<cfset var = "<cfif 1 eq 1>True part<cfelse>False part</cfif>">
<cfdocument format="pdf" filename="test.pdf">
<html>
<body>
<cfoutput>
1: #var#<br>
2: <cfif 1 eq 1>True part<cfelse>False part</cfif>
</cfoutput>
</body>
</html>
</cfdocument>

Results in generated PDF:

1: True part False part
2: True part

That is, if the <cfif> is written out in full within the <cfdocument> it works properly, but if it is within a variable, both true and false parts get put into the generated PDF.

How do I get the <cfif> to work properly when it is within the #var#?

Thanks, Chris Veness
TOPICS
Advanced techniques

Views

234

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

New Here , Jul 10, 2007 Jul 10, 2007
Thanks guys. Back to square one, with rather improved understanding, and a lesson learned for next time!

Votes

Translate

Translate
Community Expert ,
Jul 09, 2007 Jul 09, 2007

Copy link to clipboard

Copied

It has nothing in particular to do with cfdocument. It is that you practically disable a tag when you pass it as part of a string. Coldfusion ignores it. For example,

<cfset myVar = "<cfif 1 eq 1>True part<cfelse>False part</cfif>">
<cfoutput>#myVar#</cfoutput>

simply sends the text "<cfif 1 eq 1>True part<cfelse>False part</cfif>" to output. I usually left the story there.

However, Adam Cameron once made an important addition. Browsers are designed not to interprete or render tags they don't know. Since they don't know <cfif> or <cfelse>, they will ignore them. Hence, a browser will only display the text True partFalse part.




Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 09, 2007 Jul 09, 2007

Copy link to clipboard

Copied

> However, Adam Cameron, once made an important addition.

Good god! I'm being cited.

The problem with the OP's code is that the <cfif> is being generated at
runtime, and any CFML code one expects to execute needs to be there at
COMPILE time. CFML is not executed at runtime, so one cannot generate CFML
at runtime and hope for it to execute. That's not how it works. And even
if it was, one cannot expect it to be interpretted simply by *outputing*
it. As BKBK said: all you're doing is sending a string to the browser (or,
in this case, the PDF generator) which happens to have some CFML in it.

You need to revise your logic / approach.

--
Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 10, 2007 Jul 10, 2007

Copy link to clipboard

Copied

LATEST
Thanks guys. Back to square one, with rather improved understanding, and a lesson learned for next time!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation