Tag Helpers

Tag Helpers are extensions of existing HTML tags. They can extend the functionality of a tag or even create completely new tags. Smartstore has created a lot of Tag Helpers to write simple and clear code and for better productivity.

How To Use Tag Helpers

To use the Smartstore Tag Helpers in your Views, you'll need to reference specific libraries. You can do this either in your View file or by using _ViewImports.cshtml (recommended). Add the following lines and IntelliSense should recognise the Tag Helpers.

//for public Tag Helpers
@addTagHelper Smartstore.Web.TagHelpers.Public.*, Smartstore.Web.Common

//for shared Tag Helpers
@addTagHelper Smartstore.Web.TagHelpers.Shared.*, Smartstore.Web.Common

//for admin Tag Helpers
@addTagHelper Smartstore.Web.TagHelpers.Admin.*, Smartstore.Web.Common

To learn more about _ViewImports.cshtml, check out this tutorial.

Try it out!

Type: <span s You should see a suggestion for sm-if, which is part of Smartstore's IfTagHelper.

A quick example:

<span sm-if="@Model.visible">Here I am!</span>

Smartstore Tag Helpers

Smartstore differentiates between three kinds of Tag Helpers.

  • Public: Used in frontend

  • Shared: Used in both front- and backend

  • Admin: Used in backend

You can find the code to Smartstore Tag Helpers in Smartstore.Web.Common/TagHelpers/.

Public

Captcha Tag Helper

The CaptchaTagHelper creates a CAPTCHA.

It also supports the sm-enabled attribute.

ConsentScript Tag Helper

The ConsentScriptTagHelper allows a script to be loaded immediately after cookie consent is given. Otherwise it is loaded after a page refresh.

  • sm-consent-type: The type of cookie the user needs to accept.

Honeypot Tag Helper

The HoneypotTagHelper is Smartstore's cyber-security implementation of the Honeypot mechanism.

It also supports the sm-enabled attribute.

Shared

Attributes Tag Helper

The AttributesTagHelper adds attributes to the element. It can be used in two ways:

  • Adding a collection of attributes to the element.

  • Adding an attribute, if it evaluates to true.

CollapsedContent Tag Helper

The CollapsedContentTagHelper collapses the element to a maximum height. It also adds Show more or Show less to the element.

To specify the maximum number of pixel you want to show, add the sm-max-height attribute. Otherwise the catalog's default setting will be used.

Confirm Tag Helper

The ConfirmTagHelper adds a confirm button. There are many ways to customise it.

It also supports these attributes:

  • action: Action to execute, if confirmed. Default: Delete

  • controller: Controller to search for action. Default: ViewContext.RouteData.Values.GetcontrollerName()

  • form-post-url

  • type: Type of confirm action. Default: Delete

  • backdrop: Button has a backdrop. Default: true

  • title: Title of the dialog.

  • accept-button-color: Color of the accept button.

  • accept-text: Custom accept button text.

  • cancel-text: Custom cancel button text.

  • center: Dialog is centered vertically. Default: true

  • center-content: Dialog is centered. Default: false

  • size: Size of the dialog. Possible values: Small, Medium, Large, Flex, FlexSmall. Default: Medium

  • message: Custom display message.

  • icon: Icon class.

  • icon-color: Custom icon color.

FileIcon Tag Helper

The FileIconTagHelper display a file icon.

It also supports these attributes:

  • label: Custom label. Default: the files extension

FileUploader Tag Helper

The FileUploaderTagHelper adds a highly customisable way to upload files.

Here is an excerpt from Smartstore.Web/Views/Customer/Avatar.cshtml to show this.

The default value for the attribute media-path is SystemAlbumProvider.Files.

If Tag Helper

The IfTagHelper adds a conditional attribute to the element. The output is suppressed, if the condition evaluates to false.

MinifyTagHelper Tag Helper

The MinifyTagHelper allows an inline script to be minified automatically, before being loaded. Default: false

This should not be used if the script contains dynamic content like:

  • SessionId

  • A randomly generated ID

  • User related data

  • Model data

The Tag-Helper has no effect in combination with the src attribute.

SuppressIfEmpty Tag Helper

