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

Problem with <cfif> tag

Explorer ,
Nov 15, 2011 Nov 15, 2011

Basically, I'm trying to add some text to my header depending on whether or not the user is logged in. If they are, it will display one thing (Welcome, My Account, Logout), if they are not, it will display something else (Login/Register). I'm using Dreamweaver which I know can do some silly things with code, but being a CF and web design novice, the only way I could think of to do this is with a <cfif> tag. Here is what I've come up with, but I keep getting errors and I cannot figure out why. The first chunk of code is for the logout query, the second is a recordset to grab the first name of the current user, if they are logged in. Those appear to be working okay, the issues seem to be coming from the <cfif> tag in the <body>. The error I'm getting says I need a closing </cfif> tag, but it looks to me like it should be okay. Any thoughts on what I screwed up?

Thanks for any help you can give!

Jeremy

<cfif IsDefined("URL.MM_logout") AND URL.MM_logout EQ "1">

  <cflock scope="Session" type="Exclusive" timeout="30" throwontimeout="no">

  <cfset Session.MM_Username="">

  <cfset Session.MM_UserAuthorization="">

  </cflock>

  <cfset MM_logoutRedirectPage="../index.cfm">

  <cfif MM_logoutRedirectPage EQ "">

    <cfset MM_logoutRedirectPage=CGI.SCRIPT_NAME>

  </cfif>

  <cfset MM_logoutQuery=ListDeleteAt(CGI.QUERY_STRING,ListContainsNoCase(CGI.QUERY_STRING,"MM_logout=","&"),"&")>

  <cfif MM_logoutQuery NEQ "">

    <cfif Find("?",MM_logoutRedirectPage) EQ 0>

      <cfset MM_logoutRedirectPage=MM_logoutRedirectPage & "?" & MM_logoutQuery>

      <cfelse>

      <cfset MM_logoutRedirectPage=MM_logoutRedirectPage & "&" & MM_logoutQuery>

    </cfif>

  </cfif>

  <cflocation url="#MM_logoutRedirectPage#" addtoken="no">

</cfif>

<cfset CurrentPage=GetFileFromPath(GetBaseTemplatePath())>

<cfif IsNull(Session.MM_Username) EQ false>

  <cfquery name="userinfo" datasource="everettweb">

    SELECT Users.[First Name]

    FROM Users

    WHERE Users.Email = Session.MM_Username

    </cfquery>

</cfif>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

<link href="../main.css" rel="stylesheet" type="text/css" />

<style type="text/css">

<!--

