ServiceStack Template questions

We’re evaluating ServiceStack Templates and have some questions that we didn’t seem to find in the docs or we missed them.

The templates indicated on http://templates.servicestack.net/docs/ seem to always have the title as static. Is this able to be dynamic? Example: ALFKI should show the actual company name in title and not the generic ‘Company Title’

<!--
title: static title
-->

Can we push any custom attribute (e.g. most html meta/head tags) up to the layout from the template?
e.g.
<meta name="keywords" content="HTML, CSS, XML, XHTML, JavaScript">
We need the ability to greatly control SEO and structured data tags.

We didn’t notice a sample app with Templates as Model View Controller. Did we miss it? Looking for a Northwind type sample.

Can we have templates that are loaded via a database instead of physical files? Can template partials?

Is it possible with templates to do A/B testing with a whole page or with template partials? We’re concerned that maybe the caching would interfere. Understandably we’d probably have to code for the scenario.

Since templates can have SQL, is there a way to prevent a user from using SQL or other security controlled values in a template? Say we allow templates to be created by a client, we wouldn’t want them to have access to anything, but what we provide to them. Would it limit our developers if we had these limits in the same web app? The filters looked global in how they were set.

We didn’t see much with Azure Web App Service for deployment of Templates. Is that because there are issues or because there are no issues? We’re looking a regular publish release, not docker/container releases.

No the title page argument needs to be static as it needs to be accessible before and without the page being evaluated.

Dynamic Titles

For dynamic titles you could assign title variable within your page which will be available in the _layout after it’s evaluated (e.g. after {{page}}).

If you wanted a dynamic page title you could render it within your page and use CSS to position it where you want.

If you want to set the window.title you’d need to use JavaScript, e.g:

<script>window.title = "{{title}}";</script>

Only using static page arguments above, as they’re accessible before the page is evaluated.

The links to the page show the implementation which is used by the example at the bottom.

Pages can be sourced from any Virtual File System provider, the AWS + Azure Pure Cloud Apps shows examples of using S3 and Azure Blob Storage. So you could implement a RDBMS VFS provider, but there isn’t one built-in.

Otherwise have a look at the source code of blog.web-app.io which stores the content of each post in an SQLite database that it renders using evalTemplate.

Partials are just pages which are sourced from the same VFS provider and you can also define inline partials.

I don’t know what feature you’re looking for, there’s no notion of A/B in Templates but you can use blocks for conditional logic.

You can control what plugins, filters, blocks, etc templates rendered using evalTemplates (i.e. what blog.web-app.io uses) have access to, e.g. blog content can use the markdown filters/blocks in MarkdownTemplatePlugin with:

{{ content | evalTemplate({use:{plugins:'MarkdownTemplatePlugin'}}) }}

In addition to plugins, you can use filters or blocks to give it access to specific filters/blocks which are imported from the current context executing the page (e.g. TemplatesPageFeature in .html pages).

By default evalTemplate uses an empty context, i.e. new TemplateContext() so it doesn’t have access to the DB Fitlers that need to be explicitly registered.

For trusted content you can also evaluate it using the same context (e.g. TemplatesPageFeature) with:

{{ content | evalTemplate({context:true}) }}

No developers use what features are registered in the AppHost’s TemplatesPageFeature plugin whilst evalTemplate content is rendered in a new/empty context where you can control what they have access to. But as features are imported from the containing context, you can only give access to features registered in TemplatesPageFeature.

We use AWS not Azure, so we only have a Docker example for Azure. But Templates is just a pure library with no external dependencies, build tools, etc. So you’d just use whatever method you’re using to deploy Web Apps now, there’s no extra consideration needed for Templates.

We’ve noticed that the Template samples dotnet-new templates are unable to run in IIS Express from VS 2017. Instead we have to run as the project using the project. What is the recommended way to test/run in IIS Express / IIS?

Which templates samples? For any .NET Core Apps you use dotnet run.

Ahh maybe that is the disconnect we’re having. We have not done anything in .NET Core before.

We did dotnet-new templates TemplatesSS and then opened in VS 2017. Clicked play button for “IIS Express” but get that error message.

If we change IIS Express to the project name ‘TemplateSS’ then it runs as a console app.

I did not see how to dotnet run within VS 2017 unless that is what the play button (F5) for ‘TemplateSS’ does?

I would assume Core apps can be published to IIS and Azure App Service without being in a container…

We heard Core apps are more performant than standard as well as the ability to run in multiple platforms. Initially tho we would use our current Azure App Service website.

Ok if you’re trying to run it with IIS Express in the issue is due to multiple URL bindings, from: Invalid application pool name

The issue is from VS.NET trying to run the Web App under the default .NET Core 2.1 multiple bindings, i.e. http://localhost:5000/;https://localhost:5001/, but will work if you change it to an unused port, e.g:

It will also work on http://localhost:5000/ if you run VS .NET as Administrator where I’m assuming has permissions to override any previous registration on that port.

From what I can tell VS .NET and IIS Express doesn’t support .NET Core 2.1’s default use of multiple http/https bindings.
Rider/VS Code doesn’t have this issue as it just runs the .NET Core App normally.

ASP.NET Framework Templates project

If you did want to create a .NET Core App you can instead use the ServiceStack ASP.NET Templates with Bootstrap project from ServiceStackVS:

Troubleshooting

If you still can’t run the project after changing the port, close the solution and delete the .vs/ folder than try re-opening and rerunning the project again.

I run .net core apps in Azure App Services (windows) without containers. You can easily test it by publishing them via visual studio although I use appveyor for our continuous build service.

@mythz It’s looking like we might need to use SS Razor / MVC for most pages. Is it possible to inject SS Templates partials into a SS Razor page? Example: Have multiple product pages. Product page layouts and most of the page is rendered using SS Razor. However could part of the page, like the the product description be included in the SS Razor page using SS Template partial? I’m assuming not since Razor pages are usually pre-compiled. We’re looking at if we can make it easier for marketing people to “code” certain pages without having to wait for dev.

You can generate a string an embed it as you would any C# method returning a string, i.e… by creating a context and using it to evaluate content:

var context = new TemplateContext().Init();
var output = context.EvaluateTemplate("{{ 12.34 | currency }}");

Further customization is available through the normal TemplateContext and PageResult APIs documented throughout http://templates.servicestack.net, e.g. you can render a page with layout with:

context.VirtualFiles.Write("_layout.html", "I am the Layout: <b>{{ page }}</b>");
context.VirtualFiles.Write("page.html", "I am the Page");

var pageResult = new PageResult(context.GetPage("page"));
string output = await pageResult.RenderToStringAsync();