The SuppressIfEmptyTagHelper adds a conditional attribute to the element. The output is suppressed, if the condition is true and the element is empty or only contains whitespaces.

SuppressIfEmptyZone Tag Helper

The SuppressIfEmptyZoneTagHelper suppresses the output, if the targeted zone is empty or only contains whitespaces.

Here is an excerpt from Smartstore.Web/Views/ShoppingCart/Partials/OffCanvasShoppingCart.cshtml to show this.

TagName Tag Helper

The TagNameTagHelper changes the tag at runtime.

Widget Tag Helper

The WidgetTagHelper adds HTML content and injects it into a zone. More information can be found in Widgets.

The key attribute makes sure only one instance of the widget is included in the zone.

Zone Tag Helper

The ZoneTagHelper defines an zone for widgets to inject content. More information can be found in Widgets.

It also supports these attributes:

  • model: Declare what model to use within the zone.

  • replace-content: Replace content, if at least one widget is rendered.

  • remove-if-empty: Remove the root zone tag, if it has no content. Default: false

  • preview-disabled: If true, the zone preview will not be rendered. This is important for script or style zones that should not be printed. Default: false

  • preview-class: Additional CSS classes for the span tag. For example, position-absolute to better control the layout flow.

  • preview-style: CSS style definitions. For example, to set an individual max-width.

Controls

EntityPicker Tag Helper

The EntityPickerTagHelper adds an element to pick one or more entities from a larger group.

It also supports these attributes:

  • entity-type: Entity type to be picked. Default: product

  • target-input-selector: Identifier of the target input, defined by field-name.

  • caption: Caption of the dialog.

  • icon-css-class: Icon of the button that opens the dialog. Default: fa fa-search

  • dialog-title: Title of the dialog.

  • disable-grouped-products: Disable search for grouped products.

  • disable-bundle-products: Disable search for bundle products.

  • disabled-entity-ids: Ids of disabled entities.

  • selected: Ids of selected entities.

  • enable-thumb-zoomer: Enables the thumb zoomer.

  • highlight-search-term: Highlight search term in search results. Default: true

  • max-items: Maximum number of selectable items.

  • append-mode: Append selected entity ids to already chosen entities. Default: true

  • delimiter: Entity id delimiter. Default: ,

  • field-name: Fieldname of [target-input-selector] to paste selected ids. Default: id

  • ondialogloading: JS function called before dialog is loaded.

  • ondialogloaded: JS function called after dialog is loaded.

  • onselectioncompleted: JS function called after selection.

Pagination Tag Helper

The PaginationTagHelper adds pagination.

Pass an IPageable object that should be paged, using the sm-list-items attribute.

It also supports these attributes:

  • sm-list-items

  • sm-alignment: Element alignment. Possible values: Left, Centered, Right. Default: Centered

  • sm-size: Element size. Possible values: Mini, Small, Medium, Large. Default: Medium

  • sm-style: Element style. Possible values: Pagination, Blog

  • sm-show-first: Always show first page. Default: false

  • sm-show-last: Always show last page. Default: false

  • sm-show-next: Always show next page. Default: true

  • sm-show-previous: Always show previous page. Default: true

  • sm-max-pages: Maximum number of displayed pages. Default: 8

  • sm-skip-active-state

  • sm-item-title-format-string

  • sm-query-param: Default: page

TabStrip Tag Helper

The TabStripTagHelper adds a tab strip. It works with the TabTagHelper.

The TabStripTagHelper also supports these attributes:

  • sm-hide-single-item: Hide navigation on single tab. Default: true

  • sm-responsive: Collapse navigation on smaller screens. Default: false

  • sm-nav-position: Position of the navigation. Possible values: Top, Right, Below, Left Default: Top

  • sm-nav-style: Style of the navigation. Possible values: Tabs, Pills, Material.

  • sm-fade: Add fade animation. Default: true

  • sm-smart-tab-selection: Reselect active tab on reload. Default: true

  • sm-onajaxbegin

  • sm-onajaxsuccess

  • sm-onajaxfailure

  • sm-onajaxcomplete

  • sm-publish-event: Fires the TabStripCreated event. Default: true

