[xwiki-users] Interacting between groovy & velocity script
Hi everybody! I need help to organize interacting beetween groovy and velocity script on a page. I need to transmit a value of a parameter from groovy script - to velocity script (both scripts placed on one page). I know theory, that need to use xcontext: xcontext.put("variable","value") inside groovy script and $xcontext.get("variable") inside velocity script but in my situation - this doesn't work ... I cannot understand, what object in my groovy script representing xcontext object ... As a groovy script, I am using "Title Post Processing Using Groovy" based on event listener technology, originally taken from here http://extensions.xwiki.org/xwiki/bin/view/Extension/Title+Post+Processing+U.... I only made small changes on this script (calculates title according to my properties). It works perfectly. Also, I inserted on same page (where event listener script was placed) one more script - velocity script (below groovy script). My idea - when an event was happen - should execute 2 scripts. And all is working perfectly! Execute and "Title Post Processing Using Groovy" and my own velocity-script - this part working without any problems. I need to make small next step - to transmit a value of a parameter (page name where an event was happens) from groovy to velocity. But if I try to write inside this groovy script: xcontext.put("eventsource",source.getFullName()) - event listener stops working ... (looks like some error) If I trying to use (as a xcontext) - crtContext (variable which defines some context-related object in this script) - event listener working, but when I trying inside velocity-script to get: $xcontext.get("eventsource") - value is empty ... As a result - my question is - how need to use xcontext.put(... in this groovy script of event listenter which I am using and how I can get xcontext variable in a below-placed velocity script (which is also executes)? If will be small example - will be perfectly, because I am not groovy- or java-programmer and have some difficulties of understanding object-oreiented organization of xwiki ... -- Thanks beforehand! Eugen Colesnicov -- View this message in context: http://xwiki.475771.n2.nabble.com/Interacting-between-groovy-velocity-script... Sent from the XWiki- Users mailing list archive at Nabble.com.
On Mon, Dec 19, 2011 at 1:01 PM, Eugen Colesnicov <[email protected]> wrote:
Hi everybody!
I need help to organize interacting beetween groovy and velocity script on a page. I need to transmit a value of a parameter from groovy script - to velocity script (both scripts placed on one page).
I know theory, that need to use xcontext: xcontext.put("variable","value") inside groovy script and $xcontext.get("variable") inside velocity script
but in my situation - this doesn't work ... I cannot understand, what object in my groovy script representing xcontext object ...
As a groovy script, I am using "Title Post Processing Using Groovy" based on event listener technology, originally taken from here http://extensions.xwiki.org/xwiki/bin/view/Extension/Title+Post+Processing+U.... I only made small changes on this script (calculates title according to my properties). It works perfectly.
Also, I inserted on same page (where event listener script was placed) one more script - velocity script (below groovy script). My idea - when an event was happen - should execute 2 scripts. And all is working perfectly! Execute and "Title Post Processing Using Groovy" and my own velocity-script - this part working without any problems.
What change did you make exactly ? Could you paste your new scripts ?
I need to make small next step - to transmit a value of a parameter (page name where an event was happens) from groovy to velocity. But if I try to write inside this groovy script: xcontext.put("eventsource",source.getFullName()) - event listener stops working ... (looks like some error)
What error do you get exactly ?
If I trying to use (as a xcontext) - crtContext (variable which defines some context-related object in this script) - event listener working, but when I trying inside velocity-script to get: $xcontext.get("eventsource") - value is empty ...
As a result - my question is - how need to use xcontext.put(... in this groovy script of event listenter which I am using and how I can get xcontext variable in a below-placed velocity script (which is also executes)?
If will be small example - will be perfectly, because I am not groovy- or java-programmer and have some difficulties of understanding object-oreiented organization of xwiki ...
-- Thanks beforehand! Eugen Colesnicov
-- View this message in context: http://xwiki.475771.n2.nabble.com/Interacting-between-groovy-velocity-script... Sent from the XWiki- Users mailing list archive at Nabble.com. _______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Thomas Mortagne
This works for me: ----------8<---------- {{groovy}} xcontext.put("foo", "bar") {{/groovy}} {{velocity}} Velocity: $xcontext.foo {{/velocity}} ---------->8---------- I tested on a 3.4 snapshot but it should work on older versions too. Hope this helps, Marius On Mon, Dec 19, 2011 at 2:01 PM, Eugen Colesnicov <[email protected]> wrote:
Hi everybody!
I need help to organize interacting beetween groovy and velocity script on a page. I need to transmit a value of a parameter from groovy script - to velocity script (both scripts placed on one page).
I know theory, that need to use xcontext: xcontext.put("variable","value") inside groovy script and $xcontext.get("variable") inside velocity script
but in my situation - this doesn't work ... I cannot understand, what object in my groovy script representing xcontext object ...
As a groovy script, I am using "Title Post Processing Using Groovy" based on event listener technology, originally taken from here http://extensions.xwiki.org/xwiki/bin/view/Extension/Title+Post+Processing+U.... I only made small changes on this script (calculates title according to my properties). It works perfectly.
Also, I inserted on same page (where event listener script was placed) one more script - velocity script (below groovy script). My idea - when an event was happen - should execute 2 scripts. And all is working perfectly! Execute and "Title Post Processing Using Groovy" and my own velocity-script - this part working without any problems.
I need to make small next step - to transmit a value of a parameter (page name where an event was happens) from groovy to velocity. But if I try to write inside this groovy script: xcontext.put("eventsource",source.getFullName()) - event listener stops working ... (looks like some error)
If I trying to use (as a xcontext) - crtContext (variable which defines some context-related object in this script) - event listener working, but when I trying inside velocity-script to get: $xcontext.get("eventsource") - value is empty ...
As a result - my question is - how need to use xcontext.put(... in this groovy script of event listenter which I am using and how I can get xcontext variable in a below-placed velocity script (which is also executes)?
If will be small example - will be perfectly, because I am not groovy- or java-programmer and have some difficulties of understanding object-oreiented organization of xwiki ...
-- Thanks beforehand! Eugen Colesnicov
-- View this message in context: http://xwiki.475771.n2.nabble.com/Interacting-between-groovy-velocity-script... Sent from the XWiki- Users mailing list archive at Nabble.com. _______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
Thomas Mortagne wrote
What change did you make exactly ? Could you paste your new scripts ?
What error do you get exactly ?
I Attached file with content of my page (contains 2 scripts groovy & velocity) http://xwiki.475771.n2.nabble.com/file/n7108420/Sandbox.pEventListener.txt Sandbox.pEventListener.txt groovy script ia same to http://extensions.xwiki.org/xwiki/bin/view/Extension/Title+Post+Processing+U.... Only small changes in new title definition (according to my own properties) and CRITICAL line: xcontext.put("eventsource",source.getFullName()) If I add this line to this script - event listener stops working ... (no any changes in a title). I cannot see any error (or I don't know how). I think, that it is error, because event listener stops working - no any changes in title of a modified page. I checked java log errors - also not there any errors ... Without this line: xcontext.put("eventsource",source.getFullName()) groovy script works perfectly and also works velocity script (cannot get value from xcontext, but works). -- Best regards Eugen Colesnicov -- View this message in context: http://xwiki.475771.n2.nabble.com/Interacting-between-groovy-velocity-script... Sent from the XWiki- Users mailing list archive at Nabble.com.
On Mon, Dec 19, 2011 at 4:02 PM, Eugen Colesnicov <[email protected]> wrote:
Thomas Mortagne wrote
What change did you make exactly ? Could you paste your new scripts ?
What error do you get exactly ?
I Attached file with content of my page (contains 2 scripts groovy & velocity) http://xwiki.475771.n2.nabble.com/file/n7108420/Sandbox.pEventListener.txt Sandbox.pEventListener.txt groovy script ia same to http://extensions.xwiki.org/xwiki/bin/view/Extension/Title+Post+Processing+U....
Only small changes in new title definition (according to my own properties) and CRITICAL line: xcontext.put("eventsource",source.getFullName())
If I add this line to this script - event listener stops working ... (no any changes in a title). I cannot see any error (or I don't know how). I think, that it is error, because event listener stops working - no any changes in title of a modified page. I checked java log errors - also not there any errors ...
Without this line: xcontext.put("eventsource",source.getFullName()) groovy script works perfectly and also works velocity script (cannot get value from xcontext, but works).
What do you mean exactly by velocity works ? What this script is doing is to register a java object created with groovy, your velocity will never be called when an event is received. Basically what happen: * when you view the page: a java listener is created and registered in groovy and your velocity is executed but onEvent is not called * when an event is received: onEvent of previously registred java object is called and nothing else, the script is not executed So if you need to do something when an event is received you need to do it fully in the onEvent method.
-- Best regards Eugen Colesnicov
-- View this message in context: http://xwiki.475771.n2.nabble.com/Interacting-between-groovy-velocity-script... Sent from the XWiki- Users mailing list archive at Nabble.com. _______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Thomas Mortagne
Thomas Mortagne wrote
... What this script is doing is to register a java object created with groovy, your velocity will never be called when an event is received.
Basically what happen: * when you view the page: a java listener is created and registered in groovy and your velocity is executed but onEvent is not called * when an event is received: onEvent of previously registred java object is called and nothing else, the script is not executed
So if you need to do something when an event is received you need to do it fully in the onEvent method.
Thanks Thomas! As I understand, I should include all my planned script actions inside groovy script (same as groovy script make changes in a title of source page)... According to this, I have another question. I should include ALL my actions inside this onEvent object? For example, can I have on another page something like macros (like velocity macros) and everytime, when I need (when onEvent is called) - call this macros from onEvent object and execute this? This is can be helpful if I need to repeat (when onEvent received) some actions (for example - change title not only for source page, but for all pages of a some class). If I can - it is possible to call velocity macros, or all like this procedures should be written on groovy? -- Best regards Eugen Colesnicov! -- View this message in context: http://xwiki.475771.n2.nabble.com/Interacting-between-groovy-velocity-script... Sent from the XWiki- Users mailing list archive at Nabble.com.
On Mon, Dec 19, 2011 at 7:33 PM, Eugen Colesnicov <[email protected]> wrote:
Thomas Mortagne wrote
... What this script is doing is to register a java object created with groovy, your velocity will never be called when an event is received.
Basically what happen: * when you view the page: a java listener is created and registered in groovy and your velocity is executed but onEvent is not called * when an event is received: onEvent of previously registred java object is called and nothing else, the script is not executed
So if you need to do something when an event is received you need to do it fully in the onEvent method.
Thanks Thomas! As I understand, I should include all my planned script actions inside groovy script (same as groovy script make changes in a title of source page)... According to this, I have another question. I should include ALL my actions inside this onEvent object? For example, can I have on another page something like macros (like velocity macros) and everytime, when I need (when onEvent is called) - call this macros from onEvent object and execute this? This is can be helpful if I need to repeat (when onEvent received) some actions (for example - change title not only for source page, but for all pages of a some class). If I can - it is possible to call velocity macros, or all like this procedures should be written on groovy?
There is some possible hack to call velocity script from groovy but would be a lot easier to do pure groovy actually. If you want to repeat a task you should add a groovy method, for example onEvent itself is a method you can call it with parameters and it also can return an object which is a lot better that wiki macro in this context. Wiki macro are really targeting display and are really not supposed to be use as methods. To summarize: yes I think you will have to do learn some groovy and after some time you will see that it's a lot better than velocity for a use case like that where you don't have anything to display. You can take a look at http://groovy.codehaus.org/Beginners+Tutorial which seems pretty nice. You should get started pretty quickly, groovy is really easy for someone who already did programming in any other language.
-- Best regards Eugen Colesnicov!
-- View this message in context: http://xwiki.475771.n2.nabble.com/Interacting-between-groovy-velocity-script... Sent from the XWiki- Users mailing list archive at Nabble.com. _______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Thomas Mortagne
participants (3)
-
Eugen Colesnicov -
Marius Dumitru Florea -
Thomas Mortagne