You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@freemarker.apache.org by Christoph Rüger <c....@synesty.com> on 2017/02/23 14:14:06 UTC
Feature question FM2 / FM3: Hook into loops
We would like to hook into loops (<#list>) to e.g. count the number of
iterations and react based on some business conditions. For example stop
with an Exception after 10k iterations.
I would think about some kind of callback which we can register which is
called by freemarker inside <#list> iterations, so that our callback code
can do whatever is needed.
Is something like that possible already (haven't checked code yet) or
planned for FM3?
Thanks
Christoph
--
Synesty GmbH
Moritz-von-Rohr-Str. 1a
07745 Jena
Tel.: +49 3641 559649
Fax.: +49 3641 5596499
Internet: http://synesty.com
Geschäftsführer: Christoph Rüger
Unternehmenssitz: Jena
Handelsregister B beim Amtsgericht: Jena
Handelsregister-Nummer: HRB 508766
Ust-IdNr.: DE287564982
RE: Feature question FM2 / FM3: Hook into loops
Posted by Mickel Daelmans | Add to Favorites <md...@addtofavorites.nl>.
Something like http://freemarker.org/docs/ref_directive_list.html#autoid_107 ?
Or use your own iterator:
<#assign i = 0 />
<#list items as item>
<#assign i = i + 1 />
<#if i == 10000>
//break / stop / throw exception
</#if>
//do stuff in loop
</#list>
Mickel Daelmans
Developer
Goeman Borgesiuslaan 77
3515 ET Utrecht
T. 030-7551560
W. www.addtofavorites.nl
Alles weten over transactionele e-mail?
Volg onze mailroad pagina op LinkedIn
===
De inhoud van deze e-mail, inclusief bijlagen, is vertrouwelijk en enkel bestemd voor de geadresseerde. Indien dit bericht niet voor u is bestemd, verzoeken wij u het te vernietigen, de inhoud daarvan op geen enkele wijze te gebruiken of te openbaren en direct contact met ons op te nemen. Op al onze werkzaamheden zijn onze Algemene Voorwaarden van toepassing, waarin een aansprakelijkheidsbeperking is opgenomen. Onze Algemene Voorwaarden worden op verzoek toegezonden. Add to Favorites B.V. is gevestigd te Utrecht (KvK Utrecht nr. 17228639).
-----Oorspronkelijk bericht-----
Van: Christoph Rüger [mailto:c.rueger@synesty.com]
Verzonden: donderdag 23 februari 2017 15:14
Aan: Freemarker dev Mailinglist
Onderwerp: Feature question FM2 / FM3: Hook into loops
We would like to hook into loops (<#list>) to e.g. count the number of iterations and react based on some business conditions. For example stop with an Exception after 10k iterations.
I would think about some kind of callback which we can register which is called by freemarker inside <#list> iterations, so that our callback code can do whatever is needed.
Is something like that possible already (haven't checked code yet) or planned for FM3?
Thanks
Christoph
--
Synesty GmbH
Moritz-von-Rohr-Str. 1a
07745 Jena
Tel.: +49 3641 559649
Fax.: +49 3641 5596499
Internet: http://synesty.com
Geschäftsführer: Christoph Rüger
Unternehmenssitz: Jena
Handelsregister B beim Amtsgericht: Jena
Handelsregister-Nummer: HRB 508766
Ust-IdNr.: DE287564982
Re: Feature question FM2 / FM3: Hook into loops
Posted by Christoph Rüger <c....@synesty.com>.
Thanks, good stuff.
2017-02-24 6:29 GMT+01:00 Jacques Le Roux <ja...@les7arts.com>:
> +1 for timeout
>
> Jacques
>
>
>
> Le 24/02/2017 à 01:12, Daniel Dekany a écrit :
>
>> There's a hidden feature in FreeMarker 2 that allows templates be
>> interrupted with Thread.interrupt(). This can be used to limit the
>> template execution time (one thread processes the template, the other
>> waits for it to finish with a timeout, and then interrupts the
>> template processing thread). To see this in action, go to
>> http://freemarker-online.kenshoo.com/ and enter some very long loop.
>> The processing will be aborted with timeout. The related part of the
>> source code is around here:
>> https://github.com/kenshoo/freemarker-online/blob/master/src
>> /main/java/com/kenshoo/freemarker/services/FreeMarkerService.java#L123
>>
>> That's the closes I can think of right now. But I'm open to ideas,
>> because this used to be concern if you allow users to write templates.
>>
>> The problem with limiting #list iterations is that you can nest
>> #list-s into each other, so it's easy to square, cube, etc. your
>> iteration limit. (There are other fun ways of doing infinitely running
>> templates too, like with recursion. That's why simply measuring how
>> long the template is running is certainly the most robust solution.)
>>
>> If you have some collections that you know shouldn't be fully listed,
>> then perhaps you should wrap those into your own TemplateSequenceModel
>> implementation.
>>
>>
>> Thursday, February 23, 2017, 3:14:06 PM, Christoph Rüger wrote:
>>
>> We would like to hook into loops (<#list>) to e.g. count the number of
>>> iterations and react based on some business conditions. For example stop
>>> with an Exception after 10k iterations.
>>>
>>> I would think about some kind of callback which we can register which is
>>> called by freemarker inside <#list> iterations, so that our callback code
>>> can do whatever is needed.
>>>
>>> Is something like that possible already (haven't checked code yet) or
>>> planned for FM3?
>>>
>>> Thanks
>>> Christoph
>>>
>>>
>
--
Christoph Rüger, Geschäftsführer
Synesty <https://synesty.com/> - Anbinden und Automatisieren ohne
Programmieren - Automatisierung, Schnittstellen, Datenfeeds
Xing: https://www.xing.com/profile/Christoph_Rueger2
LinkedIn: http://www.linkedin.com/pub/christoph-rueger/a/685/198
--
Synesty GmbH
Moritz-von-Rohr-Str. 1a
07745 Jena
Tel.: +49 3641 559649
Fax.: +49 3641 5596499
Internet: http://synesty.com
Geschäftsführer: Christoph Rüger
Unternehmenssitz: Jena
Handelsregister B beim Amtsgericht: Jena
Handelsregister-Nummer: HRB 508766
Ust-IdNr.: DE287564982
Re: Feature question FM2 / FM3: Hook into loops
Posted by Jacques Le Roux <ja...@les7arts.com>.
+1 for timeout
Jacques
Le 24/02/2017 � 01:12, Daniel Dekany a �crit :
> There's a hidden feature in FreeMarker 2 that allows templates be
> interrupted with Thread.interrupt(). This can be used to limit the
> template execution time (one thread processes the template, the other
> waits for it to finish with a timeout, and then interrupts the
> template processing thread). To see this in action, go to
> http://freemarker-online.kenshoo.com/ and enter some very long loop.
> The processing will be aborted with timeout. The related part of the
> source code is around here:
> https://github.com/kenshoo/freemarker-online/blob/master/src/main/java/com/kenshoo/freemarker/services/FreeMarkerService.java#L123
>
> That's the closes I can think of right now. But I'm open to ideas,
> because this used to be concern if you allow users to write templates.
>
> The problem with limiting #list iterations is that you can nest
> #list-s into each other, so it's easy to square, cube, etc. your
> iteration limit. (There are other fun ways of doing infinitely running
> templates too, like with recursion. That's why simply measuring how
> long the template is running is certainly the most robust solution.)
>
> If you have some collections that you know shouldn't be fully listed,
> then perhaps you should wrap those into your own TemplateSequenceModel
> implementation.
>
>
> Thursday, February 23, 2017, 3:14:06 PM, Christoph R�ger wrote:
>
>> We would like to hook into loops (<#list>) to e.g. count the number of
>> iterations and react based on some business conditions. For example stop
>> with an Exception after 10k iterations.
>>
>> I would think about some kind of callback which we can register which is
>> called by freemarker inside <#list> iterations, so that our callback code
>> can do whatever is needed.
>>
>> Is something like that possible already (haven't checked code yet) or
>> planned for FM3?
>>
>> Thanks
>> Christoph
>>
Re: Feature question FM2 / FM3: Hook into loops
Posted by Daniel Dekany <dd...@freemail.hu>.
There's a hidden feature in FreeMarker 2 that allows templates be
interrupted with Thread.interrupt(). This can be used to limit the
template execution time (one thread processes the template, the other
waits for it to finish with a timeout, and then interrupts the
template processing thread). To see this in action, go to
http://freemarker-online.kenshoo.com/ and enter some very long loop.
The processing will be aborted with timeout. The related part of the
source code is around here:
https://github.com/kenshoo/freemarker-online/blob/master/src/main/java/com/kenshoo/freemarker/services/FreeMarkerService.java#L123
That's the closes I can think of right now. But I'm open to ideas,
because this used to be concern if you allow users to write templates.
The problem with limiting #list iterations is that you can nest
#list-s into each other, so it's easy to square, cube, etc. your
iteration limit. (There are other fun ways of doing infinitely running
templates too, like with recursion. That's why simply measuring how
long the template is running is certainly the most robust solution.)
If you have some collections that you know shouldn't be fully listed,
then perhaps you should wrap those into your own TemplateSequenceModel
implementation.
Thursday, February 23, 2017, 3:14:06 PM, Christoph R�ger wrote:
> We would like to hook into loops (<#list>) to e.g. count the number of
> iterations and react based on some business conditions. For example stop
> with an Exception after 10k iterations.
>
> I would think about some kind of callback which we can register which is
> called by freemarker inside <#list> iterations, so that our callback code
> can do whatever is needed.
>
> Is something like that possible already (haven't checked code yet) or
> planned for FM3?
>
> Thanks
> Christoph
>
--
Thanks,
Daniel Dekany