I did my best to find a solution without having to post here, but nothing I found seemed to work. I'm running Umbraco 4.
The Problem:
I have a macro that nicely diaplays the output of my XSLT file. I need to add functionaly to my XSLT that will be used in other XSLTs as well. I thought that creating an XSLT file that has my reusable code and then including it my main XSLT would be the way to go. How do I go about this?
Yes, I tried that before, but at least now I'm getting someting. Once I removed <xsl:param name="currentPage"/> from the xslt to be included it display the string of text I put in there as a test. The problem now is that the rest of my xslt won't display. Right now it seems it's an either or proposition.
Yes, I have seen that and found it utterly confusing thus unhelpful. It deals with media files and that adds a layer of complexity to the whole thing I don't need.
<!-- Don't change this, but add a 'contentPicker' element to --> <!-- your macro with an alias named 'source' --> <xsl:variable name="source" select="/macro/source"/>
<xsl:template match="/">
<!-- The fun starts here -->
This is the main xslt. <br /><br /> Insert sub xslt here: <br /><br /> <!-- what do I put in here to include sub.xslt? --> <!-- end sub.xslt --> </xsl:template>
<!-- Don't change this, but add a 'contentPicker' element to --> <!-- your macro with an alias named 'source' --> <xsl:variable name="source" select="/macro/source"/>
<xsl:template match="/">
<!-- The fun starts here --> This is the sub xslt.
Because of the way Umbraco serves the XML (using the $currentPage parameter) you will run into problems (as you have dicovered) when trying to reuse an existing macro in another macro.
You can however fix the problem by refactoring the original macro to work as another user of the common functionality, which you then include in that macro too. That way, you'll have e.g., a "functions.xslt" and two macros that include this... the $currentPage parameter should be excluded from the "functions.xslt" but remain in the others. As long as you're not using "functions.xslt" on its own, you shouldn't have problems with "undefined parameter" et.al.
Depending on the approach your original macro, you may need to do some restructuring - we can help you with that, no problem - just let us know.
I'm really at the end of my rope on this one. I could use a little more than hints in the right direction at this point. Spent like 10 hours trying to figure this out.
The RenderContent() method is hideous because you need to use a very old-style syntax for specifying the data needed - *and* you have to do some escaping to use it from within an XSLT file... that said, here's how I think you should be able to call your "sub" XSLT from your main file (note that you need to change the SUB_MACRO_ALIAS_HERE part to the alias of the macro you're trying to include):
I saw the new post and wanted to check something: In the other post it sounds like you're using the filename (e.g. "sub.xslt") where you should be using the macro's alias (usually the same as the filename, but without ".xslt") - which one did you use?
Other than that, it's pretty hard to say what else may be causing your macro not to work...
Reusing XSLT in Another XSLT
Hi,
I did my best to find a solution without having to post here, but nothing I found seemed to work. I'm running Umbraco 4.
The Problem:
I have a macro that nicely diaplays the output of my XSLT file. I need to add functionaly to my XSLT that will be used in other XSLTs as well. I thought that creating an XSLT file that has my reusable code and then including it my main XSLT would be the way to go. How do I go about this?
Thanks.
Hi Kory,
You can use includes in XSLT, e.g. <xsl:include href="uri-of-file-to-include"/>.
Yes, I tried that before, but at least now I'm getting someting. Once I removed <xsl:param name="currentPage"/> from the xslt to be included it display the string of text I put in there as a test. The problem now is that the rest of my xslt won't display. Right now it seems it's an either or proposition.
Hi Kory,
Check out this article on Chriztian's blog : <xsl:include> or <xsl:import>
Hope it helps
//fuji
Yes, I have seen that and found it utterly confusing thus unhelpful. It deals with media files and that adds a layer of complexity to the whole thing I don't need.
OK, in case I'm not being specific enough. Here is the code to my main xslt:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> ]>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:tagsLib="urn:tagsLib" xmlns:BlogLibrary="urn:BlogLibrary"
exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets tagsLib BlogLibrary ">
<xsl:output method="xml" omit-xml-declaration="yes" />
<xsl:param name="currentPage"/>
<!-- Don't change this, but add a 'contentPicker' element to -->
<!-- your macro with an alias named 'source' -->
<xsl:variable name="source" select="/macro/source"/>
<xsl:template match="/">
<!-- The fun starts here -->
This is the main xslt.
<br /><br />
Insert sub xslt here:
<br /><br />
<!-- what do I put in here to include sub.xslt? -->
<!-- end sub.xslt -->
</xsl:template>
</xsl:stylesheet>
... and this is what I want to include:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> ]>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:tagsLib="urn:tagsLib" xmlns:BlogLibrary="urn:BlogLibrary"
exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets tagsLib BlogLibrary ">
<xsl:output method="xml" omit-xml-declaration="yes" />
<xsl:param name="currentPage"/>
<!-- Don't change this, but add a 'contentPicker' element to -->
<!-- your macro with an alias named 'source' -->
<xsl:variable name="source" select="/macro/source"/>
<xsl:template match="/">
<!-- The fun starts here -->
This is the sub xslt.
</xsl:template>
</xsl:stylesheet>
How do I do it?
Hi Kory,
Because of the way Umbraco serves the XML (using the $currentPage parameter) you will run into problems (as you have dicovered) when trying to reuse an existing macro in another macro.
You can however fix the problem by refactoring the original macro to work as another user of the common functionality, which you then include in that macro too. That way, you'll have e.g., a "functions.xslt" and two macros that include this... the $currentPage parameter should be excluded from the "functions.xslt" but remain in the others. As long as you're not using "functions.xslt" on its own, you shouldn't have problems with "undefined parameter" et.al.
Depending on the approach your original macro, you may need to do some restructuring - we can help you with that, no problem - just let us know.
/Chriztian
I'm really at the end of my rope on this one. I could use a little more than hints in the right direction at this point. Spent like 10 hours trying to figure this out.
I found this article, which seems to describe exactly what I need, but I couldn't get it to work either. http://en.wikibooks.org/wiki/Umbraco/Reference/umbraco.library/RenderMacroContent
Could someone take a look at this, and tell me if it's outdated info and what do I need to get it to work. Thanks.
Hi Kory,
The RenderContent() method is hideous because you need to use a very old-style syntax for specifying the data needed - *and* you have to do some escaping to use it from within an XSLT file... that said, here's how I think you should be able to call your "sub" XSLT from your main file (note that you need to change the SUB_MACRO_ALIAS_HERE part to the alias of the macro you're trying to include):
Hope it helps - otherwise, come back...
/Chriztian
Thanks for that, but it doesn't seem to work.
I repleaced SUB_MACRO_ALIAS_HERE with the name of the macro I'm trying to include sub.xslt, but what I get is this displayed on the page: <Macro: (,)>
Any ideas?
Ah sorry,my bad — looks like there's a runat="server" attribute missing there... so:
/Chriztian
Sorry, still no cigar. I get the same thing.
Hi Kory,
I saw the new post and wanted to check something: In the other post it sounds like you're using the filename (e.g. "sub.xslt") where you should be using the macro's alias (usually the same as the filename, but without ".xslt") - which one did you use?
Other than that, it's pretty hard to say what else may be causing your macro not to work...
/Chriztian
Holy shit!!! That worked. Chriztian, you are the shiznitz. Thanks a bunch.
is working on a reply...