Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Treat a # in ColdFusion output as just text and not a variable?

I have made a JQuery list that I want to fill up with license plate numbers from a database, and to do so, I created a new cfm file and had it output what I wanted in html so I could just convert it later like this:

setPlates.cfm

<cfquery name="q_sample" datasource="cars_live">
  SELECT LICENSE FROM veh_rec 
</cfquery>

<cfoutput query="q_sample" >
        <li><a href='#Student'>#license#</a></li>
</cfoutput>

I call the get function to go into the setPlates.cfm file so I can add the license plates from the database as items in my list. Code is below:

<div class="ui-grid-solo">
            <div class="ui-block-a"><br></div>
            <div class="ui-block-a"><ul id="plates" data-role="listview" data-inset="true" data-filter="true" data-filter-reveal="true" data-filter-placeholder="Filter Students">
            <script type="text/javascript">
            $.get("setPlates.cfm")
                .done(function(data) {
                  $("#plates").html(data);
                });
                </script>
                <div id="plates"></div>
            </ul></div>
        </div><!-- /grid solo -->

Thing is, when it goes to that new file and starts reading the output, the #Student confuses it, because it's trying to read it as a variable in the database. #Student is a call to change pages to a new page which will list student information (like whether or not they own a parking permit) based on which license plate was selected. Is there a way to make it treat that pound sign as text, as opposed to the start of something like #license#? Kind of like you would do \" to use quotes inside of a string in java?

Also, if I remove the # from in front of student, all the license plates show up in the list, but they dont take me to the student page I'm trying to reach.

like image 494
Riot Goes Woof Avatar asked Dec 03 '22 23:12

Riot Goes Woof


2 Answers

As you know, # in coldfusion is a special character that is used to wrap a variable so that it outputs the contents of the variable when you are inside of a cfoutput or inside of other specific tags, such as cfmail. If you want to use # in your text, you must escape it by placing a second # right next to it.

<cfoutput>
    the item## is #itemnumber#
</cfoutput>

Taking this a step further, when you're inside of a coldfusion tag's attribute, you can use "" to escape " within a string in an attribute of a coldfusion tag.

In your case, i'd do this:

<cfoutput query="q_sample" >
        <li><a href='##Student'>#q_sample.license#</a></li>
</cfoutput>

or this:

<cfloop query="q_sample" >
        <li><a href='#Student'><cfoutput>#q_sample.license#</cfoutput></a></li>
</cfloop>

Note, i added q_sample to the variable name because it's a good practice.

like image 105
Kevin B Avatar answered Mar 03 '23 11:03

Kevin B


Use ## when you're inside a <cfoutput> block to escape the hash character.

like image 31
Henry Avatar answered Mar 03 '23 09:03

Henry