Applying ..EscapeHTML to argumentless write OR zwrite?

84 views
Skip to first unread message

Micky Hulse

unread,
Nov 6, 2012, 6:31:16 PM11/6/12
to intersystems. public. cache
Howdy,

I'd like to wrap a zwrite or write (argumentless) in a <pre> tag.

Something like:

<pre>
#[ zwrite ]#
</pre>

Unfortunately, this does not work so well... Within the output of
zwrite (or write), there's this:

%request=<OBJECT REFERENCE>[1@%CSP.Request]
%response=<OBJECT REFERENCE>[2@%CSP.Response]
%session=<OBJECT REFERENCE>[3@%CSP.Session]

... and the DOM, at least in Firefox, treats those <OBJECT REFERENCE>
bits as dom objects/tags and my page blows up (due to invalid markup).

I ended up writing a simple RULE that wraps its children and applies
..EscapeHTML() in order to convert the < and > to &lt; and &gt;
respectively.

Here's the RULE:

<https://gist.github.com/4028305#file_1.rg.encode_rule.csr>

... here's the test page:

<https://gist.github.com/4028305#file_2.encode.csp>

... and here's the output of the test page:

<https://gist.github.com/4028305#file_3.output.html>

Note: That's just a rough draft tag... I have plans for a few more features.

Unfortunately, the above RULE does not seem to like script tags
(probably because of the textonly attribute) and/or #[ zwrite ]# or #[
w ]# commands.

Question:

So, can anyone suggest a better way to escape the HTML of the
argumentless zwrite (or write) commands?

Thanks!
Micky

htg

unread,
Nov 6, 2012, 6:40:59 PM11/6/12
to intersystems...@googlegroups.com
use the spool device:
open 2 use 2 zw  close 2
or whatever you need to write
next write the content of your ^SPOOL  wherever you need it.
http://docs.intersystems.com/cache20122/csp/docbook/DocBook.UI.Page.cls?KEY=GIOD_spool

Micky Hulse

unread,
Nov 6, 2012, 7:19:38 PM11/6/12
to intersystems...@googlegroups.com
Hi htg, thanks for the quick reply, I really appreciate it. :)

On Tue, Nov 6, 2012 at 3:40 PM, htg <herman...@googlemail.com> wrote:
> use the spool device:

Oooh, that's interesting! Thanks for tip and example code! I'll
experiment some and post back my findings.

Have a great day!

Cheers,
Micky

--
http://hulse.me

Pittsburgh Trib Steve

unread,
Nov 7, 2012, 6:46:35 AM11/7/12
to intersystems...@googlegroups.com
Micky,

I had the same issue when dealing with RSS code. Once again, our approaches
are completely different.

Either way below would work.

I use Way 1 when I've got to run other DTI/CSP tags, as it's easier to read.

I use Way 2 when all of it is in Caché.

WAY 1
.....................................

<script language="cache" runat="server">
set cdPre = "<pre>", cdPost = "</pre>"
</script>

#(cdPre)# #(zwrite)# #(cdPost)#

#[ kill cdPre, cdPost ]#


WAY 2
.....................................
<script language="cache" runat="server">
set cdPre = "<pre>", cdPost = "</pre>"
w cdPre _ zwrite _ cdPost
kill cdPre, cdPost
</script>


-Steve
--
Caché, Ensemble, DeepSee

Micky Hulse

unread,
Nov 7, 2012, 9:47:55 PM11/7/12
to intersystems...@googlegroups.com
Howdy Steve! It's good to hear from you!

On Wed, Nov 7, 2012 at 3:46 AM, Pittsburgh Trib Steve
<ttms...@gmail.com> wrote:
> I had the same issue when dealing with RSS code. Once again, our approaches
> are completely different.
> Either way below would work.

Awesome, thanks so much for the tips, I really appreciate it.

It's funny that you've been through the same headaches... I actually
just started using argumentless zwrite (and write) for debug
purposes... It's a great tool to check for stray vars floating around
and/or things I forgot to kill. I wish I had learned about its
usefulness years ago! :D

In this case, I think I had tried something very similar to your examples:

<pre>#[ zwrite ]#</pre>

See attached screen shot of Firebug DOM inspection.

In the attached example the markup problem is not noticeable because
there's nothing else on the page (markup-wise). As soon as I put that
code on a, for example, story page (say, at the top of the template)
then that's when all hell breaks loose on my HTML. :D

