Cookies, as we all know, need to be set in the HTTP headers, before a page sends any output.
I'm now in the position where I need to be able to set a cookie when a visitor saves a preference. The first thing that springs to mind would be to have a snippet in the page, with a simple form, which calls the same page when it is submitted (and sets the cookie accordingly, if so). Of course, even if I make the snippet the first thing on the page, output will already have started to be sent from the page template up until that point is reached.
I know that the way in which MODx parses pages can perform certain magic to allow you do some things with hindsight (such as "retrospectively" adding a script file to the 'head' section using
regClientStartupScript, while the page is assembled, and it all sorts itself out in the end.
Is there similar magic available to do the same for setting cookies, or is my only option to have the form submit to a special page (with no template or output) which will set the cookie headers, and then do some 'referer' divination to send the visitor back where they came from, along with their freshly-baked cookies?