How to create layout elements in Orchard 1.9

送分小仙女□ 提交于 2020-01-02 08:51:04

问题


Can someone please guide me on how to create layout elements in Orchard 1.9. I couldn't find any resource online.


回答1:


In general, creating a new layout element is similar to creating a new part. There is a driver and a few views involved in the process. To the point - you need to implement as follows:

  • An element class.. Class that inherits from Element, which contains all the element data. A model, so to speak.
  • A driver. Class that inherits from ElementDriver<TElement>, where TElement is the type you created above. Each element has it's own driver that handles displaying admin editor (and the postback) and frontend display views.
  • Shapes. All shapes should be placed under /Views/Elements/ folder, by convention.

    • Display shape. Named after your element, ie. MyElement.cshtml. This one renders your element on frontend.
    • Design display shape.. Named after your element, with .Design suffix, ie. MyElement.Design.cshtml. This one renders your element inside the layout editor.
    • Editor shape.. This one should be put in /Views/EditorTemplates/ folder instead. Default naming convention is Elements.MyElement.cshtml. It renders the editor shown when you drop a new element on layout editor canvas.

With all above done, your new element should appear in the list of elements on the right side of the layout editor, ready to use.

If you want to do some more complex elements, please check the existing implementations. Layouts module has a very decent architecture so you should get up to speed pretty quickly. Just keep in mind the necessary steps I wrote above.




回答2:


To create a custom layout element first create a class that inherits from Element. Element is found in the Orchard.Layouts namespace so you need to add a reference. To follow Orchard standards put this file in a folder called Elements.

public class MyElement : Element
{
    public override string Category
    {
        get { return "Content"; }
    }

    public string MyCustomProperty
    {
        get { return this.Retrieve(x => x.MyCustomProperty); }
        set { this.Store(x => x.MyCustomProperty, value); }
    }
}

Next, create a driver class in a folder called Drivers. This class inherits from ElementDriver<TElement> and likely you will want to override the OnBuildEditor and OnDisplaying methods. OnBuildEditor is used for handling creating our editors shape and updating our database when the editor is saved. OnDisplaying is used when we need to do things when displaying our element. Oftentimes, you will want to add properties to the shape which can be done with context.ElementShape.MyAdditionalProperty = "My Value";

public class MyElementDriver : ElementDriver<MyElement>
{
    protected override EditorResult OnBuildEditor(MyElement element, ElementEditorContext context)
    {
        var viewModel = new MyElementEditorViewModel
        {
            MyCustomProperty = element.MyCustomProperty
        };

        var editor = context.ShapeFactory.EditorTemplate(TemplateName: "Elements.MyElement", Model: viewModel);

        if (context.Updater != null)
        {
            context.Updater.TryUpdateModel(viewModel, context.Prefix, null, null);
            element.MyCustomProperty = viewModel.MyCustomProperty;
        }

        return Editor(context, editor);
    }

    protected override void OnDisplaying(Reddit element, ElementDisplayContext context)
    {
        context.ElementShape.MyAdditionalProperty = "My Value";
    }
}

We then just need our views. Our editor view goes into Views/EditorTemplates. The file name needs to be what we set the template name of the editor shape. In our case the view name will be Elements.MyElement.cshtml.

@model MyNameSpace.ViewModels.MyElementEditorViewModel

<fieldset>
    <div>
        @Html.LabelFor(m => m.MyCustomProperty, T("My Custom Property"))
        @Html.TextBoxFor(m => m.MyCustomProperty, new { @class = "text medium" })
    </div>
</fieldset>

Finally, we just need a view for our frontend. This view goes into the following folder Views/Elements. The name of the view file is the same as our element class name. For this example the file would be called MyElement.cshtml.

@using MyNameSpace.Elements
@using MyNameSpace.Models

@{
    var element = (MyElement)Model.Element;
}

<h1>@element.MyCustomProperty</h1>

You will then have a new element that you can drag into your layout with the layout editor.

For more details on creating an element from start to finish check out my blog post on creating a Reddit element.



来源:https://stackoverflow.com/questions/31981090/how-to-create-layout-elements-in-orchard-1-9

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!