[ZPT] Re:

Lynn Walton waltonl@franklin.edu
Thu, 14 Jun 2001 11:53:29 -0500


Daniel,

Thanks for your example.  I don't think this is exactly what I want.  I probably
should have given a small example.

MasterPage
-------------------
<html xmlns:tal="http://xml.zope.org/namespaces/tal"
      xmlns:metal="http://xml.zope.org/namespaces/metal"
      metal:define-macro="master">
<body>

Here I want to make a call to a python script here that will take
as parameters 3 values and return the result as (lot's of table rows that vary of
course depending on the 3 parameters passed in)
html that will get inserted in the page being served.
But the three values are to be "set" in the PageTemplate that is using this master
macro by doing a "use-macro".

Here is other html including
<div metal:define-slot="main">
           If you supply a tag with a 'fill-macro="main"' attribute
           when using this macro, that tag will replace this text.
           </div>
</body>
</html>
-----------------------------------
PageTemplate A
<div tal:define="global firstVal string:foo;
                        global secondVal string: bar;
                        global thirdVal string:biff"></div>

<html xmlns:tal="http://xml.zope.org/namespaces/tal"
      xmlns:metal="http://xml.zope.org/namespaces/metal"
      metal:use-macro="here/masterTemplate/macros/master">

</html>
------------------------------------

What I need to know is what syntax do I use to get the firstVal, secondVal and
thirdVal passed to the
python script I want to call?

Daniel, I think you're example is showing me how I can get a value that was set in
the python script to be accessible from
the page.  I want to know how to get the 3 values set in PageTemplate A into the
python script.  What is the syntax to make the call,
so that I can reference those variable names in the python script?

Simple 1 variable example which I've tried but doesn't work:

If I had:
Master Template
-------------
<html xmlns:tal="http://xml.zope.org/namespaces/tal"
      xmlns:metal="http://xml.zope.org/namespaces/metal"
      metal:define-macro="master">
<body>

 <h1 tal:content="/here/myPythonScript">junk</h1>

<div metal:define-slot="main">
           If you supply a tag with a 'fill-macro="main"' attribute
           when using this macro, that tag will replace this text.
</div>
</body>
</html>

PageTemplate A
------------------------
<span tal:define="global name string:Foo"></span>

<html xmlns:tal="http://xml.zope.org/namespaces/tal"
      xmlns:metal="http://xml.zope.org/namespaces/metal"
      metal:use-macro="here/masterTemplate/macros/master">

expanded stuff in here .... including
 <div metal:fill-slot="main">
    my custom content gets put in here
</div>
<html>


MyPythonScript (just for example) looks like this and has a parameter list of
name="World":
--------------------------------
return "Hello %s." % name

What I want is for the value of the name variable set in PageTemplate A to "Foo"
to be accessible to the MyPythonScript so that it ends up returning "Hello Foo"
instead of "Hello World"


Thanks,
Lynn

daniel.a.fulton@delphiauto.com wrote:

> If this is already answered, I apologize. I only get digest mode.
>
> If I understand your question correctly, you're wanting to replace portions of
> your ZPT / template macro depending on the variables passed in.  I assume an
> http post or a form that actions a python script.
>
> You might want to take a look at below message Evan posted:
>
> http://mail.zope.org/pipermail/zpt/2001-May/001399.html
>
> Here's a sample where I do this, I only replace 'msg' in this particular
> instance but I think you get the jist.  The key is the options dict described in
> Evan's mail.
>
> <span tal:define="msg options/msg | default"
>       tal:content="msg">
>
> The below example is the password_form form and change_password method from the
> CMF re-written in ZPT and Python Scripts.
>
> --------------
> password_form
> --------------
>
> <html metal:use-macro="here/hr_template/macros/master">
>   <div metal:fill-slot="main">
>      <form action="change_password" method="post">
> <h3>Change Password</h3>
> <font color="red">Note:  This changes your password for all internet / intranet
> services</font>
> <br><br>
> <table class="FormLayout">
> <tr>
> <th>
> <span tal:define="global msg options/msg | here/msg | default"
>       tal:content="msg">
>                  </span>
> </th>
> </tr>
> <tr>
> <th>Username
> </th>
> <td tal:define="member user"
>     tal:content="member">Member
> </td>
> </tr>
> <tr>
> <th>New password
> </th>
> <td><input type="password" name="password">
> </td>
> </tr>
> <tr>
> <th>Confirm new password
> </th>
> <td><input type="password" name="confirm">
> </td>
> </tr>
> <tr>
> <th>Domains
> </th>
> <td>
> <input type="text" name="domains:tokens" value=""><br>
> <em> If you do not know what this field is for, leave it blank. </em>
> </td>
> </tr>
> <tr>
> <td><br></td>
> <td><input type="submit" value=" Change ">
> </td>
> </tr>
> </table>
> </form>
> </div>
> </html>
>
> -----------------
> change_password
> -----------------
>
> failMessage = context.portal_registration.testPasswordValidity(password,
> confirm)
>
> if failMessage:
>
>    return failMessage
>
> else:
>
>    context.portal_registration.setPassword(password, domains=None)
>    context.portal_membership.credentialsChanged(password)
>    msg = 'Password Changed'
>
>    return context.password_form(msg=msg)
>
> Regards,
>
> Daniel
>
> >Message: 5
> >Date: Wed, 13 Jun 2001 01:16:17 -0500
> >From: Lynn Walton <waltonl@franklin.edu>
> >To: zpt@zope.org
> >Subject: [ZPT] How to call a python script ...
> >
> >Can someone show me how I could achieve the following?
> >
> >I'm thinking I might wish to have in a master template (macro defined on
> >html tag)  make a call to a python script which will generate a massive
> >amount of
> >html code which needs to be dependent on 3 variables passed into the
> >script.
>
> >
> >MasterTemplate  contains call to script that yields complicated html
> >that varies based on some values passed in.
> >PageTemplates which use the main macro defined in the html tag of
> >MasterTemplate declare/set the values that I need to have passed in the
> >script ... presumably by putting something like  <div tal:define="global
> >someValue string:Fred"></div>  in the top of the page.
> >How does the call to the script look in the MasterTemplate  AND how
> >does the script retrieve the value of someValue set in the page
> >template?
>
> >Thanks,
> >Lynn