Jx.Cms开发笔记(三)-Views主题动态切换

 jx   2022-04-14 01:41:32   2537 人阅读  0 条评论

效果展示

我们可以在后台动态切换主题

theme.png

目前Jx.Cms有两个主题,其中一个是默认主题,另一个是仿的Blogs主题。

我们可以通过点击启用按钮来动态切换两个主题。

default.pngblogs.png

实现方法

首先写一个实现IViewLocationExpander接口的类,我这里命名为TemplateViewLocationExpander.

public class TemplateViewLocationExpander : IViewLocationExpander
{
    public void PopulateValues(ViewLocationExpanderContext context)
    {
        context.Values["template"] = ThemeUtil.GetThemeName();
    }

    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
    {
        if (!context.AreaName.IsNullOrEmpty())
        {
            return viewLocations;
        }
        var themeName = context.Values["template"] ?? ThemeUtil.PcThemeName;
        string[] locations = { "/Pages/" + themeName + "/{1}/{0}.cshtml", "/Pages/" + themeName + "/{0}.cshtml", "/Pages/" + themeName + "/Shared/{0}.cshtml", "/Pages/Shared/{0}.cshtml" };
        return locations.Union(viewLocations.Where(x => !x.StartsWith("/Views/")));
    }
}

首先我们在PopulateValues中给context.Values设置一个值,context.Values是一个字典,所以我们可以在里面加一个template的key,如果在PopulateValues中context有变化,就会走到ExpandViewLocations方法中。

ExpandViewLocations中,我们首先把有Area的去除,因为Area内肯定不是我们的主题。

然后查找

string[] locations = { "/Pages/" + themeName + "/{1}/{0}.cshtml", "/Pages/" + themeName + "/{0}.cshtml", "/Pages/" + themeName + "/Shared/{0}.cshtml", "/Pages/Shared/{0}.cshtml" };

这里的内容,就可以到对应的主题下获取内容了。

本文地址:http://www.jvxiang.com/Post?id=7
版权声明:本文为原创文章,版权归 jx 所有,欢迎分享本文,转载请保留出处!

 发表评论

{if $option['ZC_COMMENT_VERIFY_ENABLE']}

{/if}


表情

还没有留言,还不快点抢沙发?