Create and edit widget templates
Overview
With Sitefinity CMS, you can control the rendered HTML and modify the markup of MVC widget templates according to your preferences.
All MVC widget templates are located in the ResourcePackages
folder in the root of your project. There is a dedicated folder for the Bootstrap 5 framework, supported by Sitefinity CMS. For more information about frontend frameworks, see Resource packages.
RECOMMENDATION: When modifying a widget template, to make upgrades easier, we recommend that you do not change the default widget templates. If you need to make changes to a default template, it is generally better to create a new template altogether by duplicating the original template in the file system. Only after, you modify and make changes to the duplicated template.
RECOMMENDATION: We recommend that you store the widget templates in your resource package. For information about file priorities, see Priorities for resolving views.
Edit widget templates
You can easily edit widget templates by opening the template in the file system and modifying the markup.
In the following example, you are using the Bootstrap 5 package and you want to modify the markup of the News widget. Perform the following:
- Navigate to
%project-root%/ResourcePackages/Bootstrap5/MVC/Views/News
- Choose the template you want to edit.
Detail or List.
- Edit the file and save it.
- Refresh the browser.
As a result, the template of the News widget is changed.
Create new widget templates
You can create a new widget template either in the file system or via the Sitefinity CMS backend, depending on where your templates are located. We recommend that you store the widget templates in your resource package and create new templates by duplicating and customizing the default templates, instead of directly modifying them. Thus, you do not lose your changes upon upgrade.
NOTE: You can find the default widget templates in the MVC widgets GitHub repository.
In the file system
Generally, MVC widget templates comprise of List and Detail views and you can create new templates for each, following the Naming conventions. Some widgets, such as the Content block, do not have List and Detail views, so you just create the template with a name of your choice and place it in the Mvc » Views » ContentBlock folder.
Create List or Detail views
The following example demonstrates how to create a new List or Detail view for the News widget, when you are using the Bootstrap 5 package:
- Navigate to the
%project-root%/ResourcePackages/Bootstrap5/MVC/Views/News
folder.
- Depending on what view you want to create, duplicate one of the following:
- List template to create a new List view
- Details template to create a new Details view
- Name the new file, using the following convention:
List.[ViewName].cshtml
or Detail.[ViewName].cshtml
NOTE: The ViewName
is the name that is used for the file and how it is displayed in the backend. For more information about naming your files, see Naming conventions.
IMPORTANT: If you are creating a template for a dynamic content widget, make sure you follow the naming convention: ~/MVC/Views/<DynamicContentTypeName>/DesignerView.<ViewName>.cshtml where <DynamicContentTypeName> is the name of the dynamic content type, for example Country. If you created a dynamic type with name Countries, the folder name must be named with singular form.
- Open the file and make your changes.
- Save the file.
Create Content block view
The Content block widget is an example of a widget that does not have Detail or List views. To create a view for the Content block widget, perform the following:
-
In Visual Studio, open your SitefinityWebApp project and click MVC » Views.
-
Create a new folder and name it ContentBlock.
NOTE: The name of the folder must correspond to the name of the widget whose template you want to change. In case, for example, you need to change the template of the Navigation widget, you must name the folder Navigation. For more information, see Naming conventions.
- Open the context menu of folder ContentBlock and add a new file with name Default.cshtml.
The name of the file must be Default
, because the ContentBlockController
renders a view named Default. The extension .cshtml
is valid for Razor view engine.
NOTE: For any other widget, the name of the newly created file must match the name of the template that you are trying to replace - List or Detail.
IMPORTANT: You can use only alphanumeric characters and underscore. For more information about naming your files, see Naming conventions.
- Enter a sample text inside.
For example, enter Testing new template.
- Build the solution and run it.
- Create a new page in Sitefinity CMS and place the ContentBlock widget that is located in the MvcWidgets toolbox section.
As a result, the Testing new template text is displayed.
Create templates outside of packages
In case you are not using a specific package or you want your widgets to use the same view independently of the package used, you can also create views in the root of the project. To do this, perform the following:
- Navigate to your project folder
~\MVC\Views
.
- Create a
<widget type>
folder.
- Add a new view as a
.cshtml
file with <widget type> in its name. Once you create the new template, you can use it when you open the respective widget for editing.
EXAMPLE: If you are creating a template for Login form widget, create a LoginForm folder. Include LoginWidget in the name of your new view file.
Create templates in the backend
To create a new widget template for the Login form widget in the backend, perform the following:
- Navigate to Design » Widget Templates » Create a template.
The Create template form opens.
- In This template is applied to... dropdown box, select the type of widget that you want to create a template for.
- Add the code.
- Name your template.
NOTE: The name of your template must start with the type of view that it applies to. For more information, see Naming conventions. In addition, you can see all built-in views and their names in your project's /ResourcePackages/Bootstrap5/MVC/Views/
folder.
EXAMPLE: If you are creating a template for Login form widget, select LoginForm (MVC) and start your template name with LoginForm, for example LoginForm.<yourtemplatename>. Once you create the new template, you can use it when you open the Login form widget for editing.
EXAMPLE: When creating a template with a List or Detail view, the template name must start with List or Detail, for example List.<yourtemplatename> or Detail.<yourtemplatename>.
-
To save your changes and create the template, click Create this template.