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

Using a conditional statement inside a <div>

Explorer ,
Oct 15, 2019 Oct 15, 2019

Copy link to clipboard

Copied

I inherited a program that I've had to modify and I am stuck on adding conditions to.  I have a very long <cfset> statement that is made up of a table and several divs.  

<td  style="border-bottom: thin solid black">
    <div align="left" class="HeadingTwo">
        #DollarFormat(Session.WorkOrderReceipt.amount)# #Session.WorkOrderReceipt.Instrument# #Session.WorkOrderReceipt.CheckNumber#
    </div>
</td>

This div shows the dollar amount on the receipt and if it's paid with a check then it shows that and then the check number, but since we've added credit/debit cards it would need to show that information as well.  So basically, if #Session.WorkOrderReceipt.instrument eq check# then #Session.WorkOrderReceipt.CheckNumber# would be correct, but if #Session.WorkOrderReceipt.Instrument eq creditDebitCard# then #Session.WorkOrderReceipt.creditDebitNumber# would be correct.  There is another div that would basically be the same with the exception of the dollar amount. 

I'm not sure how to handle this, and any suggestions would be greatly appreciated.

TOPICS
Advanced techniques , Getting started

Views

675

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 ,
Oct 15, 2019 Oct 15, 2019

Copy link to clipboard

Copied

I'm not sure that you can use a conditional within a string.  Never tried it.

 

However, if you get rid of the CFSET and place your code within <CFSAVECONTENT variable="myVar"> your content </CFSAVECONTENT>, it should work fine with conditionals (if/else or switch/case).

 

V/r,

 

^ _ ^

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
Explorer ,
Oct 15, 2019 Oct 15, 2019

Copy link to clipboard

Copied

Thanks you for the quick response. Although I had hoped to not have a rewrite, I figured that is what I would have to do.

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 ,
Oct 16, 2019 Oct 16, 2019

Copy link to clipboard

Copied

I hate to say it, but a rewrite would be the best solution.  The good news is that you already have the body code written, and it should just be a simple case of removing the CFSET and replace it with the CFSAVECONTENT.  Yeah, it's kind of a pain, but better than completely throwing what you have away and starting from scratch.  🙂

 

V/r,

 

^ _ ^

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
Participant ,
Oct 16, 2019 Oct 16, 2019

Copy link to clipboard

Copied

A simple way to do this is use an inline IF, replace the #Session.WorkOrderReceipt.CheckNumber# with the following

 

#iif(Session.WorkOrderReceipt.Instrument eq "check", de(Session.WorkOrderReceipt.CheckNumber), de(Session.WorkOrderReceipt.creditDebitNumber))#

 

This assumes that if Instrument is not CHECK then it must be CreditCard. If you expect more values then I would either create a function to return the ID or include a script before the output that sets a variable, like below.


<cfscript>
vInstrumentId = "Unknown";
switch (session.WorkOrderReceipt.Instrument)
{
    case "check":
        vInstrumentId = session.WorkOrderReceipt.CheckNumber;
        break;
   case "debitcard":
      vInstrumentId = session.WorkOrderReceipt.creditDebitNumber;
      break;
}
</cfscript>

<td style="border-bottom: thin solid black">
   <div align="left" class="HeadingTwo">
      #DollarFormat(session.WorkOrderReceipt.amount)# #session.WorkOrderReceipt.Instrument# #vInstrumentId#
   </div>
</td>

 

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
Community Expert ,
Oct 16, 2019 Oct 16, 2019

Copy link to clipboard

Copied

LATEST

Two points:

 

1) The code #Session.WorkOrderReceipt.Instrument eq creditDebitCard# makes me wonder whether you actually meant #Session.WorkOrderReceipt.Instrument eq 'creditDebitCard'#;

2) In any case, you could improve your code as follows, using the DRY (Don't Repeat Yourself) principle.

<cfset amountInDollars=dollarFormat(Session.WorkOrderReceipt.amount)>

<cfswitch expression="#Session.WorkOrderReceipt.instrument#">
	<cfcase value="check" >
		<cfset instrument="check">
		<cfset instrumentNumber=Session.WorkOrderReceipt.CheckNumber>
		<cfbreak>
	</cfcase>
	<cfcase value="creditDebitCard" >
		<cfset instrument="creditDebitCard">
		<cfset instrumentNumber=Session.WorkOrderReceipt.creditDebitNumber>
		<cfbreak>
	</cfcase>
	<cfdefaultcase>
		<!--- The code for the catch-all default payment method (if there is one) --->
	</cfdefaultcase>
</cfswitch>
...
...
<table>
...
<td  style="border-bottom: thin solid black">
<div align="left" class="HeadingTwo">
    #amountInDollars# #instrument# #instrumentNumber#
</div>
</td>
...
</table>

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