2
votes

Trying to convert a <cfif> statement into it's if script counterpart but it's body contains a <form> tag.

This is located within an onRequestStart method within my application.cfc file.

CF

<cfscript>
    if (!isdefined("session.username")) {
        include "TEMP_Head.cfm";
        </cfscript>
        <form action="index.cfm" method="Post">
                        <input type="submit" name="LogOutUser" value="Log Out">
                </form>
        <cfscript>
        include "TEMP_Foot.cfm";
    }
</cfscript>

I expected this to run but I get the error message:

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

3
You can't split an if command that way. Also, the <form> tag is basically output, so it must be wrapped in WriteOutput() like this. - SOS

3 Answers

4
votes

If you're dead set on this, as @Ageax commented, the form is just a string of HTML. You can try this:

<cfscript>
    if (structKeyExists(session, "username")) {
        var loginForm = '<form action="index.cfm" method="Post">';
        loginForm &= '<input type="submit" name="LogOutUser" value="Log Out">';
        loginForm &= '</form>';

        include "TEMP_Head.cfm";
        writeOutput(loginForm);
        include "TEMP_Foot.cfm";
    }
</cfscript>

Personally, I'd put the form in its own include in order to avoid putting presentation code in Application.cfc.

Also, per James' answer, live in the now! Stop using isDefined() and use structKeyExists() or structname.keyExists() depending on which version of CF you're using.

3
votes

I wouldn't even do cfscript on this on

<cfif NOT isdefined("session.username")>
    <cfinclude template="TEMP_Head.cfm">
    <form action="index.cfm" method="Post">
        <input type="submit" name="LogOutUser" value="Log Out">
    </form>
    <cfinclude template="TEMP_Foot.cfm">
</cfif>

OffTopic

IsDefined() isn't the cleanest way to check for variables. Consider

<cfif NOT session.keyExists("username")>
2
votes

You can use code like below ( Include another cfm page, within that page write your HTML form code ) It will not give any error.

    <cfscript>
      if (!isdefined("session.username")) {
        include "TEMP_Head.cfm";  //Your Header part
        include "form_include.cfm"; // Your Form
        include "TEMP_Foot.cfm";   //Your Footer part 
      }
   </cfscript>

Within form_include.cfm use your HTML form code.

    <form action="index.cfm" method="Post">
      <input type="submit" name="LogOutUser" value="Log Out">
    </form>

REsult as screenshot