Created
March 4, 2023 15:06
-
-
Save bennadel/a776bb99b7a04fc2c342b35bcf3887e0 to your computer and use it in GitHub Desktop.
Rendering A Persistent Dismissible Banner Using Hotwire And Lucee CFML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfsavecontent variable="thistag.generatedContent"> | |
<cfoutput> | |
<!doctype html> | |
<html lang="en"> | |
<body> | |
<!--- | |
NOTE: Our banner Turbo Frame is marked Permanent. This means that once | |
Turbo Drive takes over the application navigation, the SRC attribute will | |
only be evaluated once. Then, the permanent state will be re-rendered on | |
each subsequent page load. | |
---> | |
<turbo-frame | |
id="banner" | |
src="banner.htm" | |
data-turbo-permanent> | |
<cfmodule template="../banner_content.cfm" /> | |
</turbo-frame> | |
<h1> | |
ColdFusion + Hotwire Banner Demo | |
</h1> | |
#thistag.generatedContent# | |
</body> | |
</html> | |
</cfoutput> | |
</cfsavecontent> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
if ( request.isPost ) { | |
application.bannerText = "Your upgrade has been processed!"; | |
// If the current request can support a Turbo Stream response, it means that Turbo | |
// Drive has taken over the page navigation. In that case, we will execute the | |
// REDIRECT using our custom Turbo Stream "visit" action so that we can refresh | |
// both the top-level page and the banner's Turbo Frame. | |
if ( request.turbo.isStream ) { | |
include "./upgrade_stream.cfm"; | |
exit; | |
// If this is a normal top-level page action, then let's redirect back to the main | |
// page as per usual and let ColdFusion render the newly-define banner content. | |
} else { | |
location( url = "index.htm", addToken = false ); | |
} | |
} | |
</cfscript> | |
<cfmodule template="./tags/page.cfm" section="upgrade"> | |
<cfoutput> | |
<h2> | |
Upgrade Your Subscription | |
</h2> | |
<form method="post" action="upgrade.htm"> | |
<button type="submit"> | |
Upgrade for <em>just</em> $9.99! | |
</button> | |
</form> | |
</cfoutput> | |
</cfmodule> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfcontent type="text/vnd.turbo-stream.html; charset=utf-8" /> | |
<cfoutput> | |
<!--- Refresh the banner Turbo Frame. ---> | |
<turbo-stream | |
action="visit" | |
data-url="banner.htm" | |
data-frame="banner"> | |
</turbo-stream> | |
<!--- Redirect to the main page. ---> | |
<turbo-stream | |
action="visit" | |
data-url="index.htm"> | |
</turbo-stream> | |
</cfoutput> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment