Arduino/ESP Creating LONG string for HTML

Thread Starter

ErnieM

Joined Apr 24, 2011
8,377
I noticed then lost what looks like a great technique for my needs. I am writing the text to serve out an HTML web page from my ESP32. Rather than defining text and adding lots of back slashes there was some sort of macro or directive (two really) that took the text literally without adding lots of slashes, and allowing the text to flow over multiple lines.

Anyone out there know what I'm talking about?
 

MrSalts

Joined Apr 2, 2020
2,767
You can use triple quotes to make long strings (with returns).
Code:
String htmlText = """<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>

<p>My first paragraph.</p>

</body>
</html>""";

Or, you can insert variables inside...
Code:
char i="A";

String htmlText = """<!DOCTYPE html>
<html>
<body>
<h1>Heading """ ;

htmlText += i;

htmlText += """</h1>
<p>My first paragraph.</p>
</body>
</html>""";
 

Thread Starter

ErnieM

Joined Apr 24, 2011
8,377
MrSalts: Well....

Code:
String htmlText = """<!DOCTYPE html>
<html>
<body>
<h1>Heading """ ;
Code:
EEPROM_Test:1:1: error: missing terminating " character
 String htmlText = """<!DOCTYPE html>
 ^
EEPROM_Test:4:1: error: missing terminating " character
 <h1>Heading """ ;
 ^
(And lots of other errors due to interpeting text as code)
 

MrSalts

Joined Apr 2, 2020
2,767
Sorry, I was thinking Python.
I believe this works for Arduino (Raw String)

Code:
string str = R"(
<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>

<p>My first paragraph.</p>

</body>
</html>
)";
 

Thread Starter

ErnieM

Joined Apr 24, 2011
8,377
Ahh, thanks so much. It also properly accepts quotes without complaint

That's the hang up of knowing several dozen languages... what is the exact syntax in THIS one.

Fixed one typo, the keyword "String" is capitalized.

Code:
String str = R"(
<!DOCTYPE html>
<html>
<body>

<h1>My "First" Heading</h1>

<p>My first paragraph.</p>

</body>
</html>
)";
Code:
Sketch uses 197848 bytes (15%) of program storage space. Maximum is 1310720 bytes.
Global variables use 13104 bytes (3%) of dynamic memory, leaving 314576 bytes for local variables. Maximum is 327680 bytes.
 
Last edited:
Top