Using inline macros in the Umbraco editor
Published on 28. July 2011 inI've seen the question asked a lot of times on the forum, and it's not always easy to give a good answer to it. "How do I create a macro that works inline in the editor". By default when inserting Macros in the editor, it will force a <p> ir <div> tag to be created around it, which means that the macro will always be a block of content.
In some cases however, it is nice to be able to insert a macro that just creates a single word or phrase inside a sentence. It could be a price of a product, a user specific property, or whatever you might need.
So I came up with the below solution. Of course it has benefits
and drawbacks. In short:
Benefit:
- Inline macros
Drawbacks:
- You do not get a nice way of entering data for macro
parameters
- The preview snippet is static from the moment you insert it in
the editor
At the bottom you will find a link to a complete zipped umbraco install that has been set up with this, so you can have a play with it. It's using SQL CE, so should be easy to get up and running.
If you can live with this, then read on :-). First let's take a
look at what the result will look like. When you insert a snippet
in the editor it will look like this:

In this case my snippet will write the name of the page, and
when rendered in the frontend it will look like this:

So, how does this work? There a three parts that we need to set
up
1. Create a previewable macro
2. Create an aspx page that renders the preview snippet for the
editor
3. Configure the TinyMCE editor to use the snippets with the
Templates module
Step 1: Previewable macro
Create a macro with a "preview" parameter. In this case I'm
creating an xslt macro, but works just fine with razor or
usercontrols as well.

Make the macro aware of a preview state

Step 2: Snippet page
Create an aspx page that will render the macro snippet as well
as the macro in preview mode.

My snippet page looks like this. I added the snippet class to a
span to make it appear green in the editor. Be aware that this is
all in one line to avoid too many spaces in the output when
rendered.

Now, for the TinyMCE to be able to reach the page, we need to
exclude it from the urls that Umbraco handles. We do this is by
adding the folder to the web.config:

Step 3: Enable the templates module in TinyMCE
First we add a few thing to the tinyMceConfig.config file.
The command

The plugin
![]()
Allow the umbraco:macro tag as valid html
![]()
And add configuration for the templates, so the editor knows
which snippets it can get and from which urls.

Then we disable the tidy function in umbracoSettings.config,
because that will throw an error when it sees the umbraco macro
tag.
![]()
Now, go to the Richtext Editor datatype, and enable the newly
added templates module.

I also added my own editorStyles stylesheet, but that is standard umbraco stuff :-)
And that is it. Now the macro can be inserted to your text without having to be in a <p> tag. Neato.
I hope this is useful for you. Let me know if you can see any other drawbacks than the ones that I mentioned at the beginning.
Get the demosite here: InlineMacroDemo.zip
Related posts
Comments
Comments disabled
Comments are disabled for this post.

On 01. August 2011 by Petr
Hi,
thank you for interested article.
What about integration with ShortCodes?
http://our.umbraco.org/projects/developer-tools/shortcodes
On 01. August 2011 by Morten Bock
You could use the snippets to generate shortcodes as well. This was mostly an alternative way of getting out of the box umbraco functionality to behave a bit differently. It would probably be very handy to create snippets for shortcode method calls with parameters, since I imagine that is something that content editors find difficult to do.
On 13. September 2011 by Nigel Wilson
Hey
Love the concept and thought it would work for a current site I am working on, however from reading and trying the above I have come to the conclusion that it will work fine for static information. I have the need to have an inline macro with mulitple parameters (link text, page to link to, link title, etc) and so do not think this will work for me.
Are you able to please confirm if I have correctly understood, or if I have misread it.
Thanks
Nigel
On 15. September 2011 by Morten Bock
Hi Nigel.
Yes, you are correct. This solution does not have an answer to macros that need parameters. You might have better luck with the Shortcode package that Petr mentioned in an earlier comment. Or you could mic the two. Use the snippet function to generate the template, and let the user fill in the needed parameters for a rest method: [YourAlias:text=thisisthetext&pageid=1234]
But it will not give you the nice options for a media picker, or anything like that.