我正在嘗試創建一個對我的設計師非常靈活的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