Orchard CMS - 使用字段扩展用户 - 在博客文章中公开值 [英] Orchard CMS - Extending Users with Fields - exposing values in Blog Post

查看:34
本文介绍了Orchard CMS - 使用字段扩展用户 - 在博客文章中公开值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想扩展用户内容定义,以包含可在现有博客的每篇博文中查看的简短个人简介和图片.我不确定这样做的最佳方法是什么.

I'd like to extend the users content definition to include a short bio and picture that can be viewed on every blog post of an existing blog. I'm unsure of what the best method to do this is.

我尝试使用这些字段扩展用户内容类型,但我似乎无法使用前端的形状跟踪工具在模型中看到它们.

I have tried extending the User content type with those fields, but I can't seem to see them in the Model using the shape tracing tool on the front end.

有没有办法在博客文章中传递用户形状上的字段?如果是这样,最好的方法是什么?

Is there a way to pass through fields on the User shape in a blog post? If so, what is the best way to do it?

推荐答案

我也做了很多,并且总是包含一些自定义功能来实现这一点.

I also have done this a lot, and always include some custom functionality to achieve this.

有一种方法可以执行此 OOTB,但它不是最好的 IMO.您始终在任何内容项的 CommonPart 上拥有Owner"属性,因此在您的博客文章视图中,您可以执行以下操作:

There is a way to do this OOTB, but it's not the best IMO. You always have the 'Owner' property on the CommonPart of any content item, so in your blogpost view you can do this:

@{
    var owner = Model.ContentItem.CommonPart.Owner;
}

<!-- This automatically builds anything that is attached to the user, except for what's in the UserPart (email, username, ..) -->
<h4>@owner.UserName</h4>
@Display(BuildDisplay((IUser) owner))

<!-- Or, with specific properties: -->
<h1>@T("Author:")</h1>
<h4>@owner.UserName</h4>
<label>@T("Biography")</label>
<p>
    @Html.Raw(owner.BodyPart.Text)
</p>
<!-- <owner content item>.<Part with the image field>.<Name of the image field>.FirstMediaUrl (assuming you use MediaLibraryPickerField) -->
<img src="@owner.User.Image.FirstMediaUrl" />

我经常做的是为此创建一个自定义驱动程序,这样您就可以使用placement.info 并遵循果园的最佳实践:

What I often do though is creating a custom driver for this, so you can make use of placement.info and follow the orchard's best practices:

通用部分驱动程序:

public class CommonPartDriver : ContentPartDriver<CommonPart> {

    protected override DriverResult Display(CommonPart part, string displayType, dynamic shapeHelper) {
        return ContentShape("Parts_Common_Owner", () => {
            if (part.Owner == null)
                return null;

            var ownerShape = _contentManager.BuildDisplay(part.Owner);
            return shapeHelper.Parts_Common_Owner(Owner: part.Owner, OwnerShape: ownerShape);
        });
    }
}

视图/Parts.Common.Owner.cshtml:

Views/Parts.Common.Owner.cshtml:

<h1>@T("Author")</h1>
<h3>@Model.Owner.UserName</h3>

@Display(Model.OwnerShape)

展示位置信息:

<Placement>
  <!-- Place in aside second zone -->
  <Place Parts_Common_Owner="/AsideSecond:before" />
</Placement>

这篇关于Orchard CMS - 使用字段扩展用户 - 在博客文章中公开值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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