This module provides a way to create templates securely from the admin site.
For more information about the Liquid syntax, please refer to this site: https://shopify.github.io/liquid/
All outputs are encoded into HTML by default.
This means that any HTML reserved chars will be converted to the corresponding HTML entities.
If you need to render some raw HTML chars you can use the raw filter.
All the default filters that are available in the standard Liquid syntax are available in OrchardCore.
On top of that each Orchard module can provide custom filters for their own purpose.
Here is a list of common filters that apply to content items.
Returns the URL of the content item.
Input
{{ Model.ContentItem | display_url }}Output
/blog/my-blog-post
Returns the title of the content item.
Input
{{ Model.ContentItem | display_text }}Output
My Blog Post
Returns the container content item of another content item.
Input
{{ Model.ContentItem | container | display_text }}In this example we assume ContentItem represents a blog post.
Output
Blog
Convert a text into a string that can be used in a URL.
Input
{{ "This is some text" | slugify }}Output
this-is-some-text
Converts a UTC date and time to the local date and time based on the site settings.
Input
{{ "now" | local | date: "%c" }}or
{{ Model.ContentItem.CreatedUtc | local | date: "%c" }}Output
Wednesday, 02 August 2017 11:54:48
Localizes a string using the current culture.
Input
{{ "Hello!" | t }}Output
Bonjour!
Converts a string into a friendly HTML class.
Input
{{ "LandingPage" | html_class }}Output
landing-page
Renders a liquid string template.
Input
{{ Model.ContentItem.Content.Paragraph.Content.Html | liquid }}In this example we assume that Model.ContentItem.Content.Paragraph.Content represents an HtmlField, and Html is the field value.
Output
<p> <img src="/blog/media/kitten.jpg" /> </p>Optionally you can pass a class for model binding.
Converts a Markdown string to HTML.
Input
{{ "### Services" | markdownify }}Output
<h3>Services</h3>Serializes the input value to a json string. To format the json indented, pass the true argument to the liquid filter.
Example:
{{ Model.ContentItem.Content | json }}
{{ Model.ContentItem.Content | json: true }}Converts a json string to a JObject. This can be useful to build collections and iterate over the values in liquid.
Example:
{% capture someCollection %}
[
{"key":"key1", "value":"value1"},
{"key":"key2", "value":"value2"},
]
{% endcapture %}
{% assign jsonObject = someCollection | jsonparse %}
{% for k in jsonObject %}
{{k["key"]}} {{k["value"]}}
{% endfor %}By default the liquid templates have access to a common set of objects.
When available, a zone shape that contains all the shapes generated by the content's parts and fields.
When available, represents the current content item being rendered.
The following properties are available on the ContentItem object.
| Property | Example | Description |
|---|---|---|
Id |
12 |
The id of the document in the database. |
ContentItemId |
4qs7mv9xc4ttg5ktm61qj9dy5d |
The common identifier of all versions of the content item. |
ContentItemVersionId |
4jp895achc3hj1qy7xq8f10nmv |
The unique identifier of the content item version. |
DisplayText |
Blog |
The title of a content item. Can be edited manually by using the TitlePart. |
Number |
6 |
The version number. |
Owner |
admin |
The username of the creator of this content item. |
Author |
admin |
The username of the editor of this version. |
Published |
true |
Whether this content item version is published or not. |
Latest |
true |
Whether this content item version is the latest of the content item. |
ContentType |
BlogPost |
The content type. |
CreatedUtc |
2017-05-25 00:27:22.647 |
When the content item was first created or first published. |
ModifiedUtc |
2017-05-25 00:27:22.647 |
When the content item version was created. |
PublishedUtc |
2017-05-25 00:27:22.647 |
When the content item was last published. |
Content |
{ ... } |
A document containing all the content properties. See below for usage. |
The Content property of a content item exposes all of its parts and fields. It is possible to
inspect all the available properties by evaluating Content directly. It will then render the full document.
<pre>{{ Model.ContentItem.Content }}</pre>The convention is that each Part is exposed by its name as the first level.
If the content item has custom fields, they will be available under a part whose name will match the content type.
For example, assuming the type Product has a Text field named Size, access the value of this field for a
content item as follows:
{{ Model.ContentItem.Content.Product.Size.Text }}Similarly, if the content item has a Title part, we can access it like this:
{{ Model.ContentItem.Content.TitlePart.Title }}Note: This is no longer the recommended way to display the title of a content item.
Use the Model.ContentItem.DisplayText property instead.
Represents the authenticated user for the current request.
The following properties are available on the User object.
| Property | Example | Description |
|---|---|---|
Identity.Name |
admin |
The name of the authenticated user. |
Identity.Claims |
The claims of the authenticated user. |
Checks if the User has permission clearance, optionally on a resource
{{ User | has_permission:"EditContent",Model.ContentItem }}Checks if the user is in role
{{ User | is_in_role:"Administrator" }}Checks if the user has a claim of the specified type
{{ User | has_claim:"email_verified","true" }}
{{ User | has_claim:"Permission","ManageSettings" }}Gives access to the current site settings, e.g Site.SiteName.
| Property | Example | Description |
|---|---|---|
BaseUrl |
The base URL of the site. | |
Calendar |
The site's calendar. | |
Culture |
en-us |
The site's default culture as an ISO language code. |
MaxPagedCount |
0 |
The maximum number of pages that can be paged. |
MaxPageSize |
100 |
The maximum page size that can be set by a user. |
PageSize |
10 |
The default page size of lists. |
SiteName |
My Site |
The friendly name of the site. |
SuperUser |
admin |
The user name of the site's super user. |
TimeZoneId |
America/Los_Angeles |
The site's time zone id as per the tz database, c.f., https://en.wikipedia.org/wiki/List_of_tz_database_time_zones |
UseCdn |
false |
Enable/disable the use of a CDN. |
ResourceDebugMode |
Disabled |
Provides options for whether src or debug-src is used for loading scripts and stylesheets |
CdnBaseUrl |
https://localhost:44300 |
If provided a CDN Base url is prepended to local scripts and stylesheets |
Meta |
The meta to render in the head section of the current theme. |
Represents the current request.
The following properties are available on the Request object.
| Property | Example | Description |
|---|---|---|
QueryString |
?sort=name&page=1 |
The query string. |
ContentType |
application/x-www-form-urlencoded; charset=UTF-8 |
The Content-Type header. |
ContentLength |
600 |
The Content-Length header. |
Cookies |
Usage: Request.Cookies.orchauth_Default |
The collection of cookies for this request. |
Headers |
Usage: Request.Headers.accept |
The request headers. Each property value is an array of values. |
Query |
Usage: Request.Query.sort |
The query value collection parsed from QueryString. Each property value is an array of values. |
Form |
Usage: Request.Form.value |
The collection of form values. |
Protocol |
https |
The protocol of this request. |
Path |
/OrchardCore.ContentPreview/Preview/Render |
The path of the request, unescaped. |
PathBase |
/mytenant |
The base path of the request, unescaped. |
Host |
localhost:44300 |
The Host header. May contain the port. |
IsHttps |
true |
True if the scheme of the request is https. |
Scheme |
https |
The scheme of the request. |
Method |
GET |
The HTTP method. |
Represents the current culture.
The following properties are available on the Culture object.
| Property | Example | Description |
|---|---|---|
Name |
en-US |
The request's culture as an ISO language code. |
Dir |
rtl |
The text writing direction. |
Represents the HttpContext of the current request.
The following properties are available on the HttpContext object.
| Property | Example | Description |
|---|---|---|
Items |
HttpContext.Items["Item1"] |
Returns an item with key Item1 |
Adds key/value to HttpContext.Items collection
{% httpcontext_add_items Item1:"value 1", Item2:"Value2" %}
Removes key from HttpContext.Items collection
{% httpcontext_remove_items "Item1" %}
These filters let you create and filter shapes.
Returns a shape with the specified name as input.
Input
{% assign date_time = "DateTime" | shape_new %}You can also pass properties when creating the shape.
Property names get converted to PascalCase. Ex: prop_name1 can be accessed via Model.PropName1 in the shape template.
{{ Model.Content | shape_new: prop_value1: "some value", prop_value2: 5 }}Renders a shape.
{{ Model.Content | shape_render }}Converts a shape to its string representation. Unlike shape_render, the result of this filter will
be encoded if rendered in the output.
Input
{{ "DateTime" | shape_new | shape_stringify }}
Output
Monday, September 11, 2017 3:29:26 PM
Returns a shape with the added properties.
Property names get converted to PascalCase. Ex: prop_name1 can be accessed via Model.PropName1 in the shape template.
Input
{% assign my_shape = "MyCustomShape" | shape_new | shape_properties: prop_value1: "some value", prop_value2: 5 %}Sets the layout of a view.
Input
{% layout "CustomLayout" %}Internally an alternate is added to the current theme Layout shape.
In a layout, renders the body of the current view.
Input
{% render_body %}In a layout, renders the section with the specified name.
Input
{% render_section "Header", required: false %}Alters and renders the title of the current page.
Input
{% page_title Site.SiteName, position: "before", separator: " - " %}The default parameter is a text that is appended to the current value of the title.
position is where the value is appended, in this example at the beginning.
separator is a string that is used to separate all the fragments of the title.
Removes any alternates from a shape.
Input
{% shape_clear_alternates my_shape %}Adds alternates to a shape.
Input
{% shape_add_alternates my_shape "alternate1 alternate2" %}
{% assign my_alternates = "alternate1,alternate2" | split: "," %}
{% shape_add_alternates my_shape my_alternates %}Removes any wrappers from a shape.
Input
{% shape_clear_wrappers my_shape %}Adds wrappers to a shape.
Input
{% shape_add_wrappers my_shape "wrapper1 wrapper2" %}
{% assign my_wrappers = "wrapper1,wrapper2" | split: "," %}
{% shape_add_wrappers my_shape my_wrappers %}Removes any classes from a shape.
Input
{% shape_clear_classes my_shape %}Adds classes to a shape.
Input
{% shape_add_classes my_shape "class1 class2" %}
{% assign my_classes "class1,class2" | split: "," %}
{% shape_add_classes my_shape my_classes %}Removes any attributes from a shape.
Input
{% shape_clear_attributes my_shape %}Adds attributes to a shape.
Input
{% shape_add_attributes my_shape attr_name1: "value1", attr_name2: "value2" ... %}Adds properties to a shape. This can be useful to pass values from a parent shape.
Property names get converted to PascalCase.
Ex: prop_name1 can be accessed via Model.PropName1 in the shape template.
Input
{% shape_add_properties my_shape prop_name1: "value1", prop_name2: 2 %}Removes a property from a shape by name.
Input
{% shape_remove_property my_shape "prop_name1" %}Sets the type of a shape.
Input
{% shape_type my_shape "MyType" %}Whenever the type is changed, it is recommended to clear the shape alternates before using the shape_clear_alternates tag.
Sets the display type of a shape.
Input
{% shape_display_type my_shape "Summary" %}Whenever the display type is changed, it is recommended to clear the shape alternates first.
Sets the position of a shape.
Input
{% shape_position my_shape "Content:before" %}Sets the tab of a shape.
Input
{% shape_tab my_shape "properties" %}Removes a shape by its name in a Zone.
Input
{% shape_remove_item Model.Content "HtmlBodyPart" %}
{{ Model.Content | shape_render }}In this example, the Model.Content property evaluates to a zone shape, typically from a Content Item shape template, which contains the HtmlBodyPart shape
rendered for the Body Part element. This call will remove the specific shape named HtmlBodyPart.
Replaces the properties of a Pager shape.
Input
{% shape_pager Model.Pager next_class: 'next', next_text: '>>' %}Creates the display shape for a content item. It can be used in conjunction with shape_render
to render a content item.
Input
{{ mycontentitem | shape_build_display: "Detail" | shape_render }}Creates and renders a new shape, with optional caching arguments.
Input
{% shape "menu", alias: "alias:main-menu", cache_id: "main-menu", cache_expires_after: "00:05:00", cache_tag: "alias:main-menu" %}When using the shape tag a specific wrapper and / or alternate can be specified.
{% shape "menu", alias: "alias:main-menu", alternate: "Menu_Footer" %}Sets the caching parameters of a shape.
Input
{% shape_cache my_shape cache_id: "my-shape", cache_expires_after: "00:05:00" %}For more information about the available caching parameters please refer to this section
Renders some HTML content in the specified zone.
Input
{% zone "Header" %}
<!-- some content goes here -->
{% endzone %}The content of this block can then be reused from the Layout using the {% render_section "Header" %} code.
ASP.NET Core MVC provides a set of tag helpers to render predefined HTML outputs. The Liquid module provides a way to call into these Tag Helpers using custom liquid tags.
Invokes the form tag helper of ASP.NET Core.
{% form action:"Create", controller: "Todo", method: "post" %}
... ... ...
{% endform %}Using helper invokes the input tag helper of ASP.NET Core and binds Text of the Model
{% helper "input", for: "Text", class: "form-control" %}Using helper invokes the label tag helper of ASP.NET Core and binds Text of the Model
{% helper "label", for: "Text" %}Using helper invokes the validation_summary tag helper of ASP.NET Core with div
{% helper "div", validation_summary: "All" %}Using helper invokes the validation_for tag helper of ASP.NET Core with span and binds Text of the Model
{% helper "span", validation_for: "Text" %}Invokes the link tag helper from the Orchard.ResourceManagement package.
Invokes the meta tag helper from the Orchard.ResourceManagement package. see this section
Invokes the resources tag helper from the Orchard.ResourceManagement package. see this section
Invokes the script tag helper from the Orchard.ResourceManagement package. see this section
Invokes the style tag helper from the Orchard.ResourceManagement package. see this section
Invokes the a content link tag helper from the OrchardCore.Contents package.
Route data can be added using route_* to tag helper of ASP.NET Core that supports route data using asp-route-* attribute.
In following example, route_returnUrl adds returnUrl to form action.
{% form action: "Update", method: "post", route_returnUrl: Request.Query["returnurl"] %}
... ... ...
{% endform %}In following example, route_todoid adds Model.TodoId to hyperlink.
{% a action: "Delete" , controller: "Todo", class: "btn btn-danger", route_todoid: Model.TodoId %}
Delete
{% enda %}Renders a <hidden> element (antiforgery token) that will be validated when the containing <form> is submitted.
Example
{% antiforgerytoken %}Allows custom Razor TagHelpers to be called from liquid.
For attributes (HtmlAttributeName) use camel-case and replace all - with _.
Helper Tag example
{% helper "mycustomtag", customAttribute: "foo" %}Block Tag example
{% block "mycustomtag", customAttribute: "foo" %}
{% endblock %}To render a liquid string template as IHtmlContent within Razor use the LiquidToHtmlAsync helper extension method on the view's base Orchard property, e.g.:
Input
@await Orchard.LiquidToHtmlAsync((string)Model.ContentItem.Content.Paragraph.Content.Html)In this example we assume that Model.ContentItem.Content.Paragraph.Content represents an HtmlField, and Html is the field value, and we cast to a string, as extension methods do not support dynamic dispatching.
Output
<p> <img src="/media/kitten.jpg" /> </p>Optionally you can pass a class for model binding.
https://github.com/sebastienros/fluid
Copyright (c) 2017 Sebastien Ros
MIT License