I just tried your example #1 (I changed #(zwrite)# to #[ zwrite ]# ),
and unfortunately it looks like the same problem as above. :(

<pre>
%CSPsc=1
%request=<OBJECT REFERENCE>[1@%CSP.Request]
%response=<OBJECT REFERENCE>[2@%CSP.Response]
%session=<OBJECT REFERENCE>[3@%CSP.Session]
cdPost="</pre>"
cdPre="<pre>" </pre>

In fact, the drawback here is that cdPost and cdPre are now getting
output inside the wrapping <pre></pre>, which produces this visual
output onto the page:

%CSPsc=1
%request=[1@%CSP.Request]
%response=[2@%CSP.Response]
%session=[3@%CSP.Session]
cdPost=""
cdPre="

"

The goal, at least for me, would be to get an exact output of zwrite
to my HTML page. Hence the reason why I need to escape the angle
brackets.

Your second example had similar issues (I had to tweak it slightly):

<script language="cache" runat="server">
set cdPre = "<pre>", cdPost = "</pre>"
write cdPre
zwrite
write cdPost
kill cdPre, cdPost
</script>

The output was the same as above.

So, it kinda works... but I'm looking for the contents to get escaped
so I can get a true representation of the output and so it does not
break my template's HTML. :(

But yah, I don't think we were too far about in terms of our thinking.

For me, after I realized that wrapping <pre> tags around zwrite (or
write) wasn't going to work (for my needs), my first instinct was to
write a RULE that would wrap "anything" on a template and apply
..EscapeHTML(). Unfortunately, so far, that has not worked so well on
#[ zwrite ]#/#[ write ]#. :(

I do appreciate your help though! I'm glad to know that I'm not the
only one that has take the time to prettify the output of these
commands. :D

So, it's looking like spooling is maybe going to be my best option? I
have not tested spool yet, but I'll be back with my results soon. :)

Have a great night!

Cheers,
Micky

--
Micky Hulse
Web Content Editor
The Register-Guard
3500 Chad Drive
Eugene, OR 97408
Phone: (541) 338-2621
Fax: (541) 683-7631
Web: <http://www.registerguard.com>
firebug.png

Dale du Preez

unread,
Nov 8, 2012, 2:29:07 PM11/8/12
to intersystems...@googlegroups.com
Hi Micky,

It may be simpler to change the output table for your device to use an "HTML" translation table for output so that all characters are automatically written out in an HTML-friendly fashion.

Something like the following ought to do the trick:


<script language="cache" runat="server">
  Write !,"<pre>"
  Try {
    Set currIO = ##class(%SYS.NLS.Device).SetIO("HTML")
    ZWrite
  }
  Catch {
    Write "ERROR: ",$ZERROR
  }
  If $get(currIO) '= "" {
    Do ##class(%SYS.NLS.Device).SetIO(currIO)
  }
  Write "</pre"
</script>

I hope that helps,
Dale

Micky Hulse

unread,
Nov 8, 2012, 6:54:26 PM11/8/12
to intersystems...@googlegroups.com
Hi Dale, thanks so much for the help, I really appreciate it. :)

On Thu, Nov 8, 2012 at 11:29 AM, Dale du Preez
<dale.d...@intersystems.com> wrote:
> It may be simpler to change the output table for your device to use an
> "HTML" translation table for output so that all characters are automatically
> written out in an HTML-friendly fashion.
> Something like the following ought to do the trick:

Whoa! That cool! Thanks for example!

> <script language="cache" runat="server">
> Write !,"<pre>"
> Try {
> Set currIO = ##class(%SYS.NLS.Device).SetIO("HTML")
> ZWrite
> }
> Catch {
> Write "ERROR: ",$ZERROR
> }
> If $get(currIO) '= "" {
> Do ##class(%SYS.NLS.Device).SetIO(currIO)
> }
> Write "</pre"
> </script>

I'm not at my work computer at the moment, but I will give your code a
try (and I also want to test spool as well) and I will post back my
results.

Thanks so much Dale (and everyone) for the help! Much appreciated. :)

Cheers,
Micky

Micky Hulse

unread,
Nov 9, 2012, 6:08:10 PM11/9/12
to intersystems...@googlegroups.com
On Thu, Nov 8, 2012 at 3:54 PM, Micky Hulse <rgm...@gmail.com> wrote:
> I'm not at my work computer at the moment, but I will give your code a
> try (and I also want to test spool as well) and I will post back my
> results.

Just as an update, using SetIO works great! Thanks again Dale.

I have not tested spooling yet, but that looks useful as well (even if
I don't end up using it for this).

Thanks everyone, I really appreciate all of the pro help. :D

Have a nice weekend.

Cheers,
M
Reply all
Reply to author
Forward
0 new messages