Spark View Engine:如何设置默认母版页名称?

发布于 2024-10-09 14:42:00 字数 477 浏览 5 评论 0原文

我将 Spark View Engine 与嵌套母版页一起使用。我有 Application.spark 它定义了网站的基本布局。然后还有其他几个母版,它们本身使用 Application.spark 作为母版页(Default.spark、SinlgeColumn.spark、Gallery.spark...)

如果在视图文件中未指定母版页,则自动选择 Application.spark由 Spark 视图引擎提供。由于我的几乎所有页面都使用“Default.spark”作为主页面,有没有办法全局配置它?

其他可能性是:

  1. 在每个 Spark 文件中单独设置 master。但这确实很烦人。

  2. 重命名我的主文件 (Default.spark <-> Application.spark),但这在命名中确实没有任何意义。

I use Spark View Engine with nested master pages. I have Application.spark which defines the basic layout of the website. Then there are several other masters which themselves use Application.spark as master page (Default.spark, SinlgeColumn.spark, Gallery.spark, ...)

If no master page is specified in a view file, then automatically Application.spark is choosen by the Spark View Engine. Since almost all my pages use "Default.spark" as master, is there a way to configure this globally?

The other possibilities would be:

  1. Set the master in each spark file individually <use master="Default" />. But that's really annoying.

  2. Rename my master files (Default.spark <-> Application.spark) but that really doesn't make any sense in naming.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

書生途 2024-10-16 14:42:00

有两种方法可以解决您的问题:

  1. 减少工作量 - 但增加重复性。
  2. 更多的工作,但赶走了你喜欢的约定。

更少的工作

对于任何 ActionResult,您只需在 return 语句中添加第二个参数即可指定要使用的母版页的名称:

return View("Gallery", "Default");

第一个参数名称是视图,第二个参数名称表示母版页覆盖。 ..但这确实意味着您需要在各处重复它,而且这并不比您之前的好多少。

更多工作

Spark 定位要使用的母版页的方式是通过框架中的以下代码:

protected virtual IEnumerable<string> PotentialDefaultMasterLocations(string controllerName, IDictionary<string, object> extra)
    {
        return ApplyFilters(new[]
                                {
                                    "Layouts\\" + controllerName + ".spark",
                                    "Shared\\" + controllerName + ".spark",
                                    "Layouts\\Application.spark",
                                    "Shared\\Application.spark"
                                }, extra);
    }

注意其中的硬编码 Application.spark - 这是 Spark 约定。看来您想要做的是覆盖此方法并放入类似的内容:

protected virtual IEnumerable<string> PotentialDefaultMasterLocations(string controllerName, IDictionary<string, object> extra)
    {
        return ApplyFilters(new[]
                                {
                                    "Layouts\\" + controllerName + ".spark",
                                    "Shared\\" + controllerName + ".spark",
                                    "Layouts\\Default.spark",
                                    "Shared\\Default.spark"
                                    "Layouts\\Application.spark",
                                    "Shared\\Application.spark"
                                }, extra);
    }

然后它会在找到 Application.spark 或您之前找到您的 Default.spark可以完全摆脱 Application.spark 如果它不适合您并且您更喜欢您的约定...

为了覆盖它,您需要做的就是创建一个继承的类来自 Spark.Web.Mvc.DefaultDescriptorBuilder 并覆盖上面提到的方法,并在注册视图引擎时使用它,如下所示:

public static void RegisterViewEngine(ICollection<IViewEngine> engines)
{
    var services = SparkEngineStarter.CreateContainer();
    services.SetServiceBuilder<IDescriptorBuilder>(
        c => new MyDescriptorBuilder());
    SparkEngineStarter.RegisterViewEngine(services);
}

这意味着您现在可以指示 Spark 应该在哪里查找主视图并名字会是什么。

希望有帮助,
祝一切顺利,
罗伯特·格雷

There are two ways to solve your issue:

  1. Less work - but more repetitive.
  2. More work, but drives out your preferred convention.

Less Work

With any ActionResult, you can simply add a second parameter to the return statement to specify the name of the master to use:

return View("Gallery", "Default");

the first parameter name is the view and the second signifies a master page override...but that does mean you need to repeat it everywhere, and this isn't much better than what you had before.

More Work

The way that Spark locates the master page to use is via the following code in the framework:

