AbleCommerce Forums
»
AbleCommerce
»
General Questions
»
Inserting an html Snippets into a cshtml file
Rank: Advanced Member
Groups: Authorized User, Developers Joined: 2/17/2019(UTC) Posts: 909
Thanks: 3 times Was thanked: 15 time(s) in 15 post(s)
|
Is there a way to insert an HTML snippets into an csHTML file?
Please allow me to explain. I'm constantly manipulating the _MetaTags.cshtml file. Please review what my current file looks like below. As you can see, I have manipulated the language tag, added og types, and am now in the process of adding Twitter tags. It would be more efficient if this information was stored in an html Snippet that I can just call.
If there is no way to do this today? If not, I highly recommend in the future version, that there be a configuration in the back-end where we can manipulate this file without having to directly insert code
I look forward to your feedback. - thanks -Ray
---- start
<html class="no-js" lang="en-US"> <meta http-equiv = "content-language" content = "en">
@if (!string.IsNullOrEmpty(description)) { <meta name="description" content="@description"> } @if (!string.IsNullOrEmpty(keywords)) { <meta name="keywords" content="@keywords"> } @if (!string.IsNullOrEmpty(objectUrl) && (!objectUrl.Contains("Default"))) { <link rel="canonical" href="@objectUrl" /> } @if (objectUrl.EndsWith("/Default")) { <link rel="canonical" href="@webUrl" /> }
<meta property="og:type" content="website"> <meta property="og:title" content="MyRadioMall.com | Buy Motorola Radios for less."> <meta property="og:description" content="MyRadioMall.com provides nationwide sales of Motorola Radios."> <meta property="og:url" content="https://www.myradiomall.com/"> <meta property="og:site_name" content="MyRadioMall.com"> <meta property="og:image" content="https://www.myradiomall.com/Themes/MRM-Default-Wide-2023/content/images/logo.webp">
---- end
|
|
|
|
Rank: Advanced Member
Groups: Authorized User, Developers Joined: 2/17/2019(UTC) Posts: 909
Thanks: 3 times Was thanked: 15 time(s) in 15 post(s)
|
I'm going to take a guess and say this cannot be done. In a future version, it would be nice to have a text box in the backend that lets you insert stuff directly into this tag view instead of manually entering it. I'll take it one step further: the ability to insert HTML snippets into view files would help keep everything under control, especially when the site grows large. Thanks Edited by user 6 months ago
| Reason: Not specified
|
|
|
|
Rank: Advanced Member
Groups: Administrators, Developers, Registered, HelpDesk, Authorized User, Admin, System Joined: 10/11/2018(UTC) Posts: 110
Thanks: 19 times Was thanked: 18 time(s) in 18 post(s)
|
Hi Ray, Yes, this isn't currently supported out of the box to insert HTML Snippet to view file without customization. You can achieve this by making a small customization. That is, you can load the HTML Snippet contents and render in existing _MetaTags.cshtml file. To do so, replace the following code Code:
@{
string description = ViewBag.MetaDescription ?? string.Empty;
string keywords = ViewBag.MetaKeywords ?? string.Empty;
string objectUrl = ViewBag.ObjectUrl ?? string.Empty;
}
with Code:
@using CommerceBuilder.CMS;
@{
string description = ViewBag.MetaDescription ?? string.Empty;
string keywords = ViewBag.MetaKeywords ?? string.Empty;
string objectUrl = ViewBag.ObjectUrl ?? string.Empty;
int metaTagsHtmlSnippetId = 1;
var metaTagsHtmlSnippet = AbleContext.Resolve<IHtmlContentRepository>().Load(metaTagsHtmlSnippetId);
}
@if (metaTagsHtmlSnippet != null && !string.IsNullOrEmpty(metaTagsHtmlSnippet.Html))
{
@Html.Raw(metaTagsHtmlSnippet.Html)
}
Hope this helps!
|
|
|
|
Rank: Advanced Member
Groups: Authorized User, Developers Joined: 2/17/2019(UTC) Posts: 909
Thanks: 3 times Was thanked: 15 time(s) in 15 post(s)
|
Thank you so much for taking the time to check into the code. It will be a time saver for me and, I believe, many other people. I have already created an HTML snippet called "_MetaTags.cshtml."
I'm very happy that the HTML snippet name will accept underscores and dots; this way, I will actually name them the file name that they are going to be inserted in.
Once again, many thanks for your exceptional work. - This makes life so much easier.
|
1 user thanked ray22901031 for this useful post.
|
|
|
Rank: Advanced Member
Groups: Administrators, Developers, Registered, HelpDesk, Authorized User, Admin, System Joined: 10/11/2018(UTC) Posts: 110
Thanks: 19 times Was thanked: 18 time(s) in 18 post(s)
|
Thank you!! Happy to help :)
|
|
|
|
Rank: Advanced Member
Groups: Authorized User, Developers Joined: 2/17/2019(UTC) Posts: 909
Thanks: 3 times Was thanked: 15 time(s) in 15 post(s)
|
Hi nadeem, Again, thank you for your effort in getting this done. You have no idea how our lives have now been simplified because of this. We have probably manipulated in total over 15 view files, and to have to maintain or update code is the biggest pain in the butt. We are slowly simplifying the view files by implementing the HTML Snippets insert. Please see the Screenshot provided. Right now, we have already converted two of them. I just want to show you what a time-saving feature this is. Hopefully, this can benefit other developers. We have created the following name syntax to make our lives easier. This is a three-step naming convention. All HTML Snippets that deal with view files start with "ViewFiles" + space + The name of the view file itself, whether it's partial or not + its primary purpose for modification. We had to add Twitter cards to the existing code, and what a simple process it was to go into the appropriate HTML Snippets, make the change, and save without having to log into the main server, download, edit, and upload. Thank you for the early Christmas gift HTML Snippets.jpg (158kb) downloaded 8 time(s).
|
1 user thanked ray22901031 for this useful post.
|
|
|
AbleCommerce Forums
»
AbleCommerce
»
General Questions
»
Inserting an html Snippets into a cshtml file
Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.
Important Information:
The AbleCommerce Forums uses cookies. By continuing to browse this site, you are agreeing to our use of cookies.
More Details
Close