Announcement

Collapse
No announcement yet.

Executing dynamic code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    #16
    Executing dynamic code



    Got it. I'll try the tokenize method - that makes perfect sense, just not as
    easy as the first! But definitely doable.

    Thanks for all your help, Markus!

    Susan Petracco
    [email protected]
    NetBlazon
    E-Business for Every Business

    > -----Original Message-----
    > From: [email protected]
    > [mailto:[email protected]] On Behalf Of MvMarkus
    > Sent: Wednesday, January 19, 2005 11:49 AM
    > To: 'Susan Petracco'; [email protected]
    > Subject: RE: [meu] Executing dynamic code
    > Importance: High
    >
    > Actually Susan, this was my bad (deep red blushing here)- I
    > just realized
    > that in a similar function I actually parse the script and
    > only replaces the
    > stuff that is inside the $; using miva_variable_value() like
    > this won't work
    > directly, because it considers the literal strings before and
    > after as part
    > of a variable name that it tries to replace - that's why you
    > don't get any
    > result at all.
    >
    > Try tokenize() or a simple parser (loop through the template
    > by tokenizing
    > the string until the $, and miva_variable_value() every
    > second token), or if
    > you have more specific information about your particular
    > requirement shoot
    > it over to get it going....
    >
    > Markus
    >
    > (it really makes sense to test code or code fragments before
    > posting them in
    > public...)
    >
    >
    >
    >
    >
    >
    > -----Original Message-----
    > From: [email protected]
    > [mailto:[email protected]] On Behalf
    > Of Susan Petracco
    > Sent: Wednesday, January 19, 2005 10:27 AM
    > To: [email protected]
    > Subject: RE: [meu] Executing dynamic code
    >
    > Markus,
    >
    > Thanks for the detailed explanation...still not having any
    > luck though.
    > Miva_variable_value isn't returning anything (or is returning an empty
    > string maybe). I didn't realize it had behavior as you described. But
    > regardless I'm still having trouble. Here's my test code:
    >
    > <MvFUNCTION NAME="GetMessageFormat" PARAMETERS="type"
    > STANDARDOUTPUTLEVEL="text,html,compresswhitespace"
    > ERROROUTPUTLEVEL="syntax,
    > expression">
    > <MvFIND NAME="MYDB" VALUE="{ l.type }">
    > <MvASSIGN NAME="l.domain" VALUE="www.miva.com">
    > <MvASSIGN NAME="l.xmlformat" VALUE="{ MYDB.d.message }">
    > <MvASSIGN NAME="l.xml_message" VALUE="{
    > miva_variable_value(l.xmlformat)
    > }">
    > l.xmlformat: <MvEVAL EXPR="{ l.xmlformat }"><hr>
    > l.xml_message: <MvEVAL EXPR="{ l.xml_message }"><hr>
    > <MvFUNCRETURN VALUE="{ l.xmlformat }"> </MvFUNCTION>
    >
    > l.xmlformat contains:
    > '<domainname>'$l.domain$'</domainname>'
    >
    > l.xml_message is blank.
    >
    > Am I just butting my head against a brick wall - missing the obvious?
    >
    > --Susan
    >
    > Susan Petracco
    > [email protected]
    > NetBlazon
    > E-Business for Every Business
    >
    > > -----Original Message-----
    > > From: MvMarkus [mailto:[email protected]]
    > > Sent: Tuesday, January 18, 2005 3:55 PM
    > > To: 'Susan Petracco'; 'Patrick Locke'; [email protected]
    > > Cc: 'Ivo Truxa'
    > > Subject: RE: [meu] Executing dynamic code
    > > Importance: High
    > >
    > > Hi Susan,
    > >
    > > Here a (hopefully) better explanation to your question:
    > >
    > > You save the literal string in your database, textfile or your
    > > mivascript as a variable (like a template), and "execute" it with
    > > miva_variable_value()
    > > before you send it to MvCALL. Miva_variable_value() will keep the
    > > literals, but replace the variables that are nested within,
    > outside of
    > > the single quotes, merged into the string by using the $-operator).
    > > Below I give you a
    > > pseudo-code example:
    > >
    > >
    > > 1.) You assign the xml-header....
    > >
    > > ASSIGN l.xml_message = {'<?xml version="1.0"?>'}
    > >
    > >
    > > 2.) Create you xml template, for example
    > >
    > > ASSIGN NAME =l.xml_template VALUE={'
    > > <ECommerce action="Request" version="1.1"> <Shipment>
    > > <Requestor>
    > > <ID>
    > > '$ d.id $'
    > > </ID>
    > > </Requestor>
    > >
    > > <TransactionTrace>'$
    > > d.transtr $'
    > > </TransactionTrace>
    > >
    > > </Shipment>
    > > </ECommerce>'}
    > >
    > >
    > > These two strings (xml_template and the header) can be stored in a
    > > database, a text file or a mivascript.
    > >
    > > Then, to fill the xml-file with the fresh data from the database:
    > >
    > >
    > > OPEN DATABASE
    > > MvFILTER (or other ways to select the records that you want to
    > > publish)
    > >
    > >
    > > WHILE NOT d.EOF
    > > ASSIGN l.xml_message VALUE= {l.xml_message$'
    > > '$ miva_variable_value( l.xml_template ) } SKIP END WHILE
    > >
    > > // this loops through the table, grabs the values and
    > replaces them.
    > > The literals (everything within the single quotes) remain unchanged.
    > > Alternatively, you could also use tokenize(), but it's a bit more
    > > complicated to write.
    > >
    > > You should also add some conditions to handle cases where a
    > value is
    > > empty, so that your xml file is still valid xml.
    > >
    > > To handle that case, you can make your template more fine-grained:
    > >
    > > ASSIGN NAME =l.request_block_template VALUE={'
    > > <Requestor>
    > > <ID>
    > > '$ d.id $'
    > > </ID>
    > > </Requestor>
    > > '}
    > >
    > > ASSIGN NAME =l.trans_block_template VALUE={'
    > > <Requestor>
    > > <ID>
    > > '$ d.transtr $'
    > > </ID>
    > > </Requestor>
    > > '}
    > >
    > >
    > > ASSIGN NAME =l.xml_template VALUE={'
    > > <ECommerce action="Request" version="1.1"> <Shipment>
    > > '$ l.request_block $'
    > > '$ l.trans_block $'
    > > </Shipment>
    > > </ECommerce>'}
    > >
    > >
    > >
    > > And in the loop you use all three templates:
    > >
    > > WHILE NOT d.EOF
    > > IF d.id
    > > ASSIGN l.request_block = miva_variable_value(
    > > l.request_block_template )
    > > ELSE
    > > ASSIGN l.request_block = '' //assign nothing to
    > it to clear
    > the
    > > variable
    > > ENDIF
    > >
    > > IF d.transtr
    > > ASSIGN l.trans_block = miva_variable_value(
    > l.trans_block_template )
    > > ELSE
    > > ASSIGN l.trans_block = ''
    > > ENDIF
    > >
    > >
    > > ASSIGN l.xml_message VALUE= {l.xml_message$'
    > > '$ miva_variable_value( l.xml_template ) }
    > >
    > >
    > > SKIP
    > > END WHILE
    > >
    > >
    > >
    > > In terms of performance, this doesn't take too long to
    > generate. Here
    > > is a dummy XML file that is generated from a database with 160
    > > records, each having about 10 attributes and 3 additional
    > sublevels.
    > > So altogether there are about 5500 levels:
    > >
    > > <A HREF ="http://www.emeraldobjects.com/distribution.mvc?mode=SHOW">http://www.emeraldobjects.com/distribution.mvc?mode=SHOW</A>
    > >
    > > (the file is rather large, about 290kbyte, so this takes of
    > course a
    > > bit of time to generate and download)
    > >
    > >
    > >
    > >
    > > Miva_variable_value() is one of the most powerful tools Mivascript
    > > offers.
    > > It replaces the old macro, however it doesn't work for
    > operators like
    > > * + - / or dynamic function calls. In other words, you can have a
    > > simple assignment, but not anything like 'string.... '$g.price *
    > > g.markup $'rest of your string' or 'string.... '$
    > > susans_function(price,markup) $'rest of your string' .
    > >
    > > To handle these kind of dynamic functioncalls you'd need to write a
    > > parser, use Ivo's MvFILTER-trick or his MvVladdy. Writing a
    > parser is
    > > not very hard, but quite time consuming, so that might not be an
    > > option.
    > >
    > > I hope that helps.
    > >
    > > Markus
    > >
    > >
    > >
    > >
    > >
    > >
    > >
    > > -----Original Message-----
    > > From: [email protected]
    > > [mailto:[email protected]] On Behalf Of Susan Petracco
    > > Sent: Tuesday, January 18, 2005 11:54 AM
    > > To: 'Patrick Locke'; [email protected]
    > > Cc: 'Ivo Truxa'
    > > Subject: RE: [meu] Executing dynamic code
    > >
    > > That defeats my purpose of having the actual code being ignorant of
    > > the XML format. Also, with lots of variables to replace, this would
    > > get tedious to write and slow to process.
    > >
    > > Bill's last suggestion about assigning variables, and then
    > saving to
    > > the database, also defeats my goal.
    > >
    > > I think this must be doable - as an example, Ivo's mail modules can
    > > take MivaScript in their headers/footers. And his modules
    > process that
    > > "dynamic code". Perhaps he's saving to a file and then relying on
    > > Vladdy to execute that file... Not sure if it would be that
    > > complicated or not?
    > >
    > > Susan Petracco
    > > [email protected]
    > > NetBlazon
    > > E-Business for Every Business
    > >
    > > > -----Original Message-----
    > > > From: [email protected]
    > > > [mailto:[email protected]] On Behalf Of Patrick Locke
    > > > Sent: Tuesday, January 18, 2005 12:34 PM
    > > > To: [email protected]
    > > > Subject: Re: [meu] Executing dynamic code
    > > > Importance: High
    > > >
    > > > A token should work for this.
    > > >
    > > > I.E. The value in the DB would be:
    > > > <?xml version="1.0"?><ECommerce action="Request"
    > > > version="1.1"><Requestor><ID>###$$$###</ID></Requestor><Transa
    > > > ctionTrace>Gil
    > > > ligan Rate Request</TransactionTrace></Shipment></ECommerce>'
    > > >
    > > > Then Call it like this.
    > > > <MvASSIGN NAME="xml_message" VALUE="{
    > > > glosub(MyDatabase.d.format,'###$$$###', airgolidd )}">
    > > >
    > > >
    > > > Patrick
    > > >
    > > > ----- Original Message -----
    > > > From: "Susan Petracco" <[email protected]>
    > > > To: "'Bill Gilligan'" <[email protected]>;
    > > <[email protected]>
    > > > Sent: Tuesday, January 18, 2005 6:59 AM
    > > > Subject: RE: [meu] Executing dynamic code
    > > >
    > > >
    > > > > Ok, let me backup - after looking at your example I see
    > > > that I explained
    > > > > this incorrectly. (It was late when I posted last night!)
    > > > What I'd like to
    > > > > do, is store the message format in a database. So (to use
    > > > your example)
    > > > the
    > > > > database would contain the literal:
    > > > >
    > > > > '<?xml version="1.0"?><ECommerce action="Request"
    > > > > version="1.1"><Requestor><ID>' $ airgolidd $
    > > > > '</ID></Requestor><TransactionTrace>Gilligan Rate
    > > > > Request</TransactionTrace></Shipment></ECommerce>'
    > > > >
    > > > > I then assign that to a variable:
    > > > >
    > > > > <MvASSIGN NAME="xml_message" VALUE="{ MyDatabase.d.format }">
    > > > >
    > > > > So xml_message contains the unparsed literal string...it
    > > > hasn't filled in
    > > > > "airgolidd" with the value of the variable called "airgolidd".
    > > > >
    > > > > I then need to parse the string, substituting the variable
    > > > name for the
    > > > > value of that variable, before I pass it via MvCALL.
    > > > >
    > > > > (FWIW, I want to do this so the code isn't hardcoded to a
    > > > specific message
    > > > > format.)
    > > > >
    > > > > Susan Petracco
    > > > > [email protected]
    > > > > NetBlazon
    > > > > E-Business for Every Business
    > > > >
    > > > > > -----Original Message-----
    > > > > > From: [email protected]
    > > > > > [mailto:[email protected]] On Behalf Of Bill Gilligan
    > > > > > Sent: Monday, January 17, 2005 11:43 PM
    > > > > > To: Susan Petracco; [email protected]
    > > > > > Subject: RE: [meu] Executing dynamic code
    > > > > > Importance: High
    > > > > >
    > > > > > Susan,
    > > > > > here is some example code (slightly edited).
    > > > > > ==================================
    > > > > > <MvAssign name="airtest" value="{ '<?xml
    > > version="1.0"?><ECommerce
    > > > > > action="Request" version="1.1"><Requestor><ID>' $ airgolidd $
    > > > > > '</ID></Requestor><TransactionTrace>Gilligan Rate
    > > > > > Request</TransactionTrace></Shipment></ECommerce>'}">
    > > > > >
    > > > > > <MvCALL ACTION =
    > > > > > "https://ecommerce.airborne.com/ApiLanding.asp"
    > METHOD = "XML"
    > > > > > FIELDS = "airtest"> ======================================
    > > > > >
    > > > > > Works like a charm!
    > > > > >
    > > > > > Bill
    > > > > >
    > > > > > -----Original Message-----
    > > > > > From: [email protected]
    > > > [mailto:[email protected]]On
    > > > > > Behalf Of Susan Petracco
    > > > > > Sent: Monday, January 17, 2005 9:40 PM
    > > > > > To: [email protected]
    > > > > > Subject: [meu] Executing dynamic code
    > > > > >
    > > > > >
    > > > > > Seems like I should know this, but don't...and the only
    > > references
    > > > > > I seem to find are for executing a dynamic function call.
    > > > > >
    > > > > > Anyway, I have a variable that has a MivaScript snippet
    > > to define
    > > > > > an xml message. Something like this:
    > > > > >
    > > > > > <MvASSIGN NAME="l.xml" VALUE="{ '<firstname>' $ l.firstname $
    > > > > > '</firstname>'
    > > > > > }">
    > > > > >
    > > > > > I want to parse that code for passing to a script via
    > > MvCALL, so
    > > > > > that I'm actually passing <firstname>Susan</firstname> When
    > > > > > l.firstname is Susan.
    > > > > >
    > > > > > This is doable, right? How?
    > > > > >
    > > > > > Thanks,
    > > > > > Susan
    > > > > >
    > > > > > Susan Petracco
    > > > > > [email protected]
    > > > > > NetBlazon
    > > > > > E-Business for Every Business
    > > > > >
    > > > > >
    > > > > >

    Comment

    Working...
    X