[Zope] render/parse function ? (using <dtml-var x> in a propertysheet ?)sheet ?)

chas panda@skinnyhippo.com
Sun, 09 Apr 2000 13:34:29 +0900


At 04:07 PM 04/08/2000 -0700, you wrote:
>chas wrote:
>> 
>> Sorry to bother you all again, but solving this problem
>> would save a lot of jumping through hoops and I was
>> surprised that nobody else has encountered it.
>> 
>> Consider :
>> - we have a ZClass string property called 'myproperty'.
>> - we set the value of 'myproperty' to "Hello <dtml-var FirstName>"
>
>How is this different than just creating a string property called
>'FirstName' and:
>
>Hello <dtml-var FirstName>
>
>instead of <dtml-var myproperty>?

Just a quick explanation of why this is desired :these properties 
are being used to hold the text for multiple languages. Word order 
is not the same in each case.

eg. Consider displaying an error message that is language-dependent :

    "Sorry, the username Fred Flintstone is not available"

    "Desolee, le nom 'Fred Flintstone' n'est pas disponible"

    (Sorry, my French is poor. I'm actually doing this in 3 different
    Chinese character sets, Korean and Japanese as well as English but
    figured I'd spare you the double-byte charsets. The word order
    is totally different in Asian languages too.)

    So, I want to return one of several messages, depending on 
    language :
    <dtml-var ErrorMsg_en>
    <dtml-var ErrorMsg_jp>
    <dtml-var ErrorMsg_cn>

    But each error message in itself consists of a constant 
    string (eg. "I'm sorry, the username _____ is not available") and
    the variable(s)  (eg. <dtml-var Username>)

    It looks like I'm going to have to use DTML methods but that
    really is going to be ugly to maintain and not very user-friendly.

    I had also thought of manually parsing the string too but figured
    this would slow things down even more.



>>   where FirstName is a variable whose value is set to 'Fred'
>> - we now try to render the value of 'myproperty' inside a DTML
>>   method using the DTML :
>>         <dtml-var myproperty>
>> - as would be expected, the following is displayed -
>>   "Hello <dtml-var FirstName>" since it is just a string.
>>   But really we wanted it to display "Hello Fred"
>>   ie. to evaluate the DTML tags.
>> 
>> Is there any method that can be called to parse
>> the value of a property and evaluate the DTML ?
>
>I see what you want to do here, coincidentally someone else asked
>something similar today.  

Ah, I assume to mean Graham Chiu's earlier message, to which 
you replied : 

[snip]

Uhm... I don't know.  What you want to turn your string into is an
object that can evaluate DTML code, then you can call it from another
tag such as var or call or in.  This cannot be done from DTML, you need
to drop to python to do this.  Just instanciate a 'temporary' DTML
object (proabably a method), feed it your string, and then call it. 
This could probably be done easily from an external method:

<dtml-var "anExternalMethod("<dtml-var blah>")">

[/snip]

OK. Thanks also to John Earl also for pointing me in the
right direction on this one. The following sort of works:

An external method 'externalRenderer' contains the following :

from OFS.DTMLMethod import DTMLMethod
def renderstring(str, REQUEST):
    m = DTMLMethod(str, REQUEST)
    return m()


This works if the variable exists in the REQUEST object :

eg. <dtml-call "REQUEST.set('x', 'Silly')">
    <dtml-var "externalRenderer('Hello <dtml-var x> World', REQUEST)"> 
    will display 'Hello Silly World'

BUT if you have an image object called 'myimg' that is
normally accessible with <dtml-var myimg>, you have to 
first force it into the REQUEST method : 

eg. <dtml-call "REQUEST.set('myimg', myimg)">
    <dtml-var "renderme('Hello <dtml-var myimg> World', REQUEST)">

Which sort of defeats the purpose.




>I'm not sure how useful it would be though. 

To be honest, I've wanted this on many many occasions in the past
and I know alot of other people I've worked with have wanted it too.

Here's a classic example :

You have a ZClass that is being used to store Articles (like Kevin's
KMNews product). The ZClass contains a text property (amongst many other
properties) called "main_story" where your content author writes his 
300-word story.  S/he uploads several images using the Zope image 
object but also wishes to control the positioning of the images. 
S/he wishes to write <dtml-var image1> in that text property 'main_story'.
No can do.

So, instead we've either had to fix a rigid template for where images
are rendered (not good since image dimensions change and it's simply
not aesthetic in many cases). Or usually make the content author 
create a DTML method called 'main_story'. Not as nice as a proper
ZClass.


>Perhaps you could think up some more interesting use cases than what you
>have here and put them on the InterfaceWishList:
>
>http://www.zope.org/Members/michel/Projects/Interfaces/

Took a look - seems to go in circles... keep the UI guy away from LSD.

chas