Jump to content

Using a variable in a Rule returning tagged text


scubajbc

Recommended Posts

I have a rule that I want to return tagged text.

First I do an if...then statement to set a variable called code,

but I can't figure out how to insert this variable 'code' into the

tagged text part. I tried <variable Name=\"code\"> but output

looks like {code}.

 

Rule looks something like...

if (Field("Account") == "12345")
   var code = Field("barcode1");
else if (Field("Account") == "34567")
   var code = Field("barcode2");
else if (Field("Account") == "56789")
   var code = Field("barcode3");
else
   var code = Field("barcode4");

return "<color Name=\"Black\"><f Name=\"Helvetica 45 Light\"><z newsize=\"7.0\"><variable Name=\"Name\" - <variable Name=\"Account\"><p><f Name=\"WASP 39 LC\"><z newsize=\"12.0\"><leading newsize=\"100\">*<variable Name=\"Account\"><variable Name=\"code\">*";

The very last part of the last line is where I want to insert the variable 'code'.

Anyone have any ideas on how to insert the variable into the tagged text?

 

Mac G5 OSX.4.11

FusionPro Desktop 5.1P2c

Link to comment
Share on other sites

Well, after trying a few things, I did come up with something that worked.

I took the part from the 'code' to the end out of the tagged string and appended it to the tagged string.

 


return "<color Name=\"Black\"><f Name=\"Helvetica 45 Light\"><z newsize=\"7.0\"><variable Name=\"Name\" - <variable Name=\"Account\"><p><f Name=\"WASP 39 LC\"><z newsize=\"12.0\"><leading newsize=\"100\">*<variable Name=\"Account\">" + code + "*";
[/Code]

If anyone has a better solution, I'm all ears.

Link to comment
Share on other sites

To be honest, I've never used a "<variable>" tag to add my variables. I've always done what you did for "code" (in your 2nd post) to add all variables. I would think the latter way would save you a few keystrokes, although likely return the same result. ;)
Link to comment
Share on other sites

There seems to be some confusion about the term "variable," which means different things in different contexts. Specifically, there's a difference between a local JavaScript variable within a rule and a text variable in your FusionPro job.

 

In JavaScript (and other programming/scripting languages), a "variable" is a symbolic name, or reference, to which a value is assigned in code. The keyword "var" can be used (optionally) to declare a generic variable. After it's defined, using the variable's name in JavaScript tells the interpreter to automatically replace (dereference) it with its contained value when evaluating an expression. Thus you can have code such as:

var foo = "bar";
return "The value of the variable is: " + foo;

https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Variables

 

In FusionPro, a text "variable" represents either a value pulled from a data field from your input file or a value returned from a JavaScript rule. In the case of a data field, the name of the variable represents the name of the data field. In the case of a rule, the name of the variable represents the name of the rule. All valid text variables (field and rule values) are listed in the Variable drop-down combo box in the Variable Text Editor dialog.

 

So, if you had a rule named "MyRule" and returned a value, you could then access that value directly in the Variable Text Editor or use that value in another rule by calling the Rule function or using a <variable> tag. For instance:

// The NAME of this function is "MyRule"
var foo = "bar";
return foo;

The code above doesn't create a text variable named "foo" that you can use in your job. (You would need to create a rule named "foo" do do that.) It creates a rule named, well, whatever name you type into the "Rule Name" box in the Rule Editor dialog. If you name the rule "MyRule" as the comment suggests, then you would access the returned value in another rule with the name of the rule, e.g. Rule("MyRule") or '<variable name="MyRule">'. Or you could use the text variable "MyRule" in the Variable Text Editor and you would see "bar" in the output.

 

The JavaScript variable "foo" in the code above is called a local variable, because it's only available within the local context of that rule, and can't be accessed directly by another rule (unless its value is returned). If you wanted to do something else with the local variable "foo" inside the rule, you would just use its name, for instance:

// The NAME of this function is "MyRule"
var foo = "bar";
foo = foo.replace("a", "ee");
return "The value of the variable is: " + foo;

Then if you inserted the text variable "MyRule" in the Variable Text Editor (or called Rule("MyRule") or '<variable name="MyRule">' in another rule), you would see the entire string "The value of the variable is: beer" in the output.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...