CandidContributions/CanConCloud

v8-9: Replace Razor Helpers

Closed this issue · 0 comments

functions blocks in .cshtml still work, but helpers blocks don't ...

Whilst partial pages, taghelpers or view components might be alternatives to consider we wanted to change as little as possible during this initial migration. So after a bit of trial and error, we opted for this simple solution:

v8

@helper DisplayMetaTags(IMetaTags metaTags)
{
    var metaTitle = metaTags.Value<string>("metaTitle", fallback: Fallback.ToDefaultValue, defaultValue: Model.Name);
    var metaDescription = metaTags.Value<string>("metaDescription", fallback: Fallback.ToDefaultValue, defaultValue: "...");
    var metaKeywords = metaTags.Value<string>("metaKeywords", fallback: Fallback.ToDefaultValue, defaultValue: "...");

    <meta name="title" content="@metaTitle" />
    <meta name="description" content="@metaDescription" />
    <meta name="keywords" content="@metaKeywords" />
}

@DisplayMetaTags(IMetaTags)Model)

v9

@{
    void DisplayMetaTags(IMetaTags metaTags)
    {
        var metaTitle = metaTags.Value<string>("metaTitle", fallback: Fallback.ToDefaultValue, defaultValue: Model.Name);
        var metaDescription = metaTags.Value<string>("metaDescription", fallback: Fallback.ToDefaultValue, defaultValue: "...");
        var metaKeywords = metaTags.Value<string>("metaKeywords", fallback: Fallback.ToDefaultValue, defaultValue: "...");

        <meta name="title" content="@metaTitle" />
        <meta name="description" content="@metaDescription" />
        <meta name="keywords" content="@metaKeywords" />
    }
} 

DisplayMetaTags(IMetaTags)Model);