Skip to content

ARCHIVED

!注意“归档”

This chapter has not been updated for the current version of Orchard, and has been ARCHIVED.

At the core of the idea of CMS is the ability to extend the system to new content types instead of restricting it

CMS概念的核心是能够将系统扩展到新的内容类型而不是限制它

to pre-defined ones such as blog posts.

预定义的博客文章。

Orchard has a rich composition model that enables new content types to be created from existing parts

Orchard拥有丰富的组合模型,可以从现有零件创建新的内容类型

and extended at runtime. It also enables a development model that is code-centric rather than database-centric:

并在运行时扩展。它还支持以代码为中心而不是以数据库为中心的开发模型:

for all practical purposes, database persistence will just happen without the developer having to do anything

出于所有实际目的,数据库持久性将在没有开发人员必须做任何事情的情况下发生

beyond defining the shape of his objects in code.

除了在代码中定义他的对象的形状。

Basic Concepts

基本概念

Content Item

内容项目

A content item is a piece of content that you'd want to manipulate as a single entity.

内容项是您要作为单个实体操作的一段内容。

For example, a blog post is composed of many parts: there is a title, a body, an author, comments, tags, etc.

例如,博客文章由许多部分组成:标题,正文,作者,评论,标签等。

But it is clear that the entity that you want to manipulate is the blog post as a whole.

但很明显,你想要操纵的实体是博客文章的整体。

Other examples of content items are images, videos, wiki pages, products, forum threads, etc.

内容项的其他示例是图像,视频,维基页面,产品,论坛帖子等。

A content item has an integer id that is unique across the site.

内容项具有整个站点中唯一的整数ID。

The definition of a content item is voluntarily relatively vague, because we don't want to restrict

内容项的定义是自愿相对模糊的,因为我们不想限制

what is considered a content item, so that developers can apply the same concepts to a wide array of objects.

什么被视为内容项,以便开发人员可以将相同的概念应用于各种对象。

Content Type

内容类型

A content type can be seen as a category of contents; it represents what a content item is.

内容类型可以看作是一类内容;它代表了一个内容项目。

For example, it's easy to understand that a content item is a blog post, or a photo, or a wiki page.

例如,很容易理解内容项是博客文章,照片或维基页面。

The core of the notion here are in the words "is a": if you can say that a given content item is a "something",

这里概念的核心是“是一个”:如果你可以说给定的内容项是“某事”,

that "something" probably is the content type of the content item.

“某事”可能是内容项的内容类型。

In developer speech, this concept is analogous to the concept of class, although the Orchard type system

在开发人员演讲中,这个概念类似于类的概念,虽然是Orchard类型系统

is established at run-time rather than statically determined from definitions in source code.

在运行时建立,而不是从源代码中的定义静态确定。

