So the announcement functionality on our site displays a "read more" link by default using the following code (in part):
<cfif announcement.recordCount gt 0>
<cfloop query="announcement">
<cfoutput>
<td colspan="2"><span class="left">#teaser_text# Read more »
</cfoutput>
</cfloop>
(Note, there is a cfquery statement prior to that, which I excluded for brevity in the code)
What I'm trying to do here is get the "Read More" link to show after the #teaser_text# only if no link is contained within #teaser_text#, so that I can manually add links in if needed and remove the automatically generated link.
Any thoughts on a cfif statement that would do this?
Thanks.
EDIT: To clarify, I want to remove "Read more" if ANY link is found within teaser_text.
To only show the read more link if no hyperlink is found within teaser_text, this check is likely to be good enough:
<cfif NOT refindNoCase('<a\s[^>]*?\bhref\s*=',teaser_text) >
Read more »
</cfif>
If you want to check for URLs, not for hyperlinks, you need to get more fancy.
You also need to remember that this is treating teaser_text as text (not as HTML), so commenting out a link will not prevent it from being found (if that matters, you need to investigate HTML DOM parsers; and there aren't any for CF so you'd need to look at the Java ones).
This should work:
<cfif findnocase('http://', teaser_text) eq 0>
Read more »
</cfif>
If you are placing the links in manually just change the first parameter of the findnocase() function [i.e. htp/https] or use a regex to figure out if it is a url [via: refindnocse() ]
-sean
Something like this should do what you want
<cfif announcement.recordCount gt 0>
<cfloop query="announcement">
<cfif findnocase("href",anouncement.teaser_text) >
#anouncement.teaser_text#
<cfelse>
<a href="/announcements/?id=#announcement.id#" > #anouncement.teaser_text# Read more </a>
</cfif>
</cfloop>
</cfif>
Related
I'm using <cfhttp> to pull in content from another site (coldfusion) and resolveurl="true" so all the links work. The problem I'm having is resolveurl is making the anchor links (href="#search") absolute links as well breaking them. My question is is there a way to make resolveurl="true" bypass anchor links somehow?
For starters, let's use the tutorial code from Adobe.com posted in the comments. You'll want to do something similar.
<cfhttp url="https://www.adobe.com"
method="get" result="httpResp" timeout="120">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
</cfhttp>
<cfscript>
// Find all the URLs in a web page retrieved via cfhttp
// The search is case sensitive
result = REMatch("https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?", httpResp.Filecontent);
</cfscript>
<!-- Now, Loop through those URLs--->
<cfoutput>
<cfloop array="#result#" item="item" index="index">
<cfif LEFT(item, 1) is "##">
<!---Your logic if it's just an anchor--->
<cfelse>
<!---Your logic if it's a full link--->
</cfif>
<br/>
</cfloop>
</cfoutput>
If it tries to return a full URL before the anchor as you say, (I've been getting inconsistent results with resolveurl="true") hit it with this to only grab the bit you want.
<cfoutput>
<cfloop array="#result#" item="item" index="index">
#ListLast(item, "##")#
</cfloop>
</cfoutput>
What this code does is grab all the URLs, and parse them for anchors.
You'll have to decide what to do next inside your loop. Maybe preserve the values and add them to a new array, so you can save it somewhere with the links fixed?
It's impossible to assume in a situation like this.
There does not appear to be a way to prevent CF from resolving the hashes. In our usage of it the current result is actually beneficial since when we present content from another site we usually want the user to be sent there.
Here is a way to replace link href values with just anchor if one is present using regular expressions. I'm sure there are combinations of issues that could occur here if really malformed html.
<cfsavecontent variable="testcontent">
<strong>test</strong>
go to google
go to section
</cfsavecontent>
<cfset domain = replace("current.domain", ".", "\.", "all") />
<cfset match = "(href\s*=\s*(""|'))\s*(http://#domain#[^##'""]+)(##[^##'""]+)\s*(""|')" />
<cfset result = reReplaceNoCase(testcontent, match, "\1\4\6", "all") />
<cfoutput><pre>#encodeForHTML(result)#</pre></cfoutput>
Output
<strong>test</strong>
go to google
<a href="#section>go to section</a>
Another option if you are displaying the content in a normal page with js/jquery available is to run through each link on display and update it to just be the anchor. This will be less likely error with malformed html. Let me know if you have any interest in that approach.
CFBuilder admin storage
15cdb5dcb6.jpg
Application.cfm
34ed7586e1.jpg
Login.cfm
<cfif not isDefined('FORM.submitButton')>
<cfform name="loginForm" method="post" action="#CGI.SCRIPT_NAME#">
Login:
<cfinput type="text" name="login" required="yes">
Password:
<cfinput type="password" name="password" required="yes">
<br>
<cfinput type="submit" name='submitButton' value="Sign">
<br>
<cfinput type="button" name='registerButton' value="Register">
</cfform>
<cfelse>
<cfquery name='getUser' datasource="dbfortest">
SELECT * FROM usertable WHERE login="#FORM.login#" ;
</cfquery>
<cfif getUser.RecordCount NEQ 0>
<cfif FORM.password eq getUser.password>
<cflock scope="Session" timeout="60" type="exclusive" >
<cfset Session.loggedIn = "yes">
<cfset Session.user = "#FORM.login#">
</cflock>
<cfoutput>#StructKeyList(Session)#</cfoutput>
<cfelse>
Your pass isn't correct.
</cfif>
<cfelse>
There is no user with this name.
</cfif>
</cfif>
part of page when i want to use login including.
<cfif Session.loggedIn eq "no">
<cfinclude template="login.cfm">
</cfif>
<cfif structKeyExists(session, "user")>
<cfoutput>Welcome, #Session.user#.</cfoutput>
</cfif>
<cfoutput>#StructKeyList(Session)#</cfoutput>
Hello everyone, please help me understand these sessions' behavior.
The whole problem consists in attempting to pass variables from one page to another.
So after login i don't see the session.user in session struct.
How can i pass this?
Have already tried different browsers.
#Aquitaine has given you some good information. I just wanted to also point out that another part of your problem is likely that you have set a 10 second life span for your sessions. That's probably not long enough.
In the Application.cfm example that you posted you have this line:
sessiontimeout="#createTimespan(0,0,0,10)#"
The arguments for the CreateTimeSpan function are as follows:
createTimespan(days, hours, minutes, seconds)
As such you are assigning a 10 second lifespan for sessions. Perhaps you meant to set 10 minutes instead of 10 seconds.
To figure out what's going on with the session variables, try putting in some debug code right after your cfset session statements to make sure that they're happening. Maybe <cfdump var="#session#">.
You do not need to cflock your session scope (and have not needed to since CFMX). See Adam Cameron's 2013 post on when to lock scopes
If your debug code runs and you see the session variables, but then they're gone on the next page, that may be an issue with your session storage (which is a different part of cfadmin) or else whatever front-end webserver you're using. Try <cfdump var="#session#"> in onRequestStart in Application.cfc and make sure that JSESSIONID is the same on every request. (or try disabling J2EE session variables in CFADMIN and see if the same problem persists with CFID/CFTOKEN).
If your debug code doesn't run, then you should be seeing one of your error conditions.
For ease-of-reading, be consistent in your casing when refering to scopes, e.g. session not Session. While this kind of thing may not matter functionally, it can get you into trouble with portability when referencing paths or components.
Some other issues:
If you are going to use a boolean value for loggedIn then use a boolean value: true or false or 1 or 0 or (if you must) yes or no but not "yes" which is a string; instead of being able to do if (session.loggedIn) if you will have to do if (session.loggedIn == 'yes') and nobody will be happy.
If this is meant to be working, production site code, at a minimum you need to be using cfqueryparam as you do not ever want to pass unescaped user input directly to a database query.
You might also head over to the CFML slack at cfml.slack.com and ask on #cfml-beginners for some pointers on writing login forms.
I am severely new to ColdFusion... I have searched for help on this statement and have found a bunch of material, but still don't understand what is going on. All of the parts of this statement make sense, but when I put them all together, it's confusing... the ColdFusion 8: IsDefined("URL.variable) and is not"" thread is the closest, but I still don't understand. This is the 1st statement in the index.cfm file of my application. It's not throwing an error, I just want to understand how it works. Thank you.
I have yet to be able to successfully post code here, so here is a link to a text version of the index.cfm.
Edit:
The code below should be the relevant sections related to URL.openFile
<cfif isdefined("URL.openFile")>
<cfquery name="getFile" datasource="xxxxxxxx">
SELECT filename, filename2, filecontent, filesize
FROM Help_FooterInfo
WHERE Help_id=5 and Section='Registration'
</cfquery>
<cfset sproot=#getDirectoryFromPath(getTemplatePath())#>
<cfset newDest = #sproot#&"temp\">
<cfoutput query="getFile">
<cfheader name="Content-Disposition" value="attachment; filename=#getfile.FileName2#">
<cfcontent type="application/msword" variable="#getfile.filecontent#">
</cfoutput>
</cfif>
...
<cfquery name="getRegistration" datasource="xxxxxxxx">
select * from help_footerinfo where help_id=5
</cfquery>
....
<cfoutput>#getRegistration.Content#</cfoutput><br>
<a href="<cfif #getRegistration.filename2# neq "">index.cfm?openfile=Yes</cfif>" target="_blank">
<u><cfoutput>#getRegistration.FileName#</cfoutput></u>
</a>
The error message I am receiving (see comment below): ORA-00942: table or view does not exist (ColdFusion application)
This:
<cfif IsDefined("URL.variable") and URL.variable is not "" >
means, "If url.variable actually exists and is not an empty string".
A better alternative for isDefined("URL.variable") is StructKeyExists(url,"variable").
Other alternatives for is not "" include len(trim(url.variable)) gt 0, and isNumeric(url.variable).
I was wondering if its possible to exclude data from an wraparound page footer based on which content is loaded into the index page in ColdFusion
Roughly it would be something like.
IF (not = pageToBeExcluded)
THEN {
Show content
}
There are any number of ways to do that.
The simplest is probably to key on the page address.
<cfif CGI.SCRIPT_NAME DOES NOT CONTAIN "someArbitraryPage.cfm">
<!--- show this content --->
...
</cfif>
As mentioned, you might want to use getCurrentTemplatePath() or getBaseTemplatePath().
Personally, I would probably set a variable in the template where I want this to happen. The main benefit of this is that I don't need to keep changing my footer code every time I add (or remove) a file from my list of files where I want this.
<!--- in the template itself --->
<cfset request.suppressFooterContent = true>
Then, in the footer:
<cfparam name="request.suppressFooterContent" default="false">
<cfif NOT request.suppressFooterContent>
<!--- display content here --->
...
</cfif>
I have a coldfusion web site I need to change. Have no idea or experience with this environment (I do know ASP.NET). All I need to do is to write a condition based on the referral value (the URL) of the page, and redirect to another page in some cases.
Can anyone give me an example of the syntax that would perform this?
All of the other examples would work...also if you're looking to redirect based on a referral from an external site, you may want to check CGI.HTTP_REFERER. Check out the CGI scope for several other options.
<cfif reFindNoCase('[myRegex]',cgi.http_referer)>
<cflocation url="my_new_url">
</cfif>
...my example uses a regex search (reFind() or reFindNoCase()) to check the referring URL...but you could also check it as a list with / as a delimiter (using listContainsNoCase()) depending on what you're looking for.
Lets assume your the URL variable you are basing this on is called goOn (http://yoursite.com?goOn=yes) then the following code would work:
<cfif structKeyExists(url, "goOn") AND url.goOn eq "yes">
<cflocation url="the_new_url" addtoken="false">
</cfif>
Nothing will happen after the cflocation.
There is a CGI variable scope in ColdFusion that holds information on the incoming request. Try the following:
<cfif CGI.SCRIPT_NAME EQ 'index.cfm'>
<cflocation url="where you want it to redirect" />
</cfif>
To see what else is available within the CGI scope, check out the following:
http://livedocs.adobe.com/coldfusion/8/htmldocs/Expressions_8.html#2679705
Haven't done coldfusion in a little while but:
<cfif some_condition_based_on_your_url>
<cflocation url="http://where_your_referrals_go">
</cfif>
<!--- continue processing for non-redirects --->
A dynamic version.
<cfif isdefined("somecondition")>
<cfset urlDestination = "someurl">
<cfelseif isdefined("somecondition")>
<cfset urlDestination = "someurl">
.
.
.
<cfelse>
<cfset urlDestination = "someurl">
</cfif>
<cflocation url = urlDestination>