Reputation: 125
I need to create a cshtml rendering and Sitecore control that allows me to insert JavaScript from a Sitecore field. I can then attach this control to the layout to insert the JavaScript on the page.
I tried inserting <script>
into a control that uses a rich text editor, but Sitecore strips out <script>
from the editor by default, which led me to this solution.
I know the basics--there needs to be the cshtml rendering, a template (assuming one field only, a multiline text field), and a layout rendering with the path to the cshtml file, but not much more than that.
The developers have used Glass Mapper, I don't know how that plays into the above, or how to re-instance this. This will be my first control. Here's a sample cshtml file if it helps:
@inherits Glass.Mapper.Sc.Web.Mvc.GlassView<Company.Model.TDS.sitecore.templates.User_Defined.Company.Platform.Modules.Common.Sidebar_Text_Callout>
@if (Model != null)
{
<div class="sidebar-text-callout">
<h3>@Editable(Model, x => x.Sidebar_Callout_Title)</h3>
<p>@Editable(Model, x => x.Sidebar_Callout_Copy)</p>
@if (Model.Sidebar_Callout_Link != null)
{
<div class="all-caps-callout">
<a class="link-with-icon" href="@Model.Sidebar_Callout_Link.Url" target="@Model.Sidebar_Callout_Link.Target">@Model.Sidebar_Callout_Link.Text <span class="svg">@Html.Partial("~/Views/Icons/arrowRight.cshtml")</span></a>
</div>
}
</div>
}
Upvotes: 1
Views: 1107
Reputation: 13141
Judging by the model's namespace in the example code you provided, it looks like your developers used TDS along with it's code generation feature to generate the Sidebar_Text_Callout
class.
I would recommend that you stick with the same process for creating your JavaScript rendering. The instructions for creating a Glass-based View Rendering are too broad to cover in this context, so instead i've included some high-level steps below. The Glass website as well as Sitecore's own documentation should cover this in more detail.
.cshtml
file.Cacheable
and Vary By Data
at minimum)Example View
@inherits Glass.Mapper.Sc.Web.Mvc.GlassView<Company.Model.TDS.sitecore.templates.User_Defined.Company.Platform.Modules.Common.PageLevelJavaScript>
@if (Model != null)
{
@Html.Raw(Model.MyJavaScriptField)
}
Upvotes: 2