Hi- I have a dtml page which displays the results of a sql query: <dtml-in expr="my_query(category1=category1, category2=category2)"> <TR><TD> <dtml-var NAME> </TR></TD> </dtml-in> The values that are passed to the query are obtained from the request object. If the value for category2=None then I want to leave category2 out of the sql query as below: <dtml-in expr="my_query(category1=category1)"> <TR><TD> <dtml-var NAME> </TR></TD> </dtml-in> The sql query has been designed to handle this using the optional tag. However, it falls over if I break the in tags up with an if tag, e.g.: <dtml-if "category2=None"> <dtml-in expr="my_query(category1=category1)"> <dtml-else> <dtml-in expr="my_query(category1=category1, category2=category2)"> </dtml-if> <TR><TD> <dtml-var NAME> </TR></TD> </dtml-in> I have also tried passing a string to the in tag, e.g.: <dtml-if "category2=None"> <dtml-var dummystr="my_query(category1=category1)"> <dtml-else> <dtml-var dummystr="my_query(category1=category1, category2=category2)"> </dtml-if> <dtml-in expr=<dtml-var dummystr>> <TR><TD> <dtml-var NAME> </TR></TD> </dtml-in> but the in tag cannot except a string? I have also tried handling this in python, as it is logic, however this too has been unsuccessful and would quite allow me to format the results as desired. I just want to control how the sql query is executed? Depending on the value of variables in the request object. Any help, much appreciated, Rob
I have used this type of approach before: <dtml-if pns> <dtml-call "REQUEST.set('searchfield', 'nameserver1')"> <dtml-call "REQUEST.set('searchcriteria', pns)"> </dtml-if> <dtml-if org> <dtml-call "REQUEST.set('searchfield', 'org')"> <dtml-call "REQUEST.set('searchcriteria', org)"> </dtml-if> <dtml-in "Catalog({searchfield : searchcriteria})"> You may be able to adapt this approach... HTH Jonathan ----- Original Message ----- From: "RI Dunfey" <s0231134@sms.ed.ac.uk> To: <zope@zope.org> Sent: February 16, 2004 11:32 AM Subject: [Zope] A conditional dtml-in tag?
Hi-
I have a dtml page which displays the results of a sql query:
<dtml-in expr="my_query(category1=category1, category2=category2)"> <TR><TD> <dtml-var NAME> </TR></TD> </dtml-in>
The values that are passed to the query are obtained from the request
object.
If the value for category2=None then I want to leave category2 out of the sql query as below:
<dtml-in expr="my_query(category1=category1)"> <TR><TD> <dtml-var NAME> </TR></TD> </dtml-in>
The sql query has been designed to handle this using the optional tag.
However, it falls over if I break the in tags up with an if tag, e.g.:
<dtml-if "category2=None"> <dtml-in expr="my_query(category1=category1)"> <dtml-else> <dtml-in expr="my_query(category1=category1, category2=category2)"> </dtml-if> <TR><TD> <dtml-var NAME> </TR></TD> </dtml-in>
I have also tried passing a string to the in tag, e.g.:
<dtml-if "category2=None"> <dtml-var dummystr="my_query(category1=category1)"> <dtml-else> <dtml-var dummystr="my_query(category1=category1, category2=category2)"> </dtml-if> <dtml-in expr=<dtml-var dummystr>> <TR><TD> <dtml-var NAME> </TR></TD> </dtml-in>
but the in tag cannot except a string? I have also tried handling this in python, as it is logic, however this too has been unsuccessful and would quite allow me to format the results as desired. I just want to control how the sql query is executed? Depending on the value of variables in the request object.
Any help, much appreciated,
Rob
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
On Monday 16 February 2004 10:32 am, RI Dunfey wrote:
The values that are passed to the query are obtained from the request object. If the value for category2=None then I want to leave category2 out of the sql query as below:
One solution is to call the query from Python and have a Python script like: if REQUEST.category2: return context.my_query(category1=category1, category2=category2) else: return context.my_query(category1=category1) Which is clearer to read. However, you can use the "and-or" idiom directly in DTML, if you want to avoid the extra script (this is roughly equivalent to converting the if-else above into an expression): <dtml-in expr="category2 and my_query(category1=category1, category2=category2) or my_query(category1=category1)"> <dtml-var NAME> <!-- put your loop contents here --> </dtml-in> HTH, Terry -- Terry Hancock ( hancock at anansispaceworks.com ) Anansi Spaceworks http://www.anansispaceworks.com
RI Dunfey wrote at 2004-2-16 16:32 +0000:
I have a dtml page which displays the results of a sql query:
<dtml-in expr="my_query(category1=category1, category2=category2)"> <TR><TD> <dtml-var NAME> </TR></TD> </dtml-in>
The values that are passed to the query are obtained from the request object. If the value for category2=None then I want to leave category2 out of the sql query as below:
I suggest you do the logic in a "Script (Python)". It is far easier there. It will look like: request = container.REQUEST category1 = request.get('category1') category2 = request.get('category2') query = { 'category1' : category1 } if category2: # or whatever logic you want query['category2'] = category2 return context.my_query(**query) # here you call your Z SQL Method -- Dieter
participants (4)
-
Dieter Maurer -
RI Dunfey -
Small Business Services -
Terry Hancock