What's the difference between C# Code Fragments and Assembly TBBs?

后端 未结 5 752
死守一世寂寞
死守一世寂寞 2021-02-13 03:07

I understand C# Code Fragments and .NET Assemblies offer the same functionality for modular template development. We manage the code fragments in the CME and assembly code in Vi

5条回答
  •  北恋
    北恋 (楼主)
    2021-02-13 03:56

    A C# fragment is compiled into an assembly by Tridion when the template is first invoked and after it's been modified. To compile the fragment, Tridion wraps it in some "dungeon dressing" (bonus points for those who know where that term comes from) that:

    1. Uses the Tridion.ContentManager, Tridion.ContentManager.CommunicationManagement, Tridion.ContentManager.ContentManagement and Tridion.ContentManager.Templating namespaces
    2. Makes the Package and Engine available in fields called package and engine respectively
    3. Creates a logger for the C# fragment that is available through a field called log
    4. Adds references to some commonly used assemblies (but does not add a using for their namespaces yet)

    Edit: given the other answers it seems many people are not aware of how to accomplish certain tasks in C# fragment TBBs, so I'll document them below:

    Import additional namespaces

    To import/use additional namespaces into your C# fragment, you need to use this syntax:

    <%@ Import Namespace="Tridion.ContentManager.ContentManagement.Fields" %>
    

    Note that this will only import namespaces from assemblies that are already referenced by Tridion. There is no mechanism for you to add references to other assemblies explicitly; so if you need a third-party DLL, you will need to add it to the GAC.

    Defining custom functions

    You can define custom fields and functions in your C# fragment by using this syntax:

    <%!
    
    public static string GetDate()
    {
        return new DateTime().ToString("u").Replace(" ", "T");
    }
    
    %>
    

    Defining member fields and (nested) classes

    The syntax for defining custom functions also allows you to define nested classes and/or member fields:

    <%!
    
    public class MyLittleHelper
    {
        public MyLittleHelper(string param1)
        {
        }
    }
    
    %>
    

提交回复
热议问题