Kentico Document/TreeNode 宏命名空间

Kentico Document/TreeNode Macro Namespace

creating/registering a custom macroTreeNode 参数 (CurrentDocument) 为目标时,以什么宏命名空间 can/should 为目标,以便用户在针对 [编写宏表达式时可以访问命名空间的成员=12=] 个对象?

documentation提供了StringNamespaceSystemNamespace等例子,TreeNode对象应该用什么?

自定义宏按以下方式注册。它采用 TreeNode 参数,理想情况下将与 Web 部件中的 {% CurrentDocument.Foobar() %} 等中缀表示法一起使用。

[assembly: RegisterExtension(typeof(MacroMethods), typeof(TreeNode))]
public class MacroMethods : MacroMethodContainer
{
    [MacroMethod(typeof(string), "Generates a string.", 1)]
    [MacroMethodParam(0, "CurrentDocument", typeof(TreeNode), "The current rendered document.")]
    public static object Foobar(EvaluationContext context, params object[] parameters)
    {
        // ...
    }

感谢您提供的任何帮助。

您可能不必将 CurrentDocument 作为参数传递。您可以使用 CMS.DocumentEngine.DocumentContext.CurrentDocument

在宏中访问 CurrentDocument

恐怕这是不可能的,因为您需要扩展 TreeNode class 本身。我也在查看源代码,DocumentContext 的注册方式与自定义宏 methods/namespaces/properties 不同。它没有使用 MacroMethodContainer,而是 AbstractContext,没有源代码就无法扩展。

但是,即使这是可能的,我也强烈建议不要这样做。将自定义代码与 Kentico 代码混合是违反最佳实践的。我建议做的是创建一个自定义宏命名空间并将所有 methods/properties 放在那里。

最终您将能够调用所有宏,例如:

{% MyCoolMacros.Foobar() %}