将 HTML 代码块添加到 Lua 中的字符串 (concat) [英] Adding an HTML block of code to a string (concat) in Lua
问题描述
我知道我可以像这样在 Lua 中连接字符串
String = String .. 'more'
但是如果我想添加 HTML 并且想保持(显然)它的外观怎么办?例如,我如何克服
luac: Perl to Lua:226: ''' 附近未完成的字符串
执行此操作时出现错误
Html_string = Html_string .."<tr>" . gettext("Last Upgrade") . " <td title=\"Upgrade_date\"Upgrade_status</td></tr>"
您可以使用多行字符串标记.
在 Lua 中,这使用 [[ .. ]]
语法
例如:
<前>Html_string = Html_string .. [[<tr><th class="row">]] .. gettext("Last Upgrade") .. [[</th><td title="Upgrade_date">Upgrade_status</td></tr>]]在 [[..]]
内部,您甚至不必转义任何字符.如果您的 html 内容恰好包含 [[ ..]]
本身,您可以将其扩展为 [=[ .. ]=]
以避免冲突.这种扩展可以对任意数量的 =
符号进行,只要它在开始和结束标记中的数量相同即可.
参考 PiL,它甚至使用 HTML 作为多行字符串的示例.
http://www.lua.org/pil/2.4.html
I know that I can concatenate strings in Lua like so
String = String .. 'more'
But what if I want to add HTML and want to keep (obviously) the way it looks? For example, how do I overcome the
luac: Perl to Lua:226: unfinished string near '''
error I get when I do this
Html_string = Html_string .. "
<tr>
<th class=row>" . gettext("Last Upgrade") . "</th>
<td title=\"Upgrade_date\"Upgrade_status</td>
</tr>
"
You can use multi-line string tokens.
In Lua, thats done using the [[ .. ]]
syntax
So, for example:
Html_string = Html_string .. [[ <tr> <th class="row">]] .. gettext("Last Upgrade") .. [[</th> <td title="Upgrade_date">Upgrade_status</td> </tr> ]]
Inside of [[..]]
you don't even have to escape any characters. If your html content happens to contain [[ ..]]
itself, you can expand it to [=[ .. ]=]
to avoid conflicts. This expansion can be done to any number of =
signs, as long as its the same amount in the opening and closing tag.
See the PiL for reference, it even uses HTML as an example for the multiline strings.
http://www.lua.org/pil/2.4.html
这篇关于将 HTML 代码块添加到 Lua 中的字符串 (concat)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!