开发者

Adding an HTML block of code to a string (concat) in Lua

开发者 https://www.devze.com 2023-01-07 03:01 出处:网络
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?

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

0

精彩评论

暂无评论...
验证码 换一张
取 消