Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

tridion

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 Visual Studio, but use both the same way in Template Builder.

In terms of code, I can create a C# Code Fragment Template Building Block (TBB), for example:

var timeStamp = DateTime.Now.ToString("d MMM yyyy");
package.PushItem("timeStamp from fragment", package.CreateHtmlItem(timeStamp));

I can also create a .NET assembly Template Building Block using the same code by implementing ITemplate as below.

using System;
using Tridion.ContentManager.Templating;
using Tridion.ContentManager.Templating.Assembly;

namespace CreateAndBreakTemplates
{
  [TcmTemplateTitle("Add Date to Package")]
  public class AddDateToPackage : ITemplate
  {
    public void Transform(Engine engine, Package package)
    {
      var timeStamp = DateTime.Now.ToString("d MMM yyyy");
      package.PushItem("timeStamp from assembly", 
                       package.CreateHtmlItem(timeStamp));
    }
  }
}

The docs explain that "SDL Tridion inserts the code fragment in its predefined method of a predefined class." It looks like this class implements ITemplate and adds some references below (am I missing anything?).

The assembly setup instructions mention at least these dlls.

  • Tridion.Common.dll
  • Tridion.ContentManager.dll
  • Tridion.ContentManager.Templating.dll
  • Tridion.ContentManager.Publishing.dll

Any other difference between fragment and assembly and how would you choose between the two?

like image 589
Alvin Reyes Avatar asked Aug 04 '12 22:08

Alvin Reyes


People also ask

What's the difference between C and C++?

C is a function driven language because C is a procedural programming language. C++ is an object driven language because it is an object oriented programming. Function and operator overloading is not supported in C. Function and operator overloading is supported by C++.

Is C+ better than C?

C is still in use because it is slightly faster and smaller than C++. For most people, C++ is the better choice. It has more features and more applications, which allow you to explore various roles. For most people, learning C++ is also easier especially if you are familiar with object-oriented programming.


2 Answers

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)
    {
    }
}

%>
like image 60
Frank van Puffelen Avatar answered Oct 11 '22 16:10

Frank van Puffelen


Frank has explained the difference between the two approaches, but that still leaves the question of how to choose between the two. My personal advise is to never use C# fragments for anything, with only one exception*. As you have found out, there is some dark magic going on in them that I personally do not like. Also, there is so much you cannot do in them that a .NET programmer is quite fond of, such as creating classes.

Putting my personal taste aside, I see only one reason why you would ever resort to C# fragments: if you do not have access to Visual Studio or another tool that builds DLLs. And that is not a very strong argument either: if you want a job done, you should get the proper tools!

*The exception being the C# fragments that Tridion automatically creates for each ITemplate in your assembly, of course.

like image 39
Quirijn Avatar answered Oct 11 '22 16:10

Quirijn