Possible to write a function similar to savecontent?


#1

I’d like to be able to write a function in a component that would basically take a code block as a parameter, similar to the way savecontent works, in that it wraps around a block of code. Is this possible with just vanilla Lucee, or would I need to dig into doing via Java? The usage might look like this:

mycomponent.execute {
   foo = [1,2,3,4,5]
   bar = foo.map(function(i) { return i * 2 })
   writeOutput(bar)
}

The gist is I’d like to be able to manipulate then execute the code block, possibly even returning a value from execute() as well. This is just a contrived example but I hope it helps. Thanks for any pointers or places to research how to go about this.


#2

I’m not quite clear on what you’re wanting to do. What kind of “output” are you wanting to capture? You can already run cfsavecontent from inside of script, but it doesn’t usually make as much sense in script since output is usually the remit of a function, not just the page output buffer that your tags dump into.

savecontent variable='local.foo' {
  echo( 'foo' );
  echo( 'bar' );
}

#3

Sorry for being unclear. I’m playing around with implementing a circuit breaker and I’d like to be able to wrap a code block with the breaker. The breaker will basically put the code block into a try…catch but do some other activities related to the breaker state. I’d like to be able to wrap any block with the breaker so at some point the wrapped code block will need to be executed. So if I did this, for example:

breaker {
   ( some code here that might throw an exception, like a database call )
}

I could get the resulting breaker state (open, closed, etc. ) after executing the enclosed code. I mentioned savecontent only because that is an existing function/tag that has similar behavior in that in encloses a block of code. Does that make any more sense, or did I just muddy the waters further?


#4

Hmm, I suppose I could also just pass a function to the breaker as well, if that is possible to do.


#5

Have you checked out Ben Nadel’s circuit breaker implementation for CFML? I’ve not used it but it might already have some of what you need.