我正在尝试创建一个对我的设计师非常灵活的Catalyst应用程序。我的意思是,如果他们需要添加一个静态内容的新页面,我不需要进入Catalyst
并添加该动作,然后才能为该页面创建模板。如果他们添加了任何名为name.html
的模板,那么即使没有name
操作,Catalyst
应用也可以显示http://www.domain.com/name.html
。目前,我有在根控制器,看起来像这样的子程序:未找到催化剂模板工具包模板返回404
sub any_template :Regex('(.+)\.html$') {
my ($self, $c) = @_;
$c->stash(
template => $c->action . '.html',
);
}
,这使得它,以便以“.html”结尾的任何请求都将寻找该名称的模板。但是,如果对不存在的模板请求url,Template Toolkit会抛出一个错误,指出它无法呈现模板,这是合理的,因为它不存在。但是,如果模板不存在,我希望能够显示404未找到的页面。是否有可能做到这一点?或者有没有人有更好的方法,让你可以有模板页面,而无需添加一个空的动作到你的Catalyst
应用程序?我知道可以使用perl的-e文件测试来查看模板是否存在于文件系统中,但有没有更好的方法可以做到这一点?谢谢!
UPDATE
这是我目前的包装是什么样子:
[%
debug("Applying HTML page layout wrappers to $template.name\n");
content WRAPPER "$host/site/html" + "$host/site/layout";
-%]
这主要是受TTSite产生。无论如何,我可以做类似于RET建议的东西,在那里我可以使用try catch块来使用404页面,如果失败了?
UPDATE
我试着更新我的包装的模板这样:
[%-
TRY;
content WRAPPER "$host/site/html" + "$host/site/layout";
CATCH file;
"File Error! $error.info";
CATCH;
"Error: $error.info!";
END;
-%]
而且它会说,那里是当它无法找到页面文件错误:
Couldn't render template test.html: file error - test.html: not found
但是,它不会去我的任何catch块。我应该把它放在别的地方吗?
UPDATE
的Template Toolkit单证说:
Note that the DEFAULT option (disabled by default) allows you to specify a default
file to be used any time a template file can't be found. This will prevent file
exceptions from ever being raised when a non-existant file is requested (unless,
of course, the DEFAULT file your specify doesn't exist). Errors encountered once
the file has been found (i.e. read error, parse error) will be raised as file
exceptions as per usual.
这正是我期待的,但目前还没有如何使用这样的例子。有谁知道如何使用DEFAULT
选项?
但是后来我失去了使用模板系统的好处,其中只需要更改的内容位于页面中间。我希望我的设计师不必在任何地方重复相同的页眉,页脚等。所以它是'静态'的内容,但我仍然想要像Template Toolkit这样的模板系统的好处。 – srchulo