The TabTagHelper also supports these attributes:

  • sm-name

  • sm-title

  • sm-selected

  • sm-disabled

  • sm-visible: Tab visibility. Default: true

  • sm-hide-if-empty: Default: false

  • sm-ajax: Load content with AJAX

  • sm-icon

  • sm-icon-class

  • sm-badge-text

  • sm-badge-style: Badge Style. Possible values: Secondary, Primary, Success, Info, Warning, Danger, Light, Dark.

  • sm-image-url

  • sm-adaptive-height: Responsive height. Default: false

Forms

AjaxForm Tag Helper

The AjaxFormTagHelper adds unobtrusive AJAX to a form.

It also supports these attributes:

  • sm-ajax: The form is an unobrusive AJAX form.

  • sm-confirm: Custom confirm message.

  • sm-onbegin

  • sm-oncomplete

  • sm-onfailure

  • sm-onsuccess

  • sm-allow-cache

  • sm-loading-element-id

  • sm-loading-element-duration

  • sm-update-target-id

  • sm-insertion-mode: Mode of insertion of the response. Possible values: Replace, InsertBefore, InsertAfter, ReplaceWith.

Further information can be found in this explanation.

ColorBox Tag Helper

The ColorBoxTagHelper adds a color-picker. It is used under the hood of the SettingEditorTagHelper to display colors.

Editor Tag Helper

The EditorTagHelper adds a customisable input field. It works similiar to the SettingEditorTagHelper.

To add more ViewData attributes, use asp-additional-viewdata.

FormControl Tag Helper

The FormControlTagHelper adds labels and CSS classes to form elements.

It also supports these attributes:

  • sm-append-hint

  • sm-ignore-label

  • sm-switch: Process checkboxes as switches. Default: true

  • sm-control-size: Size of the element. Default: Medium

  • sm-plaintext: View as plaintext.

  • sm-required

Hint Tag Helper

The HintTagHelper displays the localised Hint of resource passed in asp-for.

InputPassword Tag Helper

The InputPasswordTagHelper adds the ability to toggle the visibility of a password input.

  • sm-enable-visibility-toggle: Displays an eye icon to toggle the visibility of the password. Default: true

NumberInput Tag Helper

The NumberInputTagHelper extends the number-input's customisability and styles the element.

TripleDatePicker Tag Helper

The TripleDatePickerTagHelper adds a customisable date-picker displaying the day, month and year.

The default value for control-size is Medium.

Media

All media Tag Helpers support the following attributes: sm-file, sm-file-id, sm-url-host.

All image-based Tag Helpers (ImageTagHelper, MediaTagHelper, ThumbnailTagHelper, VideoTagHelper) support the following attributes: sm-model, sm-size, sm-width, sm-height, sm-resize-mode, sm-anchor-position and sm-no-fallback.

Audio Tag Helper

The AudioTagHelper adds an audio element.

Image Tag Helper

The ImageTagHelper adds an image with attributes used in Model or the File.

Media Tag Helper

The MediaTagHelper adds a suitable tag for a given media type.

Thumbnail Tag Helper

The ThumbnailTagHelper adds a thumbnail of a media file.

Video Tag Helper

The VideoTagHelper adds a video element.

Admin

BackTo Tag Helper

The BackToTagHelper adds a left arrow icon to a link.

DataGrid Tag Helper

There are different types of DataGrid Tag Helpers. They all work together to extend the functionality of the Grid Tag Helper.

Here is an excerpt from Smartstore.Web/Areas/Admin/Views/ActivityLog_Grid.ActivityLogs.cshtml to show this.

SettingEditor Tag Helper

The SettingEditorTagHelper provides automatic HTML-Input type Mapping.

It automatically checks the type of the variable passed in asp-for and looks for an appropriate HTML input. Additionally it offers model binding and matching.

SmartLabel Tag Helper

The SmartLabelTagHelper displays a label and an optional hint.

It also supports these attributes:

  • sm-ignore-hint: Hint will be ignored. Default: true

  • sm-text: Custom label text.

  • sm-hint: Custom label hint.

Further Reading

If you are interested in writing your own TagHelper or learning more about them, follow this Microsoft Tag Helper tutorial.

Last updated

Was this helpful?