示例#1
0
    /// <summary>
    /// Generates meta tags for the given content.
    /// </summary>
    /// <param name="app">The application service</param>
    /// <param name="content">The content</param>
    /// <param name="meta">If meta tags should be generated</param>
    /// <param name="opengraph">If open graph tags should be generated</param>
    /// <param name="generator">If generator tag should be generated</param>
    /// <returns>The meta tags</returns>
    public static HtmlString MetaTags(this IApplicationService app, IMeta content, bool meta = true, bool opengraph = true, bool generator = true)
    {
        var sb = new StringBuilder();

        if (meta)
        {
            // Generate meta tags
            if (!string.IsNullOrWhiteSpace(content.MetaKeywords))
            {
                sb.AppendLine($"<meta name=\"keywords\" value=\"{ content.MetaKeywords }\">");
            }
            if (!string.IsNullOrWhiteSpace(content.MetaDescription))
            {
                sb.AppendLine($"<meta name=\"description\" value=\"{ content.MetaDescription }\">");
            }
        }

        if (generator)
        {
            // Generate generator tag
            sb.AppendLine($"<meta name=\"generator\" value=\"Piranha CMS { Piranha.Utils.GetAssemblyVersion(typeof(Piranha.App).Assembly) }\">");
        }

        if (opengraph)
        {
            // Generate open graph tags
            sb.AppendLine($"<meta name=\"og:type\" value=\"article\">");
            sb.AppendLine($"<meta name=\"og:title\" value=\"{ OgTitle(content) }\">");
            if (content.OgImage != null && content.OgImage.HasValue)
            {
                sb.AppendLine($"<meta name=\"og:image\" value=\"{ app.AbsoluteUrl(content.OgImage) }\">");
            }
            if (!string.IsNullOrWhiteSpace(OgDescription(content)))
            {
                sb.AppendLine($"<meta name=\"og:description\" value=\"{ OgDescription(content) }\">");
            }
        }
        return(new HtmlString(sb.ToString()));
    }