Rendering with indent?
marclennox opened this issue · 3 comments
I'm trying to use lua-resty-template as a simple way to serve up dynamic YAML config files with nginx.
I've got it mostly working but in some cases I need to render other files inside of a template with indentation, but I'd rather not indent the source file because I could be including it at different indent levels.
Any thoughts on how to handle this?
How about making your own helper function? Something like this:
indent.lua
local function include(file, indend_level, indend_char, eol)
indend_char = indend_char or " "
local readline = io.lines
local i, lines = 1, {}
for line in readline(file) do
lines[i] = indend_char:rep(indend_level or 4) .. line
i = i + 1
end
lines[i] = ""
return table.concat(lines, eol or "\n")
end
local template = require "resty.template"
template.render("indent.html", { include = include })
indent.html
<html>
<body>
{{include"indent.txt"}}
</body>
</html>
If you don't want it html encoded, use: {*include"indent.html"*}
instead.
indent.txt
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed aliquam ultricies lacus quis interdum. Donec vel metus
lorem. Mauris lobortis ac risus a vulputate. Praesent eu
quam eu felis bibendum ultricies. Nam maximus metus quis
elit bibendum, sed porta augue finibus. Duis nec aliquet
ante, vitae suscipit elit. Vestibulum ultrices justo lacus,
rhoncus dapibus ex lobortis sed. Maecenas ac vestibulum sem,
ac congue augue. Nam scelerisque nibh sapien, sit amet
dignissim justo pharetra et. Aenean purus tortor, rhoncus
ut congue id, pretium ac libero. Vivamus quis justo quis mi
feugiat euismod sit amet ac sapien. Curabitur commodo
aliquam nisl non interdum.
Nulla a massa in velit viverra ultrices. Vestibulum ligula
ligula, aliquet vitae tincidunt lacinia, fermentum vitae libero.
Mauris ultrices ac erat eu auctor. Nunc rhoncus purus sed metus
lacinia convallis. Integer facilisis venenatis elementum. Duis
non orci orci. Maecenas hendrerit sollicitudin justo in dapibus.
Donec ac euismod lectus. Sed feugiat sem nisl, at suscipit urna
eleifend nec. Integer suscipit neque eu mauris sagittis, in
posuere ex lacinia.
Output
<html>
<body>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed aliquam ultricies lacus quis interdum. Donec vel metus
lorem. Mauris lobortis ac risus a vulputate. Praesent eu
quam eu felis bibendum ultricies. Nam maximus metus quis
elit bibendum, sed porta augue finibus. Duis nec aliquet
ante, vitae suscipit elit. Vestibulum ultrices justo lacus,
rhoncus dapibus ex lobortis sed. Maecenas ac vestibulum sem,
ac congue augue. Nam scelerisque nibh sapien, sit amet
dignissim justo pharetra et. Aenean purus tortor, rhoncus
ut congue id, pretium ac libero. Vivamus quis justo quis mi
feugiat euismod sit amet ac sapien. Curabitur commodo
aliquam nisl non interdum.
Nulla a massa in velit viverra ultrices. Vestibulum ligula
ligula, aliquet vitae tincidunt lacinia, fermentum vitae libero.
Mauris ultrices ac erat eu auctor. Nunc rhoncus purus sed metus
lacinia convallis. Integer facilisis venenatis elementum. Duis
non orci orci. Maecenas hendrerit sollicitudin justo in dapibus.
Donec ac euismod lectus. Sed feugiat sem nisl, at suscipit urna
eleifend nec. Integer suscipit neque eu mauris sagittis, in
posuere ex lacinia.
</body>
</html>
If you want to cache your includes, you can do that as well, e.g. in lua module level or otherwise, but I let that for you.
If you mean including other templates and indenting them, you can do that as well. Something like this:
{* indent_string(template.compile"subtemplate.html"(context)) *}
But there are other approaches as well (needless to say, you need to implement that indent_string first). Does this help you at all?
Thanks @bungle that's great. I had started down this exact path and have it working perfectly now. Instead of iterating line-by-line I'm loading the whole template and then replacing "\n" with "\n" + the indent
Sounds great! I'm closing this now. Please reopen, if you don't feel satisfied with this so that we can figure out a better way to do it.