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

天大地大妈咪最大 提交于 2019-12-23 12:46:19

问题


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.


回答1:


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.




回答2:


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




回答3:


You need to escape the pound sign with another pound sign (##student) because, as you discovered, the pound sign has special meaning within <cfoutput> tags.



来源:https://stackoverflow.com/questions/16866039/treat-a-in-coldfusion-output-as-just-text-and-not-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!