I have the need to insert a small piece of data - for example a telephone number retrieved from a .NET session variable - into content. I tried doing this by using a User Control macro but when inserting the the macro into the RTE the CMS decided to use a <div>. I don't want the phone number to be in a block level element, in fact I want it to be inside a <p> tag. How do I change the this macro <div> to something better, like a <span> ?
Should I even be using a macro to do this? Is there a better way to insert snippets of data like this into content?
I never found a really good, simple, way of doing that. As you mention, the macros don't work well when trying to write inline text. What I would tend to do is to make some sort of syntax for inserting text snippets like {{phoneNumber}} and then either do some replacing in the template, or maybe even hook in a httpModule to parse the entire html output for placeholders, and inserting the correct data.
So the solution depends a bit on what sort of development you want to be doing.
I don't know how you can change the <div> into something else. Inserting the macro in the rte is one way of doing this, but you can also make an xslt macro that both reads the session variable (the Umbraco.Library xslt extension has a Session method) and inserts the text in the page. Then put the macro in the template. This way you have full control over the markup. Let me know if you need more help on how to do this. About the <div> added by the rte you can maybe do some research in the folder umbraco\plugins\tinymce3 or umbraco_client\tinymce3\plugins\umbracomacro.
I've just made a test in umbraco 4.5.1 and I don't see a div around the inserted macro's content. I did try with an xslt macro though. Maybe worth a try!
bfi - sorry I wasn't clear.. the macro itself is represented by a div element in the rte, which is annoying because when I insert a macro within a paragraph tag tinymce will close the paragraph tag as it's trying to hold my hand and make me adhere to the proper standards. I've done a bit of Googling and it appears I can't change Umbraco to use a different tag to represent macros (or it's not straightfoward to do!). I may have a look at an xslt macro though, so thanks for that.
Morten - inserting {{telephone}} in the content might work for me, and I think my client would be happy to write that wherever they needed the telephone number. Where, within the Umbraco site structure, would I have to write my replacement code? I'd rather not have to create an httpModule if possible.
Thanks for the quick reply. I actually want to do this site wide, so don't want to have to put the replacement xslt code in each and every template. Do you know if this is possible?
Actually decided to do the token replacement in a Global.asax file. Do you happen to know of a good way to check if the HttpRequest is for the front-end Umbraco site and not the back-end admin area? Bearing in mind the umbraco folder has been renamed (ie. I don't want to rely on determining if it's the admin area via the URL)
Superb idea, I can use the "umbracoPath" element in web.config.
Seems to work a treat. I'm checking if the value of that umbracoPath element is at the start of the Request.Url.AbsolutePath variable using indexOf(). If it is I don't do the text replacement because I know the request is for the admin area. Sweet.
This is something I have been trying to figure out how to do also - though in my case the data would be pulled from Dictionary Items. Is there any chance you might be able to share some snippets of your Global.ascx code?
Great stuff. I will need to figure out if it would work with my circumstance.
What I want to do is to be able to use the "Dictionary" like a repository of "constants" for the website (so we have a single place to update a value across the website - like "number of customers", which might be 200,000 today, but next month is 250,000, or whatever)... so inside the RTE or templates, we could put in something like [*NumCustomers] , and when it is displayed on the website, it would dynamically replace "[*NumCustomers]" with the current number from the Dictionary, using the current culture, and the key "NumCustomers"...
Your inline macro thing looks great - except that it seems I wouldn't be able to pass in the dictionary key... right? So I would in essence have to create a different macro for each possible "constant"...? (Probably not ideal)
The shortcodes package operates the way I would like, but I will need to figure out how to program a REST extension (something I haven't done before) and right now the site is version 4.6...
I appreciate you pointing me towards these resources, though.
I think you could do just one macro, but then you would do a TinyMCE snippet for each dictionary key, where it is defined as a parameter to the macro in the snippet.
Another option would be to take a looke at the source for Shortcodes, and take a look at the http module that does the replacing. You might be able to "roll your own" specifically for your needs. (Or maybe it's already in there, but just no shown in the samples)
Inserting small piece of data into content
Hi all,
I'm new to Umbraco so please be gentle!
I have the need to insert a small piece of data - for example a telephone number retrieved from a .NET session variable - into content. I tried doing this by using a User Control macro but when inserting the the macro into the RTE the CMS decided to use a <div>. I don't want the phone number to be in a block level element, in fact I want it to be inside a <p> tag. How do I change the this macro <div> to something better, like a <span> ?
Should I even be using a macro to do this? Is there a better way to insert snippets of data like this into content?
Many Thanks,
Alex
I never found a really good, simple, way of doing that. As you mention, the macros don't work well when trying to write inline text. What I would tend to do is to make some sort of syntax for inserting text snippets like {{phoneNumber}} and then either do some replacing in the template, or maybe even hook in a httpModule to parse the entire html output for placeholders, and inserting the correct data.
So the solution depends a bit on what sort of development you want to be doing.
Hi Alex,
I don't know how you can change the <div> into something else. Inserting the macro in the rte is one way of doing this, but you can also make an xslt macro that both reads the session variable (the Umbraco.Library xslt extension has a Session method) and inserts the text in the page. Then put the macro in the template. This way you have full control over the markup. Let me know if you need more help on how to do this. About the <div> added by the rte you can maybe do some research in the folder umbraco\plugins\tinymce3 or umbraco_client\tinymce3\plugins\umbracomacro.
I've just made a test in umbraco 4.5.1 and I don't see a div around the inserted macro's content. I did try with an xslt macro though. Maybe worth a try!
Thanks for your speedy responses!
bfi - sorry I wasn't clear.. the macro itself is represented by a div element in the rte, which is annoying because when I insert a macro within a paragraph tag tinymce will close the paragraph tag as it's trying to hold my hand and make me adhere to the proper standards. I've done a bit of Googling and it appears I can't change Umbraco to use a different tag to represent macros (or it's not straightfoward to do!). I may have a look at an xslt macro though, so thanks for that.
Morten - inserting {{telephone}} in the content might work for me, and I think my client would be happy to write that wherever they needed the telephone number. Where, within the Umbraco site structure, would I have to write my replacement code? I'd rather not have to create an httpModule if possible.
Any ideas on the {{tag}} replacement stuff? Has anybody written this before for Umbraco?
Well I'll try to answer that, hopefully Morten won't mind :) You can make an xslt macro where you read the text that contains the {{telephone}} string
you make another var that holds the value of the phone number
then replace {{telephone}} with $phoneNumber
Let me know if this works.
Thanks for the quick reply. I actually want to do this site wide, so don't want to have to put the replacement xslt code in each and every template. Do you know if this is possible?
Am I going to have to use an httpModule, or is there any other way?
Actually decided to do the token replacement in a Global.asax file. Do you happen to know of a good way to check if the HttpRequest is for the front-end Umbraco site and not the back-end admin area? Bearing in mind the umbraco folder has been renamed (ie. I don't want to rely on determining if it's the admin area via the URL)
The umbraco folder name is actually in the web.config and needs to be changed if you move it, so you could rely on that just fine.
Superb idea, I can use the "umbracoPath" element in web.config.
Seems to work a treat. I'm checking if the value of that umbracoPath element is at the start of the Request.Url.AbsolutePath variable using indexOf(). If it is I don't do the text replacement because I know the request is for the admin area. Sweet.
Thanks you all for your help!
Alex,
This is something I have been trying to figure out how to do also - though in my case the data would be pulled from Dictionary Items. Is there any chance you might be able to share some snippets of your Global.ascx code?
Thanks so much!
Heather
There are a couple of ways to do this now.
One is to use the shortcodes package.
Another is to take the approach that I did here: Using inline macros in the Umbraco editor
Hope this is useful :-)
Thanks, Morten,
Great stuff. I will need to figure out if it would work with my circumstance.
What I want to do is to be able to use the "Dictionary" like a repository of "constants" for the website (so we have a single place to update a value across the website - like "number of customers", which might be 200,000 today, but next month is 250,000, or whatever)... so inside the RTE or templates, we could put in something like [*NumCustomers] , and when it is displayed on the website, it would dynamically replace "[*NumCustomers]" with the current number from the Dictionary, using the current culture, and the key "NumCustomers"...
Your inline macro thing looks great - except that it seems I wouldn't be able to pass in the dictionary key... right? So I would in essence have to create a different macro for each possible "constant"...? (Probably not ideal)
The shortcodes package operates the way I would like, but I will need to figure out how to program a REST extension (something I haven't done before) and right now the site is version 4.6...
I appreciate you pointing me towards these resources, though.
I think you could do just one macro, but then you would do a TinyMCE snippet for each dictionary key, where it is defined as a parameter to the macro in the snippet.
Another option would be to take a looke at the source for Shortcodes, and take a look at the http module that does the replacing. You might be able to "roll your own" specifically for your needs. (Or maybe it's already in there, but just no shown in the samples)
is working on a reply...