Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created March 4, 2023 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennadel/a776bb99b7a04fc2c342b35bcf3887e0 to your computer and use it in GitHub Desktop.
Save bennadel/a776bb99b7a04fc2c342b35bcf3887e0 to your computer and use it in GitHub Desktop.
Rendering A Persistent Dismissible Banner Using Hotwire And Lucee CFML
<cfscript>
param name="request.context.clearBanner" type="boolean" default=false;
if ( request.context.clearBanner ) {
application.bannerText = "";
// If this request is not being scoped to a Turbo Frame (ie, Turbo Drive has not
// yet taken over the page navigation), let's redirect the user back to the
// homepage where ColdFusion will render the EMPTY banner.
if ( ! request.turbo.isFrame ) {
location( url = "index.htm", addToken = false );
}
}
</cfscript>
<cfoutput>
<!doctype html>
<html lang="en">
<!--- .... truncated .... --->
<body>
<!---
If this Banner page is rendered directly, we want to use "_top" as the target
so that the processing of the banner executes outside of any frame scoping
(and will redirect back to the main page).
--->
<turbo-frame id="banner" target="_top">
<cfmodule template="./banner_content.cfm" />
</turbo-frame>
</body>
</html>
</cfoutput>
<cfoutput>
<cfif application.bannerText.len()>
<div class="banner">
<p class="banner__text">
#encodeForHtml( application.bannerText )#
</p>
<a href="banner.htm?clearBanner=true" class="banner__close">
Close
</a>
</div>
</cfif>
<!--- We do not expect this tag to have a body. --->
<cfexit method="exitTag" />
</cfoutput>
<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>
<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>
<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