ZPT changing javascript (HOW?) - Easy with DTML
Hi! I recently decided to change all my coding from dtml to page templates, but I now have a problem i cant solve. I need to change the javascript on html script blocks. A quick example in DTML of what i want to do and that works perfectly: <HTML> <HEAD> <TITLE>Chat</TITLE> <SCRIPT type="text/javascript"> function connect() { username = <dtml-var "USER_NAME">; password = <dtml-var "USER_NAME">; server = <dtml-var "USER_NAME">; port = <dtml-var "USER_NAME">; subRoomID = <dtml-var "USER_NAME">; groupID = <dtml-var "USER_NAME">; WBChat.Connect2(server,port,subRoomID,username,password,groupID); } </SCRIPT> </HEAD> <BODY> <!-- BODY HERE --> </BODY> </HTML> Someone can help me on how to di this with ZPT?? Thanks in advance, Pedro Costa
There may be ways of doing this directly in zpts (I think Evan Simpson may have posted something about this before), but when I got to this problem, I just put the javascript into a dtml method then inserted it into the page template. tim ----- Original Message ----- From: Pedro Beck Gomes da Costa To: zope@zope.org Sent: Wednesday, October 02, 2002 11:29 AM Subject: [Zope] ZPT changing javascript (HOW?) - Easy with DTML Hi! I recently decided to change all my coding from dtml to page templates, but I now have a problem i cant solve. I need to change the javascript on html script blocks. A quick example in DTML of what i want to do and that works perfectly: <HTML> <HEAD> <TITLE>Chat</TITLE> <SCRIPT type="text/javascript"> function connect() { username = <dtml-var "USER_NAME">; password = <dtml-var "USER_NAME">; server = <dtml-var "USER_NAME">; port = <dtml-var "USER_NAME">; subRoomID = <dtml-var "USER_NAME">; groupID = <dtml-var "USER_NAME">; WBChat.Connect2(server,port,subRoomID,username,password,groupID); } </SCRIPT> </HEAD> <BODY> <!-- BODY HERE --> </BODY> </HTML> Someone can help me on how to di this with ZPT?? Thanks in advance, Pedro Costa
Well, I tried the following, but it didn't work. It parses just fine. Are "script" tags ignored when the PT is rendered? I could understand why. <SCRIPT type="text/javascript" tal:define="USER_NAME user/getUserName"> function connect() { username = <span tal:replace="USER_NAME"/>; password = <span tal:replace="USER_NAME"/>; server = <span tal:replace="USER_NAME"/>; port = <span tal:replace="USER_NAME"/>; subRoomID = <span tal:replace="USER_NAME"/>; groupID = <span tal:replace="USER_NAME"/>; WBChat.Connect2(server,port,subRoomID,username,password,groupID); } </SCRIPT> I'd think you could put the function in another PT and do something like: <script tal:content="structure container/javascriptThinger"> Javascript </script> -Chris On Wed, 2 Oct 2002 11:29:11 +0100, Pedro Beck Gomes da Costa spoke forth:
Hi!
I recently decided to change all my coding from dtml to page templates, but I now have a problem i cant solve. I need to change the javascript on html script blocks. A quick example in DTML of what i want to do and that works perfectly:
<HTML> <HEAD> <TITLE>Chat</TITLE> <SCRIPT type="text/javascript"> function connect() { username = <dtml-var "USER_NAME">; password = <dtml-var "USER_NAME">; server = <dtml-var "USER_NAME">; port = <dtml-var "USER_NAME">; subRoomID = <dtml-var "USER_NAME">; groupID = <dtml-var "USER_NAME">;
WBChat.Connect2(server,port,subRoomID,username,password,groupI D); } </SCRIPT>
</HEAD>
<BODY> <!-- BODY HERE --> </BODY> </HTML>
Someone can help me on how to di this with ZPT??
Thanks in advance,
Pedro Costa
On Thu, Oct 03, 2002 at 12:20:14PM -0500, Christopher N. Deckard wrote:
Well, I tried the following, but it didn't work. It parses just fine. Are "script" tags ignored when the PT is rendered? I could understand why.
I'd be surprised if it parsed fine; if so, I was unaware that <span> didn't require a </span> tag. How about replacing: username = <span tal:replace="USER_NAME"/>; with username = <span tal:replace="USER_NAME">username</span>; and then doing similarly for the other variables? -- Mike Renfro / R&D Engineer, Center for Manufacturing Research, 931 372-3601 / Tennessee Technological University -- renfro@tntech.edu
On Thu, 3 Oct 2002 13:54:24 -0500, Mike Renfro spoke forth:
On Thu, Oct 03, 2002 at 12:20:14PM -0500, Christopher N. Deckard wrote:
Well, I tried the following, but it didn't work. It parses just fine. Are "script" tags ignored when the PT is rendered? I could understand why.
I'd be surprised if it parsed fine; if so, I was unaware that <span> didn't require a </span> tag. How about replacing:
username = <span tal:replace="USER_NAME"/>;
That's what the '/' at the end of the tag is for. Shorthand HTML I guess. It's valid html, but in some cases just less readable.
with
username = <span tal:replace="USER_NAME">username</span>;
and then doing similarly for the other variables?
And what's weird is the <span></span> format seems to have parsing issues when between 'script' tags. The following does not seem to parse properly: <html> <head></head> <body> <script tal:define="USER_NAME user/getUserName" type="text/javascript"> function connect() { username = <span tal:replace="USER_NAME">username</span>; } </script> </body> </html> Error Message: Compilation failed TAL.HTMLTALParser.NestingError: Open tags <html>, <body>, <script> do not match close tag </span>, at line 8, column 54 Getting rid of the 'script' tags fixes it, but does not solve the original problem. -Chris
Speaking here as someone who knows XHTML and XML but not ZPT: This probably won't work. <script> isn't supposed to contain other tags. Go read the FAQ on the XHTML validator for some idea of how javascript and XHTML just don't mix well. Consider moving your script into python or dtml. Also, you shouldn't do this for empty elements: <sometag/> You should always place a space in there: <sometag /> Again, see the XHTML spec, validator, and validator FAQ.
-----Original Message----- From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of Christopher N. Deckard Sent: Thursday, October 03, 2002 12:47 PM To: Mike Renfro Cc: zope@zope.org Subject: Re: [Zope] ZPT changing javascript (HOW?) - Easy with DTML
On Thu, 3 Oct 2002 13:54:24 -0500, Mike Renfro spoke forth:
On Thu, Oct 03, 2002 at 12:20:14PM -0500, Christopher N. Deckard wrote:
Well, I tried the following, but it didn't work. It parses just fine. Are "script" tags ignored when the PT is rendered? I could understand why.
I'd be surprised if it parsed fine; if so, I was unaware that <span> didn't require a </span> tag. How about replacing:
username = <span tal:replace="USER_NAME"/>;
That's what the '/' at the end of the tag is for. Shorthand HTML I guess. It's valid html, but in some cases just less readable.
with
username = <span tal:replace="USER_NAME">username</span>;
and then doing similarly for the other variables?
And what's weird is the <span></span> format seems to have parsing issues when between 'script' tags. The following does not seem to parse properly:
<html> <head></head> <body>
<script tal:define="USER_NAME user/getUserName" type="text/javascript"> function connect() { username = <span tal:replace="USER_NAME">username</span>; } </script>
</body> </html>
Error Message: Compilation failed TAL.HTMLTALParser.NestingError: Open tags <html>, <body>, <script> do not match close tag </span>, at line 8, column 54
Getting rid of the 'script' tags fixes it, but does not solve the original problem.
-Chris
On Thu, Oct 03, 2002 at 02:46:54PM -0500, Christopher N. Deckard wrote:
<script tal:define="USER_NAME user/getUserName" type="text/javascript"> function connect() { username = <span tal:replace="USER_NAME">username</span>; } </script>
</body> </html>
Don't use tags within the script tag, the HTML spec specifically forbids that. I use the following construct for transferring Zope data into javascript variables, this is a real life example: <script type="text/javascript" tal:content="structure string: portalURL = "${here/portal_url}" editionURL = "${here/absolute_url}" " /> <script type="text/javascript"> // Your code goes here </script> The first script tag has its contents dictated by the tal:content attribute. The second script tag has access to whatever you define in the context of the first, including the Zope data we just stuffed into there. Translating this to your specific case is left as an exercise. ;) -- Martijn Pieters | Software Engineer mailto:mj@zope.com | Zope Corporation http://www.zope.com/ | Creators of Zope http://www.zope.org/ ---------------------------------------------
Thanks, and thanks to all that answer to my original question. It worked! I really have to practice more the zpt stuff :) were to much used to DTML Pedro Costa ----- Original Message ----- From: "Martijn Pieters" <mj@zope.com> To: "Christopher N. Deckard" <cnd@ecn.purdue.edu> Cc: "Mike Renfro" <renfro@tntech.edu>; <zope@zope.org> Sent: Thursday, October 03, 2002 10:19 PM Subject: Re: [Zope] ZPT changing javascript (HOW?) - Easy with DTML
On Thu, Oct 03, 2002 at 02:46:54PM -0500, Christopher N. Deckard wrote:
<script tal:define="USER_NAME user/getUserName" type="text/javascript"> function connect() { username = <span tal:replace="USER_NAME">username</span>; } </script>
</body> </html>
Don't use tags within the script tag, the HTML spec specifically forbids that. I use the following construct for transferring Zope data into javascript variables, this is a real life example:
<script type="text/javascript" tal:content="structure string: portalURL = "${here/portal_url}" editionURL = "${here/absolute_url}" " /> <script type="text/javascript"> // Your code goes here </script>
The first script tag has its contents dictated by the tal:content attribute. The second script tag has access to whatever you define in the context of the first, including the Zope data we just stuffed into there.
Translating this to your specific case is left as an exercise. ;)
-- Martijn Pieters | Software Engineer mailto:mj@zope.com | Zope Corporation http://www.zope.com/ | Creators of Zope http://www.zope.org/ ---------------------------------------------
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
----- Original Message ----- From: "Martijn Pieters" <mj@zope.com> To: "Christopher N. Deckard" <cnd@ecn.purdue.edu> Cc: "Mike Renfro" <renfro@tntech.edu>; <zope@zope.org> Sent: Thursday, October 03, 2002 10:19 PM Subject: Re: [Zope] ZPT changing javascript (HOW?) - Easy with DTML
On Thu, Oct 03, 2002 at 02:46:54PM -0500, Christopher N. Deckard wrote:
<script tal:define="USER_NAME user/getUserName" type="text/javascript"> function connect() { username = <span tal:replace="USER_NAME">username</span>; } </script>
</body> </html>
Don't use tags within the script tag, the HTML spec specifically forbids that. I use the following construct for transferring Zope data into javascript variables, this is a real life example:
<script type="text/javascript" tal:content="structure string: portalURL = "${here/portal_url}" editionURL = "${here/absolute_url}" " /> <script type="text/javascript"> // Your code goes here </script>
The first script tag has its contents dictated by the tal:content attribute. The second script tag has access to whatever you define in the context of the first, including the Zope data we just stuffed into there.
Translating this to your specific case is left as an exercise. ;)
-- Martijn Pieters | Software Engineer mailto:mj@zope.com | Zope Corporation http://www.zope.com/ | Creators of Zope http://www.zope.org/ ---------------------------------------------
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Mike Renfro wrote:
I'd be surprised if it parsed fine; if so, I was unaware that <span> didn't require a </span> tag. How about replacing:
<script> and <style> elements contain text that is not parsed for tags. The only way to make this text dynamic is with tal:content, in either of the two following ways: <script tal:content="structure string: var x = $x; x = x + 1; ... "></script> <script tal:content="structure path/to/dtml_or_other_method"></script> In other words, big honking string expression or another object that generates the text. Note that if all you're doing is setting up script variables, you can split the script like so: <script tal:content="structure string: var x = $x; var foo = ${template/id}; "></script> <script> x = x + 1; ...etc... </script> Cheers, Evan @ 4-am
Ah yes, that's a nice way to do it. A not-so-nice way is to do: <div tal:replace="string:<script>" /> ... content of script tag including other calls to ZPT <div tal:replace="string:</script>" /> Florent Evan Simpson <evan@4-am.com> wrote:
Mike Renfro wrote:
I'd be surprised if it parsed fine; if so, I was unaware that <span> didn't require a </span> tag. How about replacing:
<script> and <style> elements contain text that is not parsed for tags. The only way to make this text dynamic is with tal:content, in either of the two following ways:
<script tal:content="structure string: var x = $x; x = x + 1; ... "></script>
<script tal:content="structure path/to/dtml_or_other_method"></script>
In other words, big honking string expression or another object that generates the text. Note that if all you're doing is setting up script variables, you can split the script like so:
<script tal:content="structure string: var x = $x; var foo = ${template/id}; "></script> <script> x = x + 1; ...etc... </script> -- Florent Guillaume, Nuxeo (Paris, France) +33 1 40 33 79 87 http://nuxeo.com mailto:fg@nuxeo.com
participants (9)
-
Charlie Reiman -
Christopher N. Deckard -
Christopher N. Deckard -
Evan Simpson -
Florent Guillaume -
Martijn Pieters -
Mike Renfro -
Pedro Beck Gomes da Costa -
Tim Hicks