Question

I do have HTML inside CGI code as following

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>
<% } %>

and my CGI code is:

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 work perfectly fine, but successmsg I get:

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  

<% } %>

and I don't know what I did wrong, so any ideas?

Was it helpful?

Solution 2

The previous method didn't work so I ended up declaring an array and pushed the message into it and now it works.

CGI Code:

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

HTML Code:

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

Hope that helps in the future

OTHER TIPS

If sendmail %msgs does not evaluate to true, then you never set the stash value for successmsg, and Mojolicious doesn't declare the variable $successmsg in the template. So you need to be sure that the stash value is always set. Try

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


I forgot about this answer I gave earlier, which is to use the stash helper function:

<%= stash('successmsg') %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top