revPrintField and <% %> substitution

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
mmiele
Posts: 55
Joined: Sun Jan 21, 2007 1:25 pm
Location: Naples, Italy

revPrintField and <% %> substitution

Post by mmiele » Tue Jan 29, 2013 8:56 pm

Hi all,

I'm guessing if it is possible to put the content of a variable into a report using the <% %> substituition engine of the revPrintField command. In other words, if I have a field named "myReport" containing:

name: <%MyName%>

if I

Code: Select all

put "Mario" into myName
revPrintField the name of field "myReport"
what I get is

name: myName

and not

name: Mario

The same if I use <%value(myName)%> even if myName is a global var.

Anyone knows a way?

thanks in advance,

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: revPrintField and <% %> substitution

Post by Simon » Tue Jan 29, 2013 10:46 pm

Seems the revPrintField only supports a few "special values" as variables.

Code: Select all

put "Mario" into myName
put myName into fld "myReport" --Add this line to fill the field
revPrintField the name of field "myReport"
Not sure of the way you are using this so I can't help much more.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

mmiele
Posts: 55
Joined: Sun Jan 21, 2007 1:25 pm
Location: Naples, Italy

Re: revPrintField and <% %> substitution

Post by mmiele » Wed Jan 30, 2013 2:49 am

What I want to do is to fill a print template with values coming from variables. I hoped to avoid to write some code by myself using the <% %> substitution.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: revPrintField and <% %> substitution

Post by Simon » Wed Jan 30, 2013 3:38 am

Hi mmiele,
Yes, I understand you would like to use variables that is clear but
You can also use the special value <%pageNumber%> in the field: this value is replaced with the page number.
and the date are the only vars the dictionary. Other functions like <%mouseLoc()%> do work.
Custom functions for a <%nameIn()%>:

Code: Select all

 function nameIn
   put "tom" into tName
   return tName
end nameIn
Don't work.

Code: Select all

put "Mario" into field "myReport
revPrintField the name of field "myReport"
Is just 2 lines of code. Same as your initial posting.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

mmiele
Posts: 55
Joined: Sun Jan 21, 2007 1:25 pm
Location: Naples, Italy

Re: revPrintField and <% %> substitution

Post by mmiele » Sun Feb 03, 2013 8:22 pm

An handler to do the task:

Code: Select all

on subsInFld @myArray,myFieldID,strForEmpty -- replaces each marker <%key%>
   -- with the content of myArray["key"] in the field wich ID is myFieldID.
   -- If myArray["key"] is empty, it replaces the marker with the optional strForEmpty.
   
   put the HTMLText of fld id myFieldID into theText
   put the keys of myArray into klist
   repeat for each line kk in klist
      put empty into t1
      if myArray[kk] is not empty then
         replace ("<%" & kk & "%>") with myArray[kk] in theText
      else
         replace ("<%" & kk & "%>") with strForEmpty in theText
      end if
   end repeat
   set the HTMLText of fld id myFieldID to theText
end subsInFld
This way you can create a RTF or HTML template using an external application, load it into the RTFtext or the HTMLText of a field and fill it with your data (possibly coming from a DB).

Bernard
Posts: 351
Joined: Sat Apr 08, 2006 10:14 pm
Location: London, England

Re: revPrintField and <% %> substitution

Post by Bernard » Sun Feb 03, 2013 9:38 pm

Might the merge() function do what you require?
http://docs.runrev.com/Function/merge

Let your field "Field1" contain this:

hello [[MyName]]

In your script do this:

Code: Select all

put the text of field "Field1" into tTemplate -- we'll put this back later
put "Roger Rabbit" into MyName
put merge(the text of field "Field1") into field "Field1"
revPrintField field "Field1"
set the text of field "Field1" to tTemplate.
This should result in
hello Roger Rabbit
being printed.

None of this tested, just typed in here.

Post Reply

Return to “Talking LiveCode”