Accessing methods from a class from a .NET assembly Template Building Block
If you have a Template Building Block that contains a .NET assembly, you can access certain of its class methods from your XSLT template by referring to the Template Building Block.
Procedure
- Create or open a Template Building Block that contains a .NET assembly.
- In the .NET assembly, create a class that implements the interface
IFunctionSource. The class must implement at least anInitializemethod that takes an engine and a package as parameters. Refer to the TOM.NET API reference documentation for more information. - Save and close your .NET assembly and update your Template Building Block.
- Open your XSLT template for editing.
- At the top of your template, add the following line:
<?XsltMediator extensionsTbbId="tcm:193-34-2048"?>where tcm:193-34-2048 is the Content Manager URI of the Template Building Block that contains your .NET assembly.
- In the root
xsl:stylesheetelement, add an attribute to declare a namespace prefix for the class you just created. By default the attribute looks like this:xmlns:Extender="http://www.sdltridion.com/ContentManager/FunctionSource/MyClass"where Extender is the prefix you choose, and MyClass is the name of the class you created.
The namespace http://www.sdltridion.com/ContentManager/FunctionSource/ with your class name appended is generated automatically for you.
However, you may wish to give the class a different namespace. For example, your XSLT template uses another .NET assembly that contains a class with the same name and one or more methods with the same names. In this case, you will want to specify the namespace yourself in your .NET code above the class (this requires you to import the class Tridion.ContentManager.Templating.Xslt.XsltExtensionAttribute):
[XsltExtension(Namespace="MySpecialNamespace")] public class MyClass : IFunctionSourceTo refer to this namespace, your attribute in the
xsl:stylesheetroot element of your XSLT stylesheet should now be as follows:xmlns:Extender="MySpecialNamespace"where again, Extender is the prefix you choose.
You have now declared a namespace prefix for your .NET class. - To make a call to one of the methods in your class, you can now write the following in your XSLT stylesheet to insert the return value of the method:
<xsl:value-of select="Extender:MyMethod()" />where Extender is the prefix you declared at the top of your stylesheet, and Method is the name of a method in your class.