【问题标题】:Building A SiteCore Template To Insert JavaScript构建 SiteCore 模板以插入 JavaScript
【发布时间】:2014-04-03 17:29:11
【问题描述】:

我正在尝试在 SiteCore 中构建一个基本的标签管理解决方案。

我在 Templates 下创建了一个名为 Tag Management 的文件夹。在 Tag Management 文件夹下,我创建了一个名为 Google Analytics 的模板。这个标签有一些属性被用作标签的参数。

如果我创建一个继承此模板的内容项,我会看到属性字段。

我需要知道的是——作为最佳实践——我会在哪里编写生成脚本标记的代码。我查看了 SiteCore 源项目,没有看到任何模板代码文件夹。


更新:根据反馈和这个网址:http://andyuzick.arke.com/2013/02/as-web-marketers-great-deal-of-our.html,我实现了一个新的类库,其中包含以下内容:

Settings.cs

namespace TagManagement
{
    public class Settings
    {
        public const string DEFAULT_GLOBAL_TAG_FOLDER = "/sitecore/content/Global/TagManagement";
        public static string GlobalTagFolder
        {
            get
            {
                return Sitecore.Configuration.Settings.GetSetting("TagManagement.GlobalTagFolder", DEFAULT_GLOBAL_TAG_FOLDER);
            }
        }
    }
}

WebControl.cs

using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using System;
using System.Text;
using System.Web.UI;

namespace TagManagement
{
    public class TagManagmentControl: Sitecore.Web.UI.WebControl
    {
        System.Web.UI.WebControls.Literal container;

        public string TagItem { get; set; }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            container = new System.Web.UI.WebControls.Literal();
        }

        protected override void CreateChildControls()
        {
            Assert.IsNotNullOrEmpty(TagItem, "tag item");
            Item item = Sitecore.Context.Database.GetItem(TagItem);
            StringBuilder tagToOutput = new StringBuilder();
            string templateName = item.TemplateName;
            switch (templateName)
            {
                case "Google Analytics":
                    tagToOutput.AppendLine("<script>");
                    tagToOutput.AppendLine("    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){");
                    tagToOutput.AppendLine("    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),");
                    tagToOutput.AppendLine("    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)");
                    tagToOutput.AppendLine("    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');");
                    tagToOutput.AppendLine("    ga('create', '" + item.Fields["Tracking ID"].Value + "', '" + item.Fields["Domain"].Value + "');");
                    if (item.Fields["Enable Demographics and Interest Reports"].Value == "1") 
                    {
                        tagToOutput.AppendLine("    ga('require', 'displayfeatures');");
                    }
                    tagToOutput.AppendLine("    ga('send', 'pageview');");
                    tagToOutput.AppendLine("</script>");
                    tagToOutput.AppendLine();
                    break;
                case "HTML Tracking Tag":
                    tagToOutput.AppendLine(item.Fields["Markup"].Value);
                    break;
            }
            container.Text = tagToOutput.ToString();
        }

        protected override void DoRender(HtmlTextWriter output)
        {
            EnsureChildControls();
            container.RenderControl(output);
        }

        protected override string GetCachingID()
        {
            return this.GetType().FullName;
        }
    }

}

PipelineProcessor.cs

using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Layouts;
using Sitecore.Pipelines.InsertRenderings;

namespace TagManagement
{
    public class InsertTags
    {
        public void Process(InsertRenderingsArgs args)
        {
            Assert.ArgumentNotNull(args, "args");

            if (Sitecore.Context.Site.Name == "shell")
            {
                return;
            }

            Item globalTagFolder = Sitecore.Context.Database.GetItem(Settings.GlobalTagFolder);
            Profiler.StartOperation("Tag Management: Adding Tags...");

            foreach (Item globalTagItem in globalTagFolder.Children)
            {
                TagManagement.TagManagmentControl control = new TagManagement.TagManagmentControl();
                if (control != null)
                {
                    control.TagItem = globalTagItem.ID.ToGuid().ToString();
                    control.Cacheable = true;
                    control.VaryByData = true;
                    RenderingReference reference = new RenderingReference(control);
                    reference.AddToFormIfUnused = true;
                    args.Renderings.Add(reference);
                    Tracer.Info(string.Concat("Tag Management: Added: '", globalTagItem.Name, "'"));
                }
            }

            Profiler.EndOperation();
        }
    }
}

如果会议室中的 SiteCore 专家提供任何建设性的反馈,我将不胜感激!

【问题讨论】:

    标签: c# sitecore sitecore6 sitecore7


    【解决方案1】:

    我相信你的方向是正确的。

    我可以通过三种方式来呈现这些标签内容。

    1) 将它添加到你的主布局代码后面(这是最简单的但我真的不喜欢那个选项,你的代码将依赖于主布局)

    2) 构建一个子布局并将其添加到头部标签的特定占位符中,赋予子布局代码渲染标签的责任(也不要太喜欢它)

    3) 在“renderLayout”管道中创建一个为您完成工作的流程(最好的选择,它是解耦的,可以通过配置文件轻松打开和关闭)*警告,您的标签需要运行服务器。

    您的流程如下所示:

    namespace YourNamespace
    {
        public class PrintTags
        {
            public void Process(RenderLayoutArgs args)
            {
                //put here validation you may require
    
                var head = WebUtil.FindControlOfType(Sitecore.Context.Page.Page, typeof(System.Web.UI.HtmlControls.HtmlHead));
    
                if (head != null)
                {
                    //add any content in the head
                    head.Controls.Add(new Literal(" CONTENT "));
                }
                else
                {
                    //make sure to not break the app instead just log the error.
                    Sitecore.Diagnostics.Log.Error("Error - The HEAD element must be runat=server", this);
                }
            }
        }
    }
    

    .配置:

    <configuration>
      <sitecore>
        <pipelines>
          <renderLayout>
            <processor type="YourNamespace.PrintTags, YourAssembly" />
          </renderLayout>
        </pipelines>
      </sitecore>
    </configuration>
    

    希望对你有帮助..

    干杯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-17
      • 2011-05-13
      • 2010-10-07
      • 2011-06-30
      • 1970-01-01
      • 2013-08-31
      • 1970-01-01
      • 2012-02-22
      相关资源
      最近更新 更多