protected virtual IEnumerable<string> PotentialDefaultMasterLocations(string controllerName, IDictionary<string, object> extra)
    {
        return ApplyFilters(new[]
                                {
                                    "Layouts\\" + controllerName + ".spark",
                                    "Shared\\" + controllerName + ".spark",
                                    "Layouts\\Application.spark",
                                    "Shared\\Application.spark"
                                }, extra);
    }

Notice the hardcoded Application.spark in there - that's a Spark convention. What it seems that you want to do is override this method and put something like this in instead:

protected virtual IEnumerable<string> PotentialDefaultMasterLocations(string controllerName, IDictionary<string, object> extra)
    {
        return ApplyFilters(new[]
                                {
                                    "Layouts\\" + controllerName + ".spark",
                                    "Shared\\" + controllerName + ".spark",
                                    "Layouts\\Default.spark",
                                    "Shared\\Default.spark"
                                    "Layouts\\Application.spark",
                                    "Shared\\Application.spark"
                                }, extra);
    }

Then it will find your Default.spark before it finds Application.spark or you could get rid of the Application.spark entirely if it doesn't ring true for you and you prefer your conventions...

In order to override this, all you need to do is create a class that inherits from Spark.Web.Mvc.DefaultDescriptorBuilder and override that method mentioned above and use it when you register the view engine like so:

public static void RegisterViewEngine(ICollection<IViewEngine> engines)
{
    var services = SparkEngineStarter.CreateContainer();
    services.SetServiceBuilder<IDescriptorBuilder>(
        c => new MyDescriptorBuilder());
    SparkEngineStarter.RegisterViewEngine(services);
}

This now means that you can now direct where Spark should look for the Master views and what the names will be.

Hope that helps,
All the best,
RobertTheGrey

横笛休吹塞上声 2024-10-16 14:42:00

不知道它是否有效,但尝试添加

<use master="Default" />

一个名为 Views\_global.spark 的文件。它会自动包含在所有视图中。

我通常将命名空间和全局变量添加到我的 _global.spark 中。

示例:

<use namespace="System.Collections.Generic"/>
<use namespace="System.Web.Mvc.Html"/>

<global type="string" Title="'My default title'"/>

另一个很酷的功能

由于您是新的 Spark 用户,我想向您展示另一个很酷的 Spark 功能。

编写而不是

Html.LabelFor(Model => Model.FirstName);

您可以

<Label For="FirstName" />

编写使视图更清晰,对吧?魔法是通过一种叫做绑定的东西来完成的。您所需要做的就是创建一个 Views\Bindings.xml 文件并向其添加绑定。

在此博客文章中了解更多信息:http: //blog.robertgreyling.com/2010/08/spark-bindings-are-you-tired-of-eating.html

Don't know if it works, but try adding

<use master="Default" />

in a file called Views\_global.spark. It's automatically included in all views.

I usally add namespaces and global vars to my _global.spark.

Example:

<use namespace="System.Collections.Generic"/>
<use namespace="System.Web.Mvc.Html"/>

<global type="string" Title="'My default title'"/>

Another cool feature

Since you are a new spark user I would like to show you another cool spark feature.

Instead of writing

Html.LabelFor(Model => Model.FirstName);

you can write

<Label For="FirstName" />

Makes the views a bit cleaner, huh? The magic is done with something called bindings. All you need to do is to create a Views\Bindings.xml file and add bindings to it.

Read more in this blog entry: http://blog.robertgreyling.com/2010/08/spark-bindings-are-you-tired-of-eating.html

美胚控场 2024-10-16 14:42:00

而不是使用 Application.spark 作为您的基础,并使用 Default.spark 作为您的第二级......
将 Default.spark 重命名为 Application.spark,将 application.spark 重命名为 BaseApplication.spark 等其他名称。

这样,默认情况下会选择您的 application.spark,但如果您想覆盖它,您可以引用 BaseApplication.spark。

有关示例,请参阅三通道渲染部分。
http://sparkviewengine.com/documentation/master-layouts#Threepassrendering

Instead of using Application.spark as your base, and Default.spark as your 2nd level...
rename Default.spark to Application.spark and the application.spark to something else like BaseApplication.spark.

This way, by default your application.spark is picked up, but if you wish to override it you can referece the BaseApplication.spark.

See the three pass rendering section for an example.
http://sparkviewengine.com/documentation/master-layouts#Threepassrendering

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文