Static View Renderings in Sitecore 6.6
Asked Answered
C

1

6

I was wondering if there was any way to statically call a View Rendering similarly to how you would call a sublayout using the following web forms code:

<sc:Sublayout Path="~/sublayouts/samplesublayouts.ascx" DataSource="SomeItemId" runat="server" />

I've tried doing this:

@Html.Sitecore().ViewRendering("~/renderings/samplerendering.cshtml", new { DataSource= "SomeItemId"})

But I can't strongly type the view rendering unless I also create the rendering item in sitecore and the also create the model item in sitecore because I'll receive and error. I'd like to know if there is a similarly simple manner I could use with MVC for statically typing internal renderers.

Cyr answered 27/6, 2013 at 20:9 Comment(0)
V
12

The approach shown below allows razor views to be statically bound to presentation items without creating rendering items.

In the layout.cshtml file statically bind a razor view that doesn't have a View Rendering presentation item in Sitecore and specify a DataSource item:

@Html.Sitecore().ViewRendering("/views/StandaloneRendering.cshtml", new { DataSource = "/sitecore/content/Home/My Datasource Item" })

The StandaloneRendering.cshtml razor view looks like this:

@using Sitecore.Mvc.Presentation
@model RenderingModel

@functions
{
    public Sitecore.Data.Items.Item Item 
    { 
        get
        {
            var item = Sitecore.Context.Item;

            if (!string.IsNullOrEmpty(Model.Rendering.DataSource))
            {
                item = Sitecore.Context.Database.GetItem(Model.Rendering.DataSource);
            }
            return item;
        }
    }
}   

<p>Item  Name: @Model.PageItem.Name</p>    
<p>Datasource Path: @Model.Rendering.DataSource</p>    
<p>Datasource Item Name: @Item.Name</p>
<p>Datasource Item Path: @Item.Paths.FullPath</p>
<p>Datasource Item Template: @Item.TemplateName</p>

The following gets output on the page:

Item Name: Home

Datasource Path: /sitecore/content/Home/My Datasource Item

Datasource Item Name: My Datasource Item

Datasource Item Path: /sitecore/content/Home/My Datasource Item

Datasource Item Template: Sample Item

A couple of things to be aware of when doing this:

  1. The Sitecore fields being rendered out by the razor view are not editable in the Page Editor.
  2. I doubt very much that the StandaloneRendering.cshtml output will make it into the Sitecore HTML cache.
  3. The Item property in the @functions block should be moved to some where so that it can be reused across multiple razor views.
  4. It's a non-standard approach. This may confuse some folks who expect to find an associated rendering item in Sitecore.
Vocation answered 28/6, 2013 at 10:10 Comment(4)
Why would a standalone rendering work, where as a rendering with a corresponding item does not? Additionally, is there no way to provide an associated Sitecore item for the rendering that is used, even if it not editable in the page editor?Fremantle
I believe this approach also works if the razor file is also setup as a view rendering in Sitecore, i.e. It has a view rendering item that's path points to it. From your original post I had assumed that you were after a light weight way to statically invoke razor views that could also receive a data source parameter. Can you clarify what it is that you are seeking to do and also what error you were seeing, thanksVocation
This would essentially do what I was looking for although I was looking for a built in method of doing this where the item could be automatically handled by a model implementing IRenderingModel. We ended up using glassmapper models and building them automatically using the built in change the the model generation pipeline. These Views are not really particular to being a view rendering in anyway so we were able to used PartialViews to achieve our goal. This way we could just pass the model itself and the PartialView can just render like normal MVC.Cyr
Sorry to be critical but; I don't understand why sitecore dev's think it's acceptable to put functions into views when no one else in the MVC world does. If you need a function to render your view you should have a controller action. This is the separation of concerns at the heart of an MVC infrastructure.Myocardiograph

© 2022 - 2024 — McMap. All rights reserved.