a.headerlink:link {color: #FFFFFF;}

a.headerlink:hover {color: #D7A05D;}

a.headerlink:visited {color: #FFFFFF;}

a.headerlink:active {color: #FFFFFF;}

-->

</style>

</head>

<body>

<cfif IsNull(Session.MM_Username) EQ false>

  <p class="loginstatus"><cfoutput query="userinfo">Welcome, #[First Name]#!</cfoutput> <a href="../register_edit.cfm" class="headerlink">My Account</a> / <a href="<cfoutput>#CurrentPage#?MM_logout=1</cfoutput>">Logout</a><img src="../images/clear.gif" width="40" height="20" border="0" /></p>

<cfelse>

  <p class="loginstatus"><a href="../login.cfm" class="headerlink">Login</a> / <a href="../register.cfm" class="headerlink">Register</a><img src="../images/clear.gif" width="40" height="20" border="0" /></p>

</cfif>

</body>

</html>

5.2K
Translate
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 ,
Nov 16, 2011 Nov 16, 2011

The most impiortant thing when posting here for help and you "keep getting errors", is to include what the error message is, as well as looking at the line of code the error points to.  And paying attention to what line the error claims to be on... you should be able to cut down how much code you need to post (or, indeed, spot what the problem is yourself).

--

Adam

Translate
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 ,
Nov 16, 2011 Nov 16, 2011

My apologies if my post was unclear. As I said, the error points to the <cfif> tag in the body and says it needs an end tag. This is line 45.  The exact wording of the error is:

Context validation error for the cfif tag.

The start tag must have a matching end tag. An explicit end tag can be provided by adding </cfif>. If the body of the tag is empty, you can use the shortcut <cfif .../>.

The CFML compiler was processing:

• The body of a cfoutput tag beginning on line 46, column 27.

The error occurred in C:\ColdFusion9\wwwroot\Everett2\Templates\login_status.cfm: line 45

43 :

44 : <Body>

45 : <cfif IsNull(Session.MM_Username) EQ false>

46 : <p class="loginstatus"><cfoutput query="userinfo">Welcome, #[First Name]#!</cfoutput> <a href="../register_edit.cfm" class="headerlink">My Account</a> / <a href="<cfoutput>#CurrentPage#?MM_logout=1</cfoutput>">Logout</a><img src="../images/clear.gif" width="40" height="20" border="0" /></p>

47 : <cfelse>

I included the additional code for a couple reasons. First, I thought maybe a more experienced coder could give me a better way to do this, if one exists, and second, because I know sometimes the error isn't actually in the line the message references, but rather in the syntax of the function above. Thanks for your help!

Jeremy

Translate
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
Valorous Hero ,
Nov 16, 2011 Nov 16, 2011

Honestly there really are no shortcuts to figuring out that type of error, which is why it is good to test in small blocks.  Like others suggested, start by commenting out the code within that last set of cfif/cfelse tags.  (It is only a few lines) If it works with an empty cfif/cfelse block,  the problem is not a missing tag.  It is the code within the cfif or cfelse block.  Uncomment one line at a time until you find the offending party. 

-Leigh

Translate
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 ,
Nov 16, 2011 Nov 16, 2011

Cheers Jeremy

I see where you're coming from, and it's sound logic.  However from my perspective , I see a whole swag of code, and I know almost all of it will not be anything to do with your error, and I don't want to wade through it.  Well more to the point, from your perspective, fi you "over quote" your code, you will frighten people off, so I try to encourage people to focus what they post as tightly as possible or as is sensible.

I think your problem is not about CFIF statements, I think it is about this bit of code:

<cfoutput query="userinfo">Welcome, #[First Name]#!</cfoutput>

#[First Name]# is not valid CFML.  So the CF parser will choke on that, and it's failing to work out WTF is going on from that point onwards.  What it DOES see is that it's in the middle of a CFIF block, and it can no-longer see the closing tag, so it guesses at what the problem might be.

You will need to alias your column so it is in a format that CF can cope with (alphanumeric and underscores only, starting with alpha or underscore).  Or you could use bracket notation, and that'll probably work:

#userinfo["First Name"][currentRow]#

However aliasing is probably tidier.

Now... once you had me focused on the specific bit of code that was erroring, I spotted that right away even without poring over your code, which is another bonus in only posting the code that's relevant...

--

Adam

Translate
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 ,
Nov 17, 2011 Nov 17, 2011

jawmusic wrote:

the error points to the <cfif> tag in the body and says it needs an end tag. This is line 45.  The exact wording of the error is:

Context validation error for the cfif tag.

The start tag must have a matching end tag. An explicit end tag can be provided by adding </cfif>.

A mistake elsewhere could make ColdFusion to mistakenly report the <cfif> error. Another suggestion for you:

<cfif NOT IsNull(Session.MM_Username)> is more efficient than <cfif IsNull(Session.MM_Username) EQ false>

Translate
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 ,
Nov 17, 2011 Nov 17, 2011

regarding

<cfif NOT IsNull(Session.MM_Username)> is more efficient than <cfif IsNull(Session.MM_Username) EQ false>

In what way is either more efficient than the other?

Translate
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 ,
Nov 17, 2011 Nov 17, 2011

Dan Bracuk wrote:

regarding

<cfif NOT IsNull(Session.MM_Username)> is more efficient than <cfif IsNull(Session.MM_Username) EQ false>

In what way is either more efficient than the other?

Here are 2 ways at least:

1) More intuitive handling of boolean, in fact according to coding standards;

2) Less code.

This makes the code easier to maintain, in the long run, from one developer to the next. Imagine the effect of 1000, or even 10000, such lines of code.

Translate
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 ,
Nov 17, 2011 Nov 17, 2011

Dan Bracuk wrote:

regarding

<cfif NOT IsNull(Session.MM_Username)> is more efficient than <cfif IsNull(Session.MM_Username) EQ false>

In what way is either more efficient than the other?

I thought of this one earlier, but it's just an inspired guess. I hope someone will chime in and give it some arms and legs, or show it to miss the mojo.

Define the expressions: e1=isNull(session.mm_username), e2=false

Case 1: NOT isNull(session.mm_username)

ColdFusion (a) evaluates e1; (b) evaluates NOT e1.

Case 2: isNull(session.mm_username) EQ false

ColdFusion (a) evaluates e1; (b) evaluates e2; (c) evaluates e1 EQ e2.

Translate
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 ,
Nov 18, 2011 Nov 18, 2011

To be honest, it's not worth the ink claiming either of the two expressions is more efficient from a performance perspective.  Whilst the one you - BKBK - suggested would be microscopically more efficient than Dan's suggestion, economies like that are never really going to be a contributing factor when assessing performance issues.  In the bigger scheme of looking at the performance of a request as a whole, this sort of thing would be close to the bottom of the list of being something to consider looking at.

However I agree with BKBK that their version is more readable, and that's how I would always code this sort of statement.  But I think that comes down to personal preference.

--

Adam

Translate
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 ,
Nov 18, 2011 Nov 18, 2011
LATEST

Adam Cameron. wrote:

To be honest, it's not worth the ink claiming either of the two expressions is more efficient from a performance perspective.  Whilst the one you - BKBK - suggested would be microscopically more efficient than Dan's suggestion, economies like that are never really going to be a contributing factor when assessing performance issues.  In the bigger scheme of looking at the performance of a request as a whole, this sort of thing would be close to the bottom of the list of being something to consider looking at.

However I agree with BKBK that their version is more readable, and that's how I would always code this sort of statement.  But I think that comes down to personal preference.

Fair enough. However, the craziness is mine and mine alone. Dan didn't make a suggestion; he only asked.

Translate
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 ,
Nov 16, 2011 Nov 16, 2011

So the error message says that you need a closing <cfif> tag?  Given how many if/else blocks you have, it would be hard to solve by simply eyeballing the code.

I suggest adding some whitespace to your code to make it more readable.  Then, comment out everything starting with this line:

<cfset CurrentPage=GetFileFromPath(GetBaseTemplatePath())>

then run the page.  If it runs without error, start uncommenting blocks of code until it crashes.  That will help you identify the problem.

Translate
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 ,
Nov 17, 2011 Nov 17, 2011

I have a number of suggestions for you.

1) Remove spaces from database column names. Thus, in place of 'first name', use first_name or firstName.

2) The query will then be something like

SELECT Users.first_name

FROM Users

WHERE Users.Email = '#Session.MM_Username#'

3) Also change accordingly,

<cfoutput query="userinfo">Welcome, #first_name#!</cfoutput>

4) If you cannot change the name of the column, or don't want to, then delimit the column name with the backtick or ASCII 96 character

SELECT Users.`first name` as first_name

FROM Users

WHERE Users.Email = '#Session.MM_Username#'

or, alternatively,

SELECT Users.#chr(96)#first name#chr(96)# as first_name

FROM Users

WHERE Users.Email = '#Session.MM_Username#'

Translate
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 ,
Nov 17, 2011 Nov 17, 2011

Thanks everyone for your help. I was able to get it to work as I had hoped with a little trial and error.

Translate
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 ,
Nov 17, 2011 Nov 17, 2011

jawmusic wrote:

Thanks everyone for your help. I was able to get it to work as I had hoped with a little trial and error.

Good for you. Then please mark your question as answered.

Translate
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