I am creating a C# TBB(C# Code fragment). For that I need to write a userdefined method. I tried creating it using <%! %>. How to access the user defined method in the code. Thanks in advance. Please suggest me a way to solve this issue.
C allows programmers to write their own functions, also known as user-defined functions. A user-defined function has three main components that are function declarations, function definition and function call. Further functions can be called by call by value or call by reference.
You can also declare your own functions and call them within a policy. User-defined functions help you encapsulate and reuse functionality in your policy. The syntax for a function declaration is the Function keyword followed by the name of the function and a comma-separated list of runtime parameters.
A function is a block of code that performs a specific task. C allows you to define functions according to your need. These functions are known as user-defined functions. For example: Suppose, you need to create a circle and color it depending upon the radius and color.
In order to transfer control to a user-defined function, we need to call it. Functions are called using their names followed by round brackets. Their arguments are passed inside the brackets. There are two types of function calls i.e, Call by value and Call by reference.
The TOM.NET API reference provides the following example:
<%@ Import Namespace="Tridion.ContentManager.Publishing"%>
<%!
private string ExtraString()
{
return "Something added by the C# template";
}
%>
log.Debug("Executing C# template");
if (engine.RenderMode == RenderMode.Publish)
{
package.GetByName(Package.OutputName).AppendToStringValue(ExtraString());
}
In addition to the above, the following syntax is supported:
<%@Import Namespace="..." %>
Imports the namespace enclosed between the quotation marks into the code fragment. Any class you import must be present in the Global Assembly Cache.
<%! ... %>
Declares methods, constants and classes for the rest of the code fragment to use. The declarations cannot contain the string '%>'. Note that any classes you create can only be referenced within the code fragment.
<%RunTemplate Template="tcm:1-184-2048" Class="Tridion.Templating.Examples.ExampleTemplateClass"%>
Runs a specific .NET Assembly Template Building Block, identified by the URI in the Template attribute. This statement is typically generated by SDL Tridion 2009 itself when you upload a .NET assembly, to provide access to a specific class in the .NET Assembly.
<%@Assembly Name="System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"%>
Inserts a reference to a nonstandard .NET assembly, which must be present in the Global Assembly Cache. Use the full assembly name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With