On Thu, May 4, 2017 at 3:31 AM, Alex Povolotsky <[email protected]> wrote:

> I want to put part of the template in database; maybe someone has already
> done it and can share a recipie?


You probably just want to render inline
<http://mojolicious.org/perldoc/Mojolicious/Guides/Rendering#Rendering-inline-templates>
.

Fetch your template from your database and then render inline, something
like:

sub action {
  my $self = shift;
  my $template = $self->pg->db->select('table', 'template', {id =>
123})->hash->{template};
  $self->stash(id => 123)->render(inline => $template);
}


Where in your database 'table' you have a record id=123, template="id: <%=
$id %>"

Here's an inline helper that may help you to take it a step further and
embed that database template into an existing template.  Not sure that I
did this well, but an initial test seems to work.

helper inline => sub {
  my $c = shift;
  my $tmpl = shift;
  my ($output, $format) = $c->app->renderer->render($c, {inline => $tmpl,
handler => "ep", format => "html", @_});
  return $output;
};

get '/' => sub {
  my $c = shift;
  $c->stash(id => 123, tmpl => $c->pg->db->select('table', 'template', {id
=> 123})->render(inline => '<%= 1+1 %> <%= inline $tmpl %>');
};

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to