A content type in Orchard is just a name (in other words, it's identified by a simple string).

Orchard中的内容类型只是一个名称(换句话说,它是由一个简单的字符串标识)。

Content Part

内容部分

Content items in Orchard are composed from existing nuggets of data and logic called content parts.

Orchard中的内容项由现有的数据块和称为内容部分的逻辑组成。

A content part packages the set of fields and methods that are necessary to implement a particular

内容部分打包实现特定所必需的字段和方法集

reusable aspect of a content item.

内容项的可重用方面。

All the parts that form a given content item share the same integer id.

构成给定内容项的所有部分共享相同的整数id。

For example, the "Comments" part has four properties: lists of published and pending comments,

例如,“注释”部分有四个属性:已发布和待处理注释的列表,

a flag that determines whether comments are shown, and a flag that determines

确定是否显示注释的标志,以及确定的标志

whether the comment thread is closed. That part can be added to a content type to make it commentable.

评论线程是否已关闭。可以将该部分添加到内容类型以使其可评论。

In this system, the comment aspect doesn't need to know what it's commenting on, and the commented item

在这个系统中,评论方面不需要知道它正在评论什么,以及评论的项目

doesn't need to know that it has comments.

不需要知道它有评论。

This of course provides high reusability as the composed units are narrow and loosely-coupled aspects.

这当然提供了高可重用性,因为组合单元是狭窄且松散耦合的方面。

Still, the size of the aspects is large enough that they can package a whole feature

尽管如此,方面的大小足够大,可以打包整个功能

(as opposed to field-level composition that does not enable rich feature sets to be encapsulated as one single entity,

(与不允许将丰富的功能集封装为单个实体的字段级组合相反,

and to type-level definition of behavior that doesn't provide easy reuse of cross-concerns

以及不能轻易重用交叉问题的行为的类型级定义

that apply to more than one content type).

适用于多种内容类型)。

Record

记录

A record is a concept that only needs to be known to content-type developers and that doesn't need

记录是一个只需要内容类型开发人员知道并且不需要的概念

to surface to the end user of the application.

表面到应用程序的最终用户。

A record is the simple code representation of the data for a content part, as an almost Plain CLR Object

记录是内容部分的数据的简单代码表示,作为几乎普通的CLR对象

that is used for persistence of the part in and out of the database.

用于持久存储和退出数据库的部分。

"Almost Plain CLR" because it usually derives from ContentPartRecord, which gives them an Id and a reference

“几乎是普通的CLR”,因为它通常来自ContentPartRecord,它为它们提供了Id和引用

to the content item the part is participating in.

到该部分参与的内容项目。

Content Driver

内容驱动程序

A content driver is similar to an MVC controller, except that it works at the level of the content part.

内容驱动程序类似于MVC控制器,除了它在内容部分的级别上工作。

It is responsible for preparing the views of the part, on the front-end, but also as an editor in

它负责准备部分的视图,在前端,但也作为编辑

the content item editor. It also handles post modifications in those editors in order to persist changes.

内容项编辑器。它还处理这些编辑器中的后期修改以保持更改。

Finally, it handles the importing and exporting of the part. In many ways, the driver is the brain of your part.

最后,它处理零件的导入和导出。在许多方面,司机是你的大脑。

Content Handler

内容处理程序

A content handler is the object that manages content items and parts.

内容处理程序是管理内容项和部件的对象。

It is a set of services that will chime in whenever the application needs to create parts or items,

它是一组服务,只要应用程序需要创建零件或项目,它就会响起,

among other tasks. Content providers are built around the idea of a filter, where the application

其他任务。内容提供程序是围绕应用程序所在的过滤器构建的

calls into all filters when it needs something, and each filter can decide whether to participate or not,

在需要时调用所有过滤器,每个过滤器可以决定是否参与,

depending on the parameters communicated by the application.

取决于应用程序传达的参数。

For example, a content handler often manages the persistence of a part into a repository.

例如,内容处理程序通常将部件的持久性管理到存储库中。

Most handlers are implemented as a simple assemblage of pre-defined filters.

大多数处理程序都是作为预定义过滤器的简单组合实现的。

Shapes and Templates

形状和模板

Drivers create dynamic objects that represent the data to be rendered to the browser. Those objects are

驱动程序创建动态对象,表示要呈现给浏览器的数据。那些对象是

called shapes and correspond to a view model in MVC, except that they represent a single part instead

调用形状并对应MVC中的视图模型,除了它们代表单个部分

of the whole model for the complete view.

整个模型的完整视图。

When the time comes to render those shapes and transform them into HTML, Orchard looks in the file system

当需要渲染这些形状并将它们转换为HTML时,Orchard会在文件系统中查找

for templates and in special code constructs called shape methods for the most relevant way to handle

对于模板和特殊代码构造,称为形状方法,用于处理最相关的方法

that specific shape. Templates are typically what you will use. They are typically .cshtml files found in the Views

那个特定的形状。模板通常是您将使用的。它们通常是在视图中找到的.cshtml文件

folder of a theme or module.

主题或模块的文件夹。

Building a new content type

构建新的内容类型

New content types, and even new content parts, can be built from the admin UI. For more details about

可以从管理UI构建新的内容类型,甚至是新的内容部分。有关的更多详细信息

this scenario, please read creating custom content types.

在这种情况下,请阅读[创建自定义内容类型](Creating-custom-content-types)。

Of course, content parts and types can also be built from code.

当然,内容部分和类型也可以从代码构建。

Read the Getting Started with Modules course or the writing a content part guide for examples.

阅读[模块入门课程](模块入门)或[编写内容部分](写入内容部分)指南以获取示例。

Composing types from parts

从零件组成类型

On its own, a part doesn't do much. To make it useful, we need compose multiple parts

就其本身而言,一部分并没有做太多。为了使它有用,我们需要组合多个部分

into a content type. Here are a few examples of the most frequently used parts:

进入内容类型。以下是最常用部件的几个示例:

  • CommonPart gives an owner, created and last modified dates as well as an optional container (useful for hierarchies of content items such as a book containing chapters that contain pages that contain paragraphs for example).

  • CommonPart提供所有者,创建日期和最后修改日期以及可选容器(对于内容项的层次结构非常有用,例如包含包含例如包含段落的页面的章节的书籍)。 *

  • TitlePart gives the item a title

  • TitlePart为项目提供标题 *

  • AutoroutePart gives the item a path, making it possible to navigate to the item as a page in the front-end.

  • AutoroutePart为项目提供路径,使其可以作为前端页面导航到项目。 *

  • BodyPart adds a body field and a format for that body.

  • BodyPart为该主体添加了一个正文字段和一种格式。 *

Settings

设置

Content parts can have settings that define the behavior of the part for all items of a certain type.

内容部件可以具有定义部件对于特定类型的所有项的行为的设置。

For example, a map part can have settings for the default location to map, or for whether the map

例如,地图部件可以设置要映射的默认位置,或者是否为地图

should be interactive or not.

应该互动还是不互动。

These part settings can be modified from the admin UI by going into Content/Content Types, choosing the content

通过进入内容/内容类型,选择内容,可以从管理UI修改这些部分设置

type to configure and then by deploying the section of the part.

输入要配置的类型,然后通过部署部件的部分。

Admin Menu

管理员菜单

Modules can plug into the admin system.

模块可以插入管理系统。

This can be accomplished by using the INavigationProvider interface in the Orchard.UI.Navigation namespace.

这可以通过使用Orchard.UI.Navigation命名空间中的IN​​avigationProvider接口来完成。

Here is an example of an admin menu item:

以下是管理菜单项的示例:

public class AdminMenu : INavigationProvider {

    public string MenuName { get { return "admin"; } }



    public void GetNavigation(NavigationBuilder builder) {

        builder.Add("Sandbox", "1",

            menu => menu

                .Add("Manage Something", "1.0",

                    item => item

                        .Action("List", "Admin", new { area = "Orchard.Sandbox" })

                        .Permission(StandardPermissions.AccessAdminPanel)));

    }

}

Note that we're using Orchard.Security.StandardPermissions.AccessAdminPanel here.

请注意,我们在这里使用Orchard.Security.StandardPermissions.AccessAdminPanel。

You can just as easily define your own permissions using the IPermissionProvider interface.

您可以使用IPermissionProvider接口轻松定义自己的权限。

Creating items of a custom type

创建自定义类型的项目

Once you have created your own content type, you can create items of this type from the admin UI

创建自己的内容类型后,可以从管理UI创建此类型的项目

by clicking New/YourContentType if the type has been marked "creatable".

如果类型已标记为“可创建”,则单击New / YourContentType。

Items can also be created from code:

也可以从代码创建项目:

var custom = _contentManager.Create<CustomPart>("CustomType", part => {

    part.Record.SomeProperty = "My property value";

});

Querying the catalog

查询目录

To get a specific content item of which you know the id, you can call:

要获取您知道其ID的特定内容项,您可以致电:

var page = _contentManager.Get<CustomPart>(id);

It is also possible to create more general queries against the content manager,

也可以针对内容管理器创建更一般的查询,

that can return lists of items:

可以返回项目列表:

var items = _contentManager.Query<TitlePart, TitlePartRecord>()

    .Where(t => t.Title.Contains("foo"))

    .OrderBy(r => r.Title)

    .Slice(10, 5);

The code above will get the items 10 to 15 in the list of items that have "foo" in their titles, when ordered by title.

当按标题排序时,上面的代码将在标题中具有“foo”的项目列表中获得项目10到15。

Accessing the parts of the item

访问项目的各个部分

To access the different parts of a content item, you can call the As method on any of the parts:

要访问内容项的不同部分,可以在任何部分上调用As方法:

var body = mypage.As<BodyPart>().Text

Revisions

修订

06-10-2012: rewrite for current version of Orchard (1.4)

06-10-2012:重写当前版本的Orchard(1.4)