C# 代码片段和程序集 TBB 之间有什么区别? [英] What's the difference between C# Code Fragments and Assembly TBBs?

查看:29
本文介绍了C# 代码片段和程序集 TBB 之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解 C# 代码片段和 .NET 程序集为模块化模板开发提供了相同的功能.我们在 CME 中管理代码片段,在 Visual Studio 中管理汇编代码,但在模板生成器中使用相同的方式.

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.

在代码方面,我可以创建一个C#代码片段模板构建块(TBB),例如:

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));

我还可以通过实现以下 ITemplate 使用相同的代码创建 .NET 程序集模板构建块.

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

文档 解释了插入SDL 代码"片段在其预定义类的预定义方法中."看起来这个类实现了 ITemplate 并在下面添加了一些引用(我是否遗漏了什么?).

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?).

组装设置说明至少提到了这些 dll.

The assembly setup instructions mention at least these dlls.

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

片段和汇编之间还有其他区别吗?您会如何在两者之间做出选择?

推荐答案

当第一次调用模板和修改模板时,Tridion 会将 C# 片段编译为程序集.为了编译该片段,Tridion 将其包裹在一些地牢敷料"中(那些知道该术语来自哪里的人的奖励积分):

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. 使用 Tridion.ContentManagerTridion.ContentManager.CommunicationManagementTridion.ContentManager.ContentManagementTridion.ContentManager.Templating 命名空间
  2. 使 PackageEngine 分别在名为 packageengine 的字段中可用
  3. 为 C# 片段创建一个记录器,该记录器可通过名为 log
  4. 的字段使用
  5. 添加对一些常用程序集的引用(但尚未为其命名空间添加using)
  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)

<小时>

鉴于其他答案,似乎很多人不知道如何在 C# 片段 TBB 中完成某些任务,因此我将在下面记录它们:


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:

要将其他命名空间导入/使用到 C# 片段中,您需要使用以下语法:

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

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

请注意,这只会从 Tridion 已引用的程序集中导入命名空间.没有机制可以让您显式地添加对其他程序集的引用;因此,如果您需要第三方 DLL,则需要将其添加到 GAC.

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.

您可以使用以下语法在 C# 片段中定义自定义字段和函数:

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

%>

这篇关于C# 代码片段和程序集 TBB 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