Skip to main content
Ben Nadel at CFUNITED 2008 (Washington, D.C.) with: Qasim Rasheed and Sana Ullah
Ben Nadel at CFUNITED 2008 (Washington, D.C.) with: Qasim Rasheed ( @qasimrasheed ) Sana Ullah

Russian Doll Content Wrapping With CFSaveContent In ColdFusion

By on
Tags:

In web development, the term "Russian Doll" is sometimes used to refer to content that is wrapped inside another piece of content of the same type. This is based on the Russian Doll toy (Matryoshka), which has a multitude of smaller toys contained within it. In the past, I've looked at using the Russian Doll pattern for error handling in Node.js as well as for error handling in ColdFusion. But, its value extends beyond just errors - I often use the CFSaveContent tag to build up a content payload from the outside in. And, I thought it would make for a nice example.

If you dig into web application frameworks like Framework One (FW/1), they often use Russian Doll content nesting to render page responses. They do this by storing content into a given variable; and then, redefine said variable, over and over again, referring to itself in the subsequent expressions.

This might sound strange in the abstract, but we actually use this kind of approach all the time. Consider incrementing a numeric value:

value = ( value + 1 );

In this assignment, the value variable is being redefined using itself as part of the evaluated expression. When it comes to wrapping content, it's the same exact thing. And, with ColdFusion, the CFSaveContent tag makes this Russian Doll nesting extremely easy!

In the following ColdFusion template, I'm going to use several successive CFSaveContent tags. Each tag is going to assign content to the body variable; and, all but the first tag are going to include the body variable within their own tag bodies:

<cfoutput>

	<!--- Define the initial BODY content. --->
	<cfsavecontent variable="body">
		<p>
			This is the body!
		</p>
	</cfsavecontent>


	<!--- Wrap the BODY content in more content. --->
	<cfsavecontent variable="body">
		<header>
			This is the header.
		</header>
		<main>
			#body#
		</main>
		<footer>
			This is the footer.
		</footer>
	</cfsavecontent>


	<!--- Wrap the BODY content in EVEN MAOR content! --->
	<cfsavecontent variable="body">
		<!doctype html>
		<html lang="en">
		<head>
			<title>
				ColdFusion is Amaze-balls!
			</title>
		</head>
		<body>
			#body#
		</body>
		</html>
	</cfsavecontent>


	<!--- Output the doubly-wrapped content. --->
	#body#

</cfoutput>

As you can see, each subsequent CFSaveContent tag wraps the previous tag's body value by interpolating the #body# string into its own tag body evaluation. Each body value is "Russian Doll nested" back into itself.

If we then run this ColdFusion page and look at the generated source, we get the following output - I've decorated each branch of the DOM (Document Object Model) tree that was supplied by a CFSaveContent tag:

Screenshot of rendered HTML.

The CFSaveContent tag is definitely one of the unsung heroes of the CFML language. I use it all the time to store generated content into a variable (this is especially helpful in ColdFusion custom tags). And, when composing layouts, the CFSaveConent tag combined with Russian Doll nesting is a force to recon with!

Want to use code from this post? Check out the license.

Reader Comments

196 Comments

@Ben,

I love this (hidden from me) gem! I really hadn't ever considered using it in this way, but brilliant 👌 wunderbar 🎉 excellent 🙌

simple, elegant AND OH SO useful!

15,608 Comments

@Chris,

Awesome! That's exactly what I like to over-share -- you never know what someone has or has not seen yet. Never hurts!

196 Comments

@Ben,

Indeed. Much appreciated. Everything we've ever learned we've once learned for the first time. Today is that day for this particular topic 😂

Post A Comment — I'd Love To Hear From You!

Post a Comment

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel