首页 > 其他分享 >Sitecore FieldRenderer

Sitecore FieldRenderer

时间:2023-11-10 17:25:54浏览次数:35  
标签:item FieldRenderer result FileDescription Sitecore new Model public

前提

Sitecore 的 @Html.Sitecore().Field("file icon", item, new {@class="icon", }) 确实也挺好用的,但是局限于 item ,当有多个 sub item,或者其他地方的 item,远不如使用 @Model 方便。

所以自己写了个 Helper:

public class CustomerRenderer : FieldRenderer
{
    private Stack<string> _endFieldStack;
    protected virtual Stack<string> EndFieldStack => this._endFieldStack ?? (this._endFieldStack = new Stack<string>());

    public CustomerRenderer(Item item, string fieldName)
    {
        base.Item = item;
        base.FieldName = fieldName;
    }

    private RenderFieldResult _result;

    public HtmlString BeginRender()
    {  
        _result = base.RenderField();
        string str = _result.ValueOrDefault<RenderFieldResult, string>(result => result.FirstPart).OrEmpty();
        this.EndFieldStack.Push(_result.ValueOrDefault<RenderFieldResult, string>(result => result.LastPart).OrEmpty());
        return new HtmlString(str);
    }

    public HtmlString EndRender()
    {
        Stack<string> endFieldStack = this.EndFieldStack;
        return endFieldStack.Count != 0 ? new HtmlString(endFieldStack.Pop()) : throw new InvalidOperationException("There was a call to EndField with no corresponding call to BeginField");
    }

    public HtmlString Wrap(Func<RenderFieldResult, string> wrap)
    {
        var final = new StringBuilder();
        var begin = BeginRender();
        var content = wrap(_result);
        var end = EndRender();
        final.Append(begin);
        final.Append(content);
        final.Append(end);
        return new HtmlString(final.ToString());
    }

    public override string ToString()
    {
        return this._result?.ToString();
    }
}

使用方法:

后台通过 Controller 将 Model 传到 View:

public class ArticleController : Controller{
    public ActionResult Index(){
        return View("~/Views/Article.cshtml", CreateModel());
    }

    public ArticleModel CreateModel(){
        var item = RenderingContext.Current.Rendering.Item;
        var articleModel = new ArticleModel()
        {
            FileTitle = new CustomerRenderer(item, "File Title"),
            FileIcon = new CustomerRenderer(item, "File Icon"),
            FileDescription = new CustomerRenderer(item, "File Description"),
        };
        return articleModel;
    }
    
    public class ArticleModel
    {
        public FieldRenderer FileTitle { get; set; }
        
        public FieldRenderer FileIcon { get; set; }
        
        public FieldRenderer FileDescription { get; set; }
    }
}
// ~/Views/Article.cshtml
// 用法一
@if (!string.IsNullOrEmpty(Model.FileDescription))
{
    <li>
        @Model.FileLink.BeginRender()
        <i class="icon">@Model.FileIcon</i>
        @Model.FileDescription
        @Model.FileLink.EndRender()
    </li>
}

// 用法二
@if (!string.IsNullOrEmpty(Model.FileDescription))
{
    <li>
        @Model.FileLink.Wrap(r => $"<i class=\"icon\">{Model.FileIcon}</i>{Model.FileDescription}")
    </li>
}

// 最后生成:
<li>
    <a><i class=""><img src="./icon" /></i>FileDescription</a>
</li>

标签:item,FieldRenderer,result,FileDescription,Sitecore,new,Model,public
From: https://www.cnblogs.com/fires/p/17824165.html

相关文章

  • Sitecore 扩展CM部署
    相关文档:配置多个Sitecore实例实例结构:有两个Sitecore实例:Instance1和Instance2,分别管理各自的Content,在Publishing的时候调用SharedInstance(共享Sitecore实例)来作对应的Solr索引构建,最后相关的Scheduler也放在SharedInstance里。配置步骤SharedCM需要......
  • Sitecore 优化
    IIS优化1.设置AlwaysRunninng前往应用池选中对应的站点,点击高级设置,在StartMode那列选中AlwaysRunning,最后保存。2.设置PreloadEnabled为True在站点列选中你的站点,点击高级设置,将PreloadEnabled设置True,然后保存。3.设置doAppInitAfterRestart为True......
  • Sitecore 里删除 Item 报错 Could not create SSL/TLS secure channel.
    解决方法:前往mmc里添加你的域名win+r输入mmc打开下图面板;......
  • Sitecore XP 10.3(latest) Docker一键部署
    本文演示通过PowerShell+DockerDesktopforWindows一键部署Sitecore10.3(即Sitecore最新版)Docker开发/测试/演示环境。官方参考 SitecoreXP10.3.0DeveloperWorkstationDeploymentWithDocker演示配置为XPSingle(XP0) 环境准备1,windows10+/WindowsServer2019(Windows......
  • Sitecore10 Demo演示环境Azure一键部署(Step By Step Guide to installing Sitecore10
    本文演示SitecoreXPSingle(XP0)在Azure上的一键部署,即“30分钟生成Sitecore演示环境”的一环。关于XP(即SitecoreExperiencePlatform)roles的相关介绍移步XPSingle配置主要用来开发和测试:FourSitecoreroles:ContentDelivery,ContentManagement,Processing,andRepo......
  • Sitecore Nuget 配置
    配置SitecoreNuget包的路径配置文件路径地址:C:\Users<username>\AppData\Roaming\NuGet\NuGet.Config<?xmlversion="1.0"encoding="utf-8"?><configuration><pa......
  • Sitecore DI
    依賴注入通過showconfig.aspx工具查看可參考系統自帶的配置文件寫法:<siteroot>\App_Config\Sitecore\ExperienceForms\Sitecore.ExperienceForms.Mvc.config<sitec......
  • Sitecore Form 的使用
    下載安裝todo...使用文檔:https://doc.sitecore.com/xp/en/users/92/sitecore-experience-platform/sitecore-forms.html選擇Forms進入,創建自己的第一個form,......
  • Sitecore Field 类型与 C# 类型映射表
    配置文件路径位置:\App_Config\FieldTypes.config<!--SimpleTypes--><fieldTypename="Checkbox"type="Sitecore.Data.Fields.CheckboxField,Sitecore.Kernel"/><......
  • Sitecore 打包备份与恢复
    打包备份首先进入Desktop面板,选择PackageDesigner,之后一路操作之后填入sourcename恢复安装包时跳出来的选项网上博客的有关解释https://www.partech......