Saturday, August 15, 2026
HomeJavaScriptCreating String Interpolation Templates In ColdFusion

Creating String Interpolation Templates In ColdFusion


I have been feeling a bit blocked — creatively — currently. So, I have been attempting to maintain my thoughts lively with a bit inside house-cleaning on my weblog infrastructure. One of many issues that my weblog has to do is generate search engine optimized (search engine optimization) URLs based mostly on semantic segments. For instance, the deep-link to a weblog submit appears one thing like this:

/weblog/123-coldfusion-is-my-jam.htm

This useful resource is only a concrete implementation of this template:

/weblog/{id}-{slug}.htm

At present, I am hand-coding these URLs in every single place (with some minor efficiencies). Then, I am utilizing common expression patterns to parse these URLs on every request with the intention to route incoming URLs again to the proper ColdFusion Controllers.

There’s plenty of room for enchancment; and one of many first issues I am occupied with it the right way to generate these value-interpolated search engine optimization URLs utilizing centralized mechanics.

my ColdFusion JRegEx repository).

As a primary thought, I figured I may outline a pattern-based URL like:

/weblog/{{id}}-{{slug}}.htm

After which create a way that generates a “stamping” operate — ie, returns a closure — that can output interpolated values when given a dictionary of tokens to make use of. The stamping operate is generated utilizing a templateNew() operate, which is meant to supply this as a generic mechanic:

<cfscript>

	// ColdFusion language extensions (world features).
	embrace "/core/cfmlx.cfm";

	// ------------------------------------------------------------------------------- //
	// ------------------------------------------------------------------------------- //

	stamp = templateNew( "Good morning {{identify}}, I hope you {{assertion}}!" );

	dump(
		label = "Utilizing Java Sample Matcher",
		var = [
			stamp({ name = "Sarah", assertion = "have a great day" }),
			stamp({ name = "Laura", assertion = "stay cool" }),
			stamp({ name = "Jason", assertion = "keep on keeping on" }),
		]
	);

	// ------------------------------------------------------------------------------- //
	// ------------------------------------------------------------------------------- //

	/**
	* I return a operate that may stamp-out the given template, interpolating the phrases
	* from the offered dictionary into the template string. Placeholders are within the kind
	* of `{{token}}`. Any token lacking from the dictionary will likely be interpolated as an
	* empty string.
	*/
	personal string operate templateNew( required string templateSource ) {

		return ( required struct dictionary ) => {

			var matcher = new java( "java.util.regex.Sample" )
				.compile( "{{([a-zA-Z0-9_$]+)}}" )
				.matcher( templateSource )
			;
			var buffer = new java( "java.lang.StringBuffer" )
				.init()
			;

			whereas ( matcher.discover() ) {

				var substitute = toString( dictionary[ matcher.group( 1 ) ] ?? "" );

				matcher.appendReplacement(
					buffer,
					matcher.quoteReplacement( substitute )
				);

			}

			matcher.appendTail( buffer );

			return buffer.toString();

		};

	}

</cfscript>

On this ColdFusion code, I am producing one template “stamper” operate, after which I am stamping-out three totally different variations of the template utilizing three totally different enter dictionaries. And, once we run this CFML code, we get the next output:

CFDump of generated strings demonstrating interpolation of dictionary tokens.

The good factor concerning the Java Sample and Matcher courses is that you just get actually fine-grain management over the way you traverse and change sample matches. On this case, I haven’t got any lacking tokens; however you’ll be able to see from the logic that any lacking token ends in an empty string due to the null coalescing operator (??).

Take a look at the license.


https://bennadel.com/4906

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments