Create dynamic variables

Hello

I run a webpage in my lua code that way :

function main ()
		
	-- Lancement du serveur web
	web.site[""] = web.compile(webpage.Page_Supervison_Enless)
		
	web.start("*", 8080)
...

And the HTML code is into a lua variable.

I know how to display a lua variable in the webpage ( “<%=Var>” in html code) but now I need to generate dynamic variables.

The situation :

I receive frames from emitters with their ID (contained in Tab_Emetteur[l]) and when I recieved them (contained in Tab_Emetteur[j]).
Each time I receive a frame I generate a line in a HTML table :

li_Em = li_Em .. "<tr><td align=\"center\">" .. Tab_Emetteur[l] .. "</td><td align=\"center\">" .. Tab_Emetteur[j] .. "</td></tr>"

So I have a something like that

The left column with emitter IDs stay the same but I want to display when I recieved the last frame from each emitter. (Don’t mind the <%=Tab_Heure[<%=n>]>, I was a test and it didn’t work obviously…)

So I need to generate a variable when a new table line is created. And then when a frame is received, the time is updated in front of the right ID.
The number of emitter is not defined regarding where I run the program, that’s why creating a table with already X lines with X declared variables is not possible.

I tried something like that but I don’t know if it lead somewhere :

emetteur = {}
-- I receive a frame, I take its ID and time
ID = Tab_Emetteur[l]
time = Tab_Emetteur[j]

-- I add in my table a key = value to associate ID and time
emetteur.ID = time

-- li_Em = li_Em .. "<tr><td align=\"center\">" .. Tab_Emetteur[l] .. "</td><td align=\"center\">" .. emetteur.ID .. "</td></tr>"

In that way, I have variables like emetteur.11606534, emetteur.11324740 … Then I need to access those variable to change their associated values. But the use of emetteur.ID (or <%=emetteur.ID>) didn’t work.

Let me know if you need more details, it’s kinda hard to explain.

Thanks,
Dylan

Hi Dylan,

Thx for the effort you put in the details and presentation of the post. But as you mentioned it is hard to grasp the exact issue you are having. So I’ll try to provide help, let me know it that works for you.

When using Lua templates, you can actually use two syntax’s. The one you described “<%=Var%>” (expression syntax) that allows you to print the content of a variable. And another syntax that allows you to actually write Lua code. “<% Some Lua code %>” (generic syntax). In the Lua code you have to use the echo() function in order to print content to the page. The good thing is that you can actually use loops to display tables of variable length, for instance.

FYI the expression syntax ("<%=Var%>") is equivalent to the generic syntax “<% echo(Var) %>”

Here is an example on how to use the generic syntax to print a variable length table for a for loop.

<table class="table table-hover">
            <tr>
                <th>Date</th>
                <th>S/N</th>
                <th>Model</th>
                <th>RM Type</th>
                <th>IP</th>
                <th>File downloaded</th>
            </tr>
            <% for _, entry in ipairs(connection_logs) do %>
                <tr>
                    <td><span class="glyphicon glyphicon-calendar" aria-hidden="true"></span><% =os.date("%c", entry.time) %></td>
                    <td><% =entry.sn %></td>
                    <td><% =entry.model %></td>
                    <td><% =entry.rmtype %></td>
                    <td><% =entry.ip %></td>
                    <td><% =entry.file %></td>
                </tr>
            <% end %>
            </table>

In my example above connection_logs is a list of tables, each one containing named fields.
Something like:

connection_logs =
{
   {sn = "65456-78", model="RV50", rmtype="MC7354", ip="192.168.13.31", file="Package.bin"},
   {sn = "AHDBDFG-45", model="GX450", rmtype="MC7304", ip="192.168.13.31", file="Package.bin"},
}

Please note that each <% %> does not need to be complete Lua syntactic element. For instance the first block contains the “for” loop definition, but the “end” of the loop is actually in another <% %> block.

Let me know if that helps.

Hi,

I didn’t know we can write code, it’s useful thanks.
But I noticed we can’t do that in a variable and then, add it to the html code. The lua code has to be written directly in the html code otherwise the code is just printed.

In my case, I solved the problem in a different way, ugly way maybe but it works !
I parse the html line to get the information I want to change, then replace the all line with a new one.

-- Search the field to replace in li_Em
local recup_id = string.find(li_Em,IDem,1)

li_Em2 = li_Em

-- Keep all data previous the field to change, replace date and time par a new one, add the rest at the end. 
li_Em = string.sub(li_Em,1,recup_id + 64) .. Dateem .. " " .. Timeem .. string.sub(li_Em2,recup_id + 84)

The string has always the same length so it’s ok to process it like that.

Well the goal is to have something working in the end, so if that works for you I guess it could be OK.
Please note thought that you do not have to “write the code in the html doc” You can simply write a Lua function in your Lua file (make sure it is in the global scope) and simply call that function from within your HTML page.

Yes I proceed that way most of the time.

When I launch the program, the functions are called.
But with a function like that it’s annoying :

function rebootModem()
print("Reboot function called")
system.reboot()
end

I want to trigger that function on a button clic but instead the function is called instantly.
I tried something like that :

<ul class="topnav">
...
<li><a href="#Reboot" OnClick=<%rebootModem()%>>Reboot</a></li>
...
</ul>

The button didn’t work and it was an infinite reboot loop !

Indeed, the way to do it, is to define a specific URL that when called will call the reboot function (a custom page handler in the web server config). In your htlml you cable your button as link to that URL. When you click the link it will call the new page that you defined and that in fact just call the reboot function.