【问题标题】:Perl Catalyst - Couldn't render template..........not foundPerl Catalyst - 无法渲染模板............找不到
【发布时间】:2013-12-31 12:25:10
【问题描述】:

我在开发服务器中遇到的错误:

[info] *** Request 2 (0.000/s) [681] [Thu Dec 12 21:05:39 2013] ***
[debug] Path is "homescreen"
[debug] "GET" request for "homescreen" from "192.168.1.100"
[debug] Rendering template "homescreen/homescreen.tt2"
[error] Couldn't render template "homescreen/homescreen.tt2: file error - homescreen/homescreen.tt2: not found"
[error] Couldn't render template "homescreen/homescreen.tt2: file error - homescreen/homescreen.tt2: not found"
[debug] Response Code: 500; Content-Type: text/html; charset=utf-8; Content-Length: 14312
[info] Request took 0.033915s (29.485/s)
.------------------------------------------------------------+-----------.
| Action                                                     | Time      |
+------------------------------------------------------------+-----------+
| /homescreen                                                | 0.000341s |
| /end                                                       | 0.014055s |
|  -> Myproject::View::HTML->process                         | 0.013049s |
'------------------------------------------------------------+-----------'

我在做什么:

我有以下Controller/Homescreen.pm

package Myproject::Controller::Homescreen;

use strict;
use warnings;
use parent 'Catalyst::Controller';
use Data::Dumper;
use JSON;

__PACKAGE__->config->{namespace} = '';

sub homescreen :Path('/homescreen') :Args(0)  {

        my ( $self, $c ) = @_;
        print STDERR "IN THE HOMESCREEN ACTION\n";


        $c->stash({template => 'homescreen/homescreen.tt2',
                   title => 'Home Screen'
                 });
}

我有以下View/HTML.pm

package Myproject::View::HTML;
use Moose;
use namespace::autoclean;

extends 'Catalyst::View::TT';

__PACKAGE__->config({
    #Changed default TT extension to TT2
    TEMPLATE_EXTENSION => '.tt2',
    render_die => 1,
});

我有以下lib/Myproject.pm

__PACKAGE__->config(
    name => 'Myproject',
    # Disable deprecated behavior needed by old applications
    disable_component_resolution_regex_fallback => 1,
    #enable_catalyst_header => 1, # Send X-Catalyst header
);

__PACKAGE__->config(
        #Configure the view
        'View::HMTL' => {
                #Set the location for TT files
                INCLUDE_PATH => [
                        __PACKAGE__->path_to( 'root', 'src' ),
                ],
        },
);


# Start the application
__PACKAGE__->setup();

然后我有一个 root/src/homescreen/homescreen.tt2 与我的 Catalyst 目录,其中包含我所有的 html 代码(最终它将使用模板工具包,但目前它是纯粹的 html 和 javscript 代码,我知道没问题)。

我在浏览器的应用程序页面上遇到的错误是:

Couldn't render template "homescreen/homescreen.tt2: file error - homescreen/homescreen.tt2: not found" 

我尝试在我的 HTML.pm 视图中使用 DEBUG => 'undef' 来帮助调试,但我似乎没有得到任何额外的输出。

我可能忽略了一些非常明显的东西,但我无法弄清楚它是什么。

更新

我刚刚在浏览器调试屏幕的Config 部分注意到以下内容:

配置

  do {
  my $a = {
    "Action::RenderView" => {
      ignore_classes => [
                          "DBIx::Class::ResultSource::Table",
                          "DBIx::Class::ResultSourceHandle",
                          "DateTime",
                        ],
      scrubber_func  => sub { ... },
    },
    "disable_component_resolution_regex_fallback" => 1,
    "home" => "/home/fred/Myproject",
    "name" => "Myproject",
    "Plugin::ConfigLoader" => {},
    "Plugin::Static::Simple" => {
      debug => 1,
      dirs => [],
      ignore_dirs => [],
      ignore_extensions => ["tmpl", "tt", "tt2", "html", "xhtml"],   <---- IS THIS SIGNIFICANT AT ALL?
      include_path => [
        bless({
          dirs => ["", "home", "fred", "Myproject", "root"],
          file_spec_class => undef,
          volume => "",
        }, "Path::Class::Dir"),
      ],
      mime_types => {},
      mime_types_obj => bless({}, "MIME::Types"),
      no_logs => 1,
    },
    "root" => 'fix',
    "stacktrace" => { context => 3, verbose => 0 },
    "static" => 'fix',
    "View::HMTL" => {
      INCLUDE_PATH => [
        bless({
          dirs => ["", "home", "fred", "Myproject", "root", "src"],
          file_spec_class => undef,
          volume => "",
        }, "Path::Class::Dir"),
      ],
    },
  };
  $a->{"root"} = $a->{"Plugin::Static::Simple"}{include_path}[0];
  $a->{"static"} = $a->{"Plugin::Static::Simple"};
  $a;
}

我认为这意味着它忽略了我的模板文件,因为它具有 .tt2 文件扩展名?

但是,我没有在我的 Catalyst 项目中的任何位置设置此 ignore_extensions 属性?这是我的问题的原因还是完全不相关的原因?

【问题讨论】:

  • 你是root用户吗?也许您的网络服务器没有以 root 身份运行,因此找不到模板。尝试加载CWD 并在控制器中添加die cwd; 之类的内容,以查看您的东西在哪里运行。如果它在另一个用户的主目录中,则该用户可能无权读取/root 中的文件。
  • 我在发布之前确实检查了文件权限(我忘了提及)。另外,我只是尝试以 root 身份运行开发服务器,但没有任何区别。我仍然收到相同的错误消息。还有其他想法吗?谢谢
  • __PACKAGE__-&gt;config-&gt;{'View::HTML'} 转储到 STDERR 并确认路径是您预期的路径。
  • 您是否尝试在控制器中提供模板的完整路径?
  • 模板必须在catalyst项目目录的“root/src/homescreen”目录下,而不是在“/root/src/homescreen”目录下。

标签: html perl model-view-controller catalyst template-toolkit


【解决方案1】:

您的配置似乎没有生效。尝试将您的模板放入 root/homescreen/homescreen.tt2 而不是 root/src/homescreen/homescreen.tt2,Catalyst 会找到它。

啊,你的 lib/Myproject.pm 中有错字:

__PACKAGE__->config(
        #Configure the view
        'View::HMTL' => {

改用'View::HTML'(注意你有HMTL - 拼写错误)。

【讨论】:

  • 好地方,谢谢...我期待它是显而易见的,但不是那么明显...我看过这个的次数我应该发现它 - 再次感谢
  • RET 第一定理:调试的问题是解决方案是您最不想想到的。 :-)
猜你喜欢
  • 2016-02-26
  • 1970-01-01
  • 1970-01-01
  • 2018-09-15
  • 2013-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多