# Run macro only on create or edit

**URL:** <https://forum.xwiki.org/t/run-macro-only-on-create-or-edit/2754>\
**Category:** Help / Discuss\
**Created:** [March 18, 2018, 11:50am UTC](https://forum.xwiki.org/t/run-macro-only-on-create-or-edit/2754 "2018-03-18T11:50:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrew\_florida](https://forum.xwiki.org/letter_avatar_proxy/v4/letter/a/5e9695/32.png) [@andrew\_florida](https://forum.xwiki.org/u/andrew_florida)\
**Post date:** [March 18, 2018, 11:50am UTC](https://forum.xwiki.org/t/run-macro-only-on-create-or-edit/2754/1 "2018-03-18T11:50:24Z")

</div>

Hello everyone.  
I want to achieve something like this, when user adds {{myMacro}}#BODY{{/myMacro}} to his page, I want to parse text inside tags (#BODY) and return some html in there, but the thing is, I wan’t to do this only when someone is editing or adding new page, not everytime the page is visited. Is it possible to achieve it using custom Macro?

Regards,  
Andrew

---

<div class="post-metadata">

**Author:** ![vmassol](https://forum.xwiki.org/user_avatar/forum.xwiki.org/vmassol/32/6_2.png) [@vmassol](https://forum.xwiki.org/u/vmassol)\
**Post date:** [March 18, 2018, 12:13pm UTC](https://forum.xwiki.org/t/run-macro-only-on-create-or-edit/2754/2 "2018-03-18T12:13:31Z")

</div>

Hi, yes, it’s possible and easy to do actually. In your wiki macro (see [http://www.xwiki.org/xwiki/bin/view/Documentation/DevGuide/Tutorials/WritingMacros/WikiMacroTutorial/](http://www.xwiki.org/xwiki/bin/view/Documentation/DevGuide/Tutorials/WritingMacros/WikiMacroTutorial/) for a tutorial) you’ll just need to test for the context action.

For example:

```auto
{{velocity}}
...
#if ($xcontext.action == 'edit')
   ... do something...
#else
   ... do something else...
#end
...
{{/velocity}}

```

---

<div class="post-metadata">

**Author:** ![andrew\_florida](https://forum.xwiki.org/letter_avatar_proxy/v4/letter/a/5e9695/32.png) [@andrew\_florida](https://forum.xwiki.org/u/andrew_florida)\
**Post date:** [March 18, 2018, 12:20pm UTC](https://forum.xwiki.org/t/run-macro-only-on-create-or-edit/2754/3 "2018-03-18T12:20:17Z")

</div>

Thank you for quick reply, but I don’t know if we are on the same page.  
On ‘edit’ or ‘create’ I want to run macro and save some html produced by it, then on view i wan’t to display this html which was produced before.

So, when one types

{{myMacro}}abcd{{/myMacro}}

And let’s say that myMacro reverse a string, then on page we will see:

dcba

But i wam’t myMacro function to run only once(while creating), not everytime someone is accessing page.

---

<div class="post-metadata">

**Author:** ![vmassol](https://forum.xwiki.org/user_avatar/forum.xwiki.org/vmassol/32/6_2.png) [@vmassol](https://forum.xwiki.org/u/vmassol)\
**Post date:** [March 18, 2018, 12:41pm UTC](https://forum.xwiki.org/t/run-macro-only-on-create-or-edit/2754/4 "2018-03-18T12:41:51Z")

</div>

Ok so indeed we were not talking about the same thing at all 🙂

You cannot really use macros for your need.

What I think would be better:

- Write an event listener and listen to `DocumentCreatingEvent` and `DocumentUpdatingEvent` events to be called when a page is being modified/created. See example at [http://www.xwiki.org/xwiki/bin/view/Documentation/DevGuide/Tutorials/WritingEventListenerTutorial/#HAddingcontenttopagesonsave](http://www.xwiki.org/xwiki/bin/view/Documentation/DevGuide/Tutorials/WritingEventListenerTutorial/#HAddingcontenttopagesonsave)
- Modify the XDOM (that’s the AST of the page) to modify the content of the page. See [http://rendering.xwiki.org/xwiki/bin/view/Main/WebHome](http://rendering.xwiki.org/xwiki/bin/view/Main/WebHome) and [http://rendering.xwiki.org/xwiki/bin/view/Main/GettingStarted#HModifyallLinkstobedisplayedinitalics](http://rendering.xwiki.org/xwiki/bin/view/Main/GettingStarted#HModifyallLinkstobedisplayedinitalics) (for ex).

You can write your listener as a wiki component or as a java component (recommended).

Hope it helps  
-Vincent

---

<div class="post-metadata">

**Author:** ![andrew\_florida](https://forum.xwiki.org/letter_avatar_proxy/v4/letter/a/5e9695/32.png) [@andrew\_florida](https://forum.xwiki.org/u/andrew_florida)\
**Post date:** [March 18, 2018, 12:47pm UTC](https://forum.xwiki.org/t/run-macro-only-on-create-or-edit/2754/5 "2018-03-18T12:47:50Z")

</div>

Awesome, thank you for your help!
