Write a Orchard online store module from scratch (5)-Create and render ProductCatalog content types

Source: Internet
Author: User

Original address: Http://skywalkersoftwaredevelopment.net/blog/writing-an-orchard-webshop-module-from-scratch-part-5
Create and render ProductCatalog content types

This is the 5th chapter of the tutorial on writing a new orchard module from scratch.
For an overview of this tutorial, please see the introduction.

For site visitors to be able to add products to their shopping cart, we need a product catalog.
The product catalog can be a simple product list. However, in this tutorial, we want the topic author to take over the rendering (rendering) work, with examples of classifying items by category. In order to do this, we need to be able to ' sknnable ' things, the list itself is not easy to change skin (it can be changed skin, but all the list will have the same appearance, this is not what we want.) I had to admit I didn't know if the case was feasible, but I had some problems with customizing the list rendering and I had to change it to something else.

So, we will introduce the ProductCatalog content type, from which we can create a ProductCatalog content item, in turn, if we decide that we want to customize the rendering, we can create a template.
The requirements for our catalogue are as follows:

    • Each directory displays a category of products (such as books, DVDs, CDs). There is no technical limit to prevent other product types from appearing, which is just what our demo needs.
    • The rendering of the catalog should be overridden by other topics, so we'll create it as a content type (instead of having the user create a new list from the table).


Let's start by defining ProductCatalog to the migration class, adding the following code definition:

public int UpdateFrom2 ()
{
Contentdefinitionmanager.altertypedefinition ("ProductCatalog", type = = Type
. Withpart (typeof (Containerpart). Name)
. Withpart (typeof (Menupart). Name)
. Withpart (typeof (Adminmenupart). Name)
. Withpart (typeof (Routepart). Name)
. Creatable ()
);

return 3;
}


This code uses Contentdefinitionmanager a new content type named "ProductCatalog", which contains 3 parts (part) and can be created from the admin interface. Finally, it returns the version number of a new ochard migration to keep track of the current version of our module.

About Parts (Parts):

So that ProductCatalog can contain other content items,Containerpartis necessary.
Menupartis optional, but it enables our webmaster to add the product catalog to the main menu.
AdminmenupartIt is also optional, but it allows our webmaster to add product catalogs to the main menu of the admin interface, which is very handy.
RoutepartIn fact, to be able to access from the front-end and simultaneously giveProductCatalogSlug (acronyms) and title attributes are provided.

Hint, as you can see, I used the typeof (Containerpart). The name expression. This is because our module adds a pair of itemsOrchard.coreThe reference to this can only be done.
For other types of content, you may want to attach some content parts that have already been defined in a third-party module. However, when you add references to other modules, you should remind the users of your modules that they must first install the dependent modules. If they install your module first without installing a dependent module, orchard throws an exception and the only way to recover the site is to delete your module.
This means that if you just want to simply attach some parts of other modules, only the name can be used (such as "Advancedmenupart" instead of typeof (Advancedmenupart). Name).

When you refresh the admin page, Orchard will notify you that you need to upgrade your module. As we continue and update the module, we will see that the orchard has created a new content type (contents type):

We also saw a new menu item in the "New" node of the Admin interface:

Before we go on, I just want to mention that we're not necessarily using migration to create a new content type, we can do the same thing on the admin interface.
However, doing so in the code eliminates how the user who guides our module configures a new product catalog. As a developer of modules, we should strive to make our modules as easy as possible.

We will continue to create (and publish) a new book directory, which will be able to contain the content type of books by means of the name "Books", "book" as the contents of type, "Books" as the name of the menu item. We will also display the menu as a shortcut in the Administration page.

Next, we will add 3 new books to the book Catalog we created earlier:


Now let's take a look at the page:

We now have a basic Product catalog module that our webmaster can manage.
When you click on the "More" link, you will see the product details:

This is one of the wonderful places in orchard: You don't have to just stare at a Web page that has a set of properties: A page is also a content type that can attach some parts, and you can freely add more parts.
In our demo, we are able to build a new type and reusable features such as tags and comment widgets.
Wait a minute: do we not attach Productpart to our book content type? We did it, didn't we? However, they do not appear in the catalog, nor do they have detailed information about the product. What are we missing?

We did not see the reason for these fields because we did not implement the display method of the Productpart driver. The driver's part content (primarily) is responsible for creating the shape, which represents the view of the part content.

Now, we're going to show our productpart on the page, let's go back to our productdriver and add the following code:

 protected override Driverresult Display (Productpart part, string displayType, dynamic Shapehelper) {
R Eturn contentshape ("Parts_product", () = Shapehelper.parts_product (
Price:part. Price,
Sku:part. Sku
));
}


When the front-end page displays a content item with an additional Productpart, orchard calls the display method, and the display method calls the Contentshape method and returns Contentshaperesult, which is similar to the Partialview () method in ASP.
Contentshaperesult will contain the name of the shape, as well as its own shape, which is just a dynamic clay object. We use the Shapehelper object to create a shape and invoke a dynamically defined method. We give this method by submitting a set of parameters that will be converted into attributes and stored in our shape. The resulting shape will be the model of the Razor template file, and his name will match according to the naming rules of the shape. For shapes see this excellent introduction document!

We are creating a shape named "parts_product". In accordance with the Orchard specification, this means that a template file with the file name "Parts.Product.cshtml" exists (the underscore replaces the.).
If we name our shape "Jacky_chan_goes_mad", our razor file should be "Jacky.Chan.Goes.Mad.cshtml". Obviously, we should use meaningful names to accurately describe the characteristics of our shapes. A "part" prefix is a good specification, just like our productpart.

So, don't forget to create a file named "Parts.Product.cshtml" under the Views folder:

Otherwise you would be in pain:

Before orchard renders our productpart shape, we also need to use Placement.info to configure its location (placement):

<Placement>
<place parts_product_edit= "Content:1"/>
<place parts_product= "content:0"/>
</Placement>

If you forget where you created the shape, it won't be rendered.

The bold line indicates that a shape named "parts_product" is added to the area named "Content" on position 0. The position is used as a sort indicator to determine the order in which to render a shape within an area.
For more information about location (Placement), I encourage you to read more about it in the documentation.

Now refresh the front page (don't forget to save your changes):

The detail page now looks like this:

This is the work that is required to create a functional product catalog. For this example, we created a single product type and directory, which makes it easy for webmasters to define new product types and product catalogs!

In the next section, we will create a shopping cart feature that enables our site visitors to actually add products and settle them.

Write a Orchard online store module from scratch (5)-Create and render ProductCatalog content types

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.