I need to render the Social Media Share
component in the cshtml
of Cloned Search Results
, is there a Sitecore Helper to render a component in the view? I am using SXA 1.8
so I know that we have the ability to render a component inside a rendering variant
.
1 Answer
You have 2 main options:
Use a placeholder
In your razor view, just add a placeholder, then in the Experience Editor, your marketers/editors can add the Social Media Share
module to that.
Pros - its nice and easy, give flexibility too if you wanted to add anything else. Cons - the editors have to remember to add the component every time.
Render the component inline
You can also use the Sitecore Html helper to render a component:
@Html.Sitecore().Rendering("<your rendering definition item id>", new { DataSource = "<your datasource item id>" })
Add the item Id of the Social Media Share
component as the first parameter. You will also need to create a datasource item for that component and you add the DataSource item Id to that too.
You can also add parameters for things like Caching:
@Html.Sitecore().Rendering("<your rendering definition item id>", new { DataSource = "<your datasource item id>", Cacheable = true, CacheKey = "create a cache key", Cache_VaryByData = true })
Pros - again, its simple to do, the content editors don't have to remember to add any extra components on the page Cons - you are reliant on a content item always being there. If that gets deleted, you will not get any warnings, this component will just fail.
-
Hey Richard, do you have any idea how I can preselect a specific rendering variant when I render an sxa component inline?– Bgl86Nov 13, 2019 at 9:40
-