Mental model for how Templates with Functions get "run"

This is a question more for people like @Zackster or @micstriit – I’m curious to understand what kind of mental model I should have for a CFML template with Functions defined within it. For something like a .cfc that is instantiated and cached in memory, the persistence story seems more intuitive - it’s just an allocated class with functions bound to it and some data mixed in (probably not totally accurate, but that’s the story in my head).

Now, take something like an <cfinclude template="test.cfm">, where test.cfm includes a set of function() {} declarations. I understand that the CFML template is compiled to byte code; so, it’s not like the CFML and the functions are be compiled over-and-over every time the template is being included. But, I don’t have an instinct for what the “overhead” of the template include actually entails?

When I include the template, are the Functions being “re-declared” or is the underlying Function class being “re-instantiated”? Or, is the same Function instance basically being used every time that template executes?

I guess I’m trying to better understand the performance / overhead gap between a CFC and something like a <cfmodule> that has similar(ish) encapsulation of CFML + Functions.

Hope that’s clear - I just want to better flesh out my mental model for some of this stuff.

All functions UDFs go into the variables scope, whether in cfm or cfc

(script based cfc’s get wrapped in a <cfscript> tag to be compiled)

Conceptually and in practice, both a cfc and a cfm are just a bag of functions in the variables scope. cfml components just introduce access modifiers, properties etc

If you want to see how the sausage is made, find the class for your cfml files in the context/cfclasses folder and drop them into a java decomplier tool like

like i said, it’s all about the variables scope, so when you include a template, it more or less just goes and registers all the functions into the current variables scope, which variables scope depends if you include from a cfml template or a component (aka a mixin)

happy to elaborate more, ask away!

2 Likes

@Zackster ok cool, I appreciate the answer. That gives me a bit more relief. I’m working on a little side-project and one of the things I’m doing is including an “extensions” file (cfmlx.cfm) into a lot of places (all the places). It’s just a collection of Functions; and exactly what you are saying - it’s just appending them to the variables scope is my intention. My worry was that I had no idea who much compilation (or other) overhead was incurring. It seems very fast; but, I’m the only one using the project :stuck_out_tongue: