• DHTML and Variables
    4 replies, posted
Hi guys. Is there any way I can place (call) a custom variable into a dhtml code? There aren't many tutorials over the net so I hope my question isn't really stupid D:
I mean I have code like [CODE]my_variable = 228 SCBHTML:SetHTML( [[<iframe bla src="blablabla"..my_variable.."blableblya"></iframe>]] )[/CODE] And this devil refuses to work :c
You're using [[ to start the string so you need to close the string, concat your variable, and then concat the last part of that string
[QUOTE=MediCat;51453696]I mean I have code like [CODE]my_variable = 228 SCBHTML:SetHTML( [[<iframe bla src="blablabla"..my_variable.."blableblya"></iframe>]] )[/CODE] And this devil refuses to work :c[/QUOTE] I would use string formatting ([img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/string/format]string.format[/url]) Something like: [lua] local my_variable = 228 local html_content = [[ <p>The value of my_variable is: %d</p> ]] SCBHTML:SetHTML(string.format(html_content, my_variable)) [/lua]
[QUOTE=Banana Lord.;51453809]You're using [[ to start the string so you need to close the string, concat your variable, and then concat the last part of that string[/QUOTE] Unfortunately this don't work, and console has a looooooong THML error :( [editline]1st December 2016[/editline] [QUOTE=zerf;51453812]I would use string formatting ([img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/string/format]string.format[/url]) Something like: [lua] local my_variable = 228 local html_content = [[ <p>The value of my_variable is: %d</p> ]] SCBHTML:SetHTML(string.format(html_content, my_variable)) [/lua][/QUOTE] Well, this should work brilliant, thanks for the idea. But what if I already have "%" chars in the HTML code? UPD: Yay I replaced all %'s with %% and it worked brilliantly! Thank you so much :3
Sorry, you need to Log In to post a reply to this thread.