-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Propose TemplatePrinter #56
Conversation
Long ago, with AsyncTCP we discussed that the template character should be changed to a more complex type.
|
Good call, after I get my assignment submitted tonight I can adjust the code to allow variable length delimiters. |
@zekageri My implementation is different to others I've seen (AsyncWeb...), and example 1 shows that unhandled matches get printed out, so unless you are explicitly match a parameter and return Additionally, the delimiter can be changed to any other character, so you could do I'm thinking it may be good to support a different prefix/suffix char so things like However, multi char prefixes and suffixes from what I can see will deliver little to no additional benefit. |
I can't recall what was the specific issue with async template but i will search for it asap. Maybe it was just broken from the start. I remember that if you had two classnames which both contained % char for a parameter the file broke. And there was a long discussion too. Will check! |
Thank you very much for putting that together. Hopefully what I've written below will dispel some worries. To start off with, sure the ESPAsyncWebServer version does have many issues, and is quite... something. This is the reason why I did a custom implementation. However, my implementation does not have the issues posted above. It is a far simpler approach, and it strictly follows the rules outlined in the proposal. None of the strings surrounding Here is an live example showing this: https://wokwi.com/projects/386442945617954817The answer to the issue below covers this, the output is not reprocessed.
There is a major difference between ESPAsyncWebServer and my handler functions: ESPAsyncWebServerString processor(const String& var)
{
if(var == "HELLO_FROM_TEMPLATE")
return F("Hello world!");
return String();
} TemplatePrinterbool templateHandler(Print &output, const char *param){
if(strcmp(param, "HELLO_FROM_TEMPLATE") == 0){
output.print("Hello world!");
return true;
}
return false; // Tell engine that input was not a parameter
}
This is simply not needed with my implementation, only valid characters match a parameter. This is now fixed, can view in the live example posted above:
And finally, with my template engine, the character is easily changed with the constructor: TemplatePrinter printer(Serial, templateHandler, '~'); |
I have updated the code to ignore zero length parameters, not a harmful bug, but does not meet validity requirements.. Thank you @zekageri |
Well, it sounds like a whole lot better than Async's template parameter! |
Hey all, just wanted to say that I like what I see here and when you are ready to merge this in just let me know. I don't have any strong feelings on implementation, so I will leave it up to you @Chris--A |
Yeah, lets do it. I was planning on allowing the size of the buffer (64 bytes) to be modifiable, but is a reasonable size. Not too short that complex parameter names can't be used, and not so long that it uses heaps of memory. Certainly something I can look at if there is a compelling reason in the future. |
@hoeken You should merge this, I am planing to replace ESPAsyncWebServer. |
Will do.
…On Mon, Feb 26, 2024, 21:09 Pham Viet Dzung ***@***.***> wrote:
@hoeken <https://github.com/hoeken> You should merge this, I am planing
to replace ESPAsyncWebServer.
—
Reply to this email directly, view it on GitHub
<#56 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABEHSUCKM74M6UBJB2OYM3YVU555AVCNFSM6AAAAABBPK2TBWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNRVGY3DCNJQGM>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Merged this as part of pull request by @dzungpv Thanks again for your contribution. |
* Added TemplatePrinter from @Chris--A (properly this time)
I have created a simple template processing tool. Primarily this has been built to work with
PsychicStreamResponse
proposed in #45. Incorporation directly intoPsychicStreamResponse
and other response types is possible. Implements some of #55.This is not specific to PsychicHttp, and it works with any
Print
object. You could for example, template data out toFile
,Serial
, etc....The template engine is a
Print
interface and can be printed to directly, however, if you are just templating a few short strings, I'd probably just useresponse.printf()
instead. Its benefit will be seen when templating large inputs such as files.One benefit may be templating a JSON file avoiding the need to use ArduinoJson.
Before closing the underlying
Print
/Stream
that this writes to, it must be flushed as small amounts of data can be buffered. A convenience method to take care of this is shows inexample 3
.The header file is not currently added to
PsychicHttp.h
and users will have to add it manually:Template parameter definition:
%
a-z
,A-Z
,0-9
, and_
null
).%MY_PARAM%
%SOME1%
%MY PARAM%
%SOME1 %
%UNFINISHED
%%
Template processing
A function or lambda is used to receive the parameter replacement.
Parameters:
Print &output
- the underlyingPrint
, print the results of templating to this.const char *param
- a string containing the current parameter.The handler must return a
bool
.true
: the parameter was handled, continue as normal.false
: the input detected as a parameter is not, print literal.See output in example 1 regarding the effects of returning
true
orfalse
.Template input handler
This is not needed unless using the static convenience function
TemplatePrinter::start()
. See example 3.Parameters:
TemplatePrinter &printer
- The template engine, print your template text to this for processing.Example 1 - Simple use with
PsychicStreamResponse
:This example highlights its most basic usage.
The output for example looks like:
Example 2 - Templating a file
Example 3 - Using the
TemplatePrinter::start
method.This static method allows an RAII approach, allowing you to template a stream, etc... without needing a
flush()
. The function call is laid out as:TemplatePrinter::start(host_stream, template_handler, input_handler);
*these examples use the
templateHandler
function defined in example 1.Serve a file like example 2
Template a string like example 1