【问题标题】:syntax error at template rout.html.ep from DATA来自 DATA 的模板 rout.html.ep 的语法错误
【发布时间】:2013-12-28 16:44:53
【问题描述】:

我在 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  

<% } %>

我不知道我做错了什么,有什么想法吗?

【问题讨论】:

    标签: html perl cgi mojolicious


    【解决方案1】:

    如果sendmail %msgs 的计算结果不为真,那么您永远不会为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') %>
    

    【讨论】:

      【解决方案2】:

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

      CGI 代码:

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

      HTML 代码:

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

      希望对未来有所帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-12
        • 2011-04-30
        • 2015-04-17
        相关资源
        最近更新 更多