Is it possible to exit a Freemarker template? - exit

I'd like to be able to end the page's rendering from within an <#if> tag.
I'm aware of the <#stop> tag, but I'd prefer not to have to throw an exception in order for the page to stop rendering.

Here's a dirty trick for that: Put the whole content of the template inside a macro and then immediately call that macro. Inside the macro you can use <#return>.

Related

Embedded Crystal variables in templating

I’m new to Crystal (and never really used ruby) so apologies for the ignorance here! I've looked at the ecr docs but can't seem to find an answer there.
I’m looking at using Embedded Crystal for dynamic templates in Kemal. Can I confirm - can templates only render variables that are available in the scope of the call, or can one make method/function calls from within the template itself? I.E. is there any possibility/risk of being able to execute “malicious” crystal code from within a template (in this case malicious refers to I/O or file access etc)?
To take an example from the Kemal docs:
get "/:name" do |env|
name = env.params.url["name"]
render "src/views/hello.ecr"
end
In the view hello.ecr - is name the only item that will be available in the template, or could one call File.delete("./foo")from within the template for example?
A template is compiled into Crystal code, you can write any kind of code in there, like File.delete("./foo"), for example if you write <% File.delete("./foo") %> inside of your template.
If your worry is that name will contain code and that will somehow get executed, then don't worry, that's not going to happen. Dynamic runtime code execution in Crystal is not possible, so there's no way someone will inject malicious code into your templates.

Custom tag using the Cffunction Tag

I am trying to create a custom tag for cffunction for trying to reuse and not write cffunction again and again, I am using the following as:
<cffunction access="#attributes.access#" returntype="#attributes.return#" output="#attributes.output#" name="#attributes.method#">
but cfbuilder version 2 is giving me illegal attribute error, i am not sure, it is right or wrong, can i do like this
Thanks
No, you cannot use dynamic values (variables) in attributes of <cffunction>. Functions are pre-compiled and thus do not allow evaluation at runtime.
If your reason to wrap all functions is to catch errors, there are better approaches to do so, e.g. the onError handler.

jsRender template disappears after rendering

After I call render on my jsRender template, it seems to be consumed, and thus is removed from the DOM. This is frustrating as I have a page where the template needs to be rendered several times depending on user interaction.
console.log($('#tpl'));
$('#container').html($('#tpl').render(json));
console.log($('#tpl'));
The second console.log is an empty array, and I can confirm the template no longer exists using the DOM inspector and the exception that jsRender throws: Uncaught JsRender Error: Unknown template: "#tpl" -- the page must be reloaded to re-inject the template into the DOM.
How can I persist the jsRender template between renderings?
I'm still not sure why it has to be consumed and simply can't stay in the DOM after rendering the first time, but I found a workaround. If anybody knows the reason for removing the template from the DOM, I'm still interested.
Update: Actual answer (Thanks, Boris)
My template was within my #container element, so the html() method was of course overwriting it. Silly me.
Workaround Neat little trick anyway
Using this 'variant' example, I saved the template in a local variable. Then I call render on the variable name instead of the jQuery selector:
var tpl = $.templates('#tpl');
.
.
.
console.log(tpl);
$('#container').html(tpl.render(json));
console.log(tpl);
This has also managed to preserve the template across renderings.
I also had a similar problem today where I had two target divs and two script block templates in the body. My problem was that I hadn't closed the div element tags correctly (too much xaml) and the result was the second template was never rendered as it couldn't be found.
Here's a JsFiddle showing the correct usage (rather than the /> self-closing syntax):
http://jsfiddle.net/jgoldsmith/XvvPC/
Hope that helps someone else.

refactor cferror tag to application.cfc's onError()

How to translate
<cferror type="EXCEPTION" template="ErrorTemplate.cfm">
to an onError() function for Application.cfc? Notice that cferror create a var error struct for the template. Is there a quick and easy way to create the same error struct? How to pass that into ErrorTemplate.cfm using <cfinclude>?
I know onRequest() shares the variable scope with the target page, but does onError share variables scope too? If not, what shall I do? Stick it to the Form scope?
Thank you
According to http://livedocs.adobe.com/coldfusion/8/htmldocs/appFramework_15.html
Consider replacing cferror tags with an onError event method. If you
do not do so, put the cferror tags in the CFC initialization code.
According to http://www.coldfusionjedi.com/index.cfm/2007/12/5/The-Complete-Guide-to-Adding-Error-Handling-to-Your-ColdFusion-Application
What if you are using Application.cfc? Well one thing to remember is
that you can just as easily put CFERROR tags inside your
Application.cfc file. That is allowed, and I've done that before.
Why would you not just use onError in App.cfc to display a "Sorry and error occurred, we are working on it" type page?

How to pass an argument to a method on a template variable in Django?

I am using the lxml library to define a variable (category) in a view. lxml provides a method .get to retrieve custom attributes. I'd like to use it in the template like so:
{{ category.get("foo") }}
I know that when using template variables it is unnecessary to use parenthesis, but I get the following error:
{{ category.get "foo" }}
Could not parse the remainder: ' "foo"' from 'category.get "foo"'
I'm assuming that there is something wrong with my syntax but google has been no help. The django docs say that methods are looked up by using a .
You can't pass an argument to a callable attribute like this. Either pull the value in the view, or write a custom template tag to do it.
I agree with the philosophy of separating logic from design, but there's an exception. I am currently writing a get_image(height=xxx, width=xxx) method for a model. Clearly, it should be up to the template designer to decide the size of the image as the last stage. Though I suppose the correct thing to do is write a custom tag, but why restrict?
Here I wrote a hack to call a function and pass the arguments
http://www.sprklab.com/notes/13-passing-arguments-to-functions-in-django-template/