来自DATA的模板rout.html.ep的语法错误

时间:2013-12-10 22:35:30

标签: html perl cgi mojolicious

我在CGI代码中有HTML,如下所示

app->start
__DATA__
@@ rout.html.ep
<% if ($errormsgs) { %>
<% for my $e (@$errormsgs){ %>
<%=$e%>
<br>
<% } %>
<a href="<%= url_for('/') %>">Go back to fix the form</a><br>
<% } %>
<% else{ %>
<%=$successmsg %>
<a href="<%= url_for('/') %>">Send another?</a><br>
<% } %>

我的CGI代码是:

if (sendmail %mail) {
            $ok .= "Congratulation, your mail was sent!";
            $self->stash ( successmsg => $ok );
        }
        else {
            push @errors, "Error sending: $Mail::Sendmail::error";
        }
    }
    $self->stash ( errormsgs => \@errors );
    $self->render ( 'rout' );

@errors工作得非常好,但successmsg我得到了:

syntax error at template rout.html.ep from DATA section line 8, near ";
 else"
Global symbol "$successmsg" requires explicit package name at template rout.html.ep from DATA section line 9.
syntax error at template rout.html.ep from DATA section line 11, near "} $_M "
syntax error at template rout.html.ep from DATA section line 11, near "} }"

6   

<a href="<%= url_for('/') %>">Go back to fix the form</a><br>

7   

<% } %>

8   

<% else{ %>

9   

<%=$successmsg %>

10  

<a href="<%= url_for('/') %>">Send another?</a><br>

11  

<% } %>

我不知道自己做错了什么,所以有什么想法吗?

2 个答案:

答案 0 :(得分:0)

如果sendmail %msgs未评估为true,则您永远不会为successmsg设置隐藏值,并且Mojolicious不会在模板中声明变量$successmsg。因此,您需要确保始终设置存储值。尝试

    if (sendmail %mail) {
        ...
    }
    else {
        push @errors, "Error sending: $Mail::Sendmail::error";
        $self->stash( successmsg => undef );   # or "", or whatever
    }

<小时/> 我忘记了this answer I gave earlier,即使用stash辅助函数:

<%= stash('successmsg') %>

答案 1 :(得分:0)

之前的方法不起作用所以我最终声明了一个数组并将消息推入其中,现在它可以正常工作。

CGI代码:

my @success;
push @success,  "Congratulation, your mail was sent!";
$self->stash ( successmsg => \@success);

HTML代码:

<% if ($successmsg) { %>
<% for my $s (@$successmsg) { %>
<%=$s %>
<% } %>
<% } %>

希望将来有所帮助