Gibt es eine Möglichkeit, dies zu tun, einschließlich der Erstellung einer anderen Build-Markup-Funktion?Rebol: Wie lokale Variablen mit Build-Markup-Funktion verwenden?
2
A
Antwort
1
Leider build-Markup verwendet nur globale Variablen: link text sagt: Beachten Sie, dass Variablen innerhalb von Tags sind immer globale Variablen verwendet.
Hier ist ein wenig verschroben, wie es mit einem inneren Objekt zu tun (bm-1 demonstriert das Problem: a und b sind mit ihren globalen Werten gedruckt; bm-2 ist die verschroben Arbeit um):
a: "global-a"
b: "global-b"
bm-1: func [a b][
print build-markup "<%a%> <%b%>"
]
bm-2: func [a b][
cont: context [
v-a: a
v-b: b
]
print build-markup "<%cont/v-a%> <%cont/v-b%>"
]
bm-1 "aaa" "bbb"
bm-2 "aaa" "bbb"
REBOL3 hat reword statt build-Markup. Das ist viel flexibler.
1
Ich habe die Build-Markup-Funktion gepatcht der Lage sein, lokale Kontexte zu verwenden:
build-markup: func [
{Return markup text replacing <%tags%> with their evaluated results.}
content [string! file! url!]
/bind obj [object!] "Object to bind" ;ability to run in a local context
/quiet "Do not show errors in the output."
/local out eval value
][
content: either string? content [copy content] [read content]
out: make string! 126
eval: func [val /local tmp] [
either error? set/any 'tmp try [either bind [do system/words/bind load val obj] [do val]] [
if not quiet [
tmp: disarm :tmp
append out reform ["***ERROR" tmp/id "in:" val]
]
] [
if not unset? get/any 'tmp [append out :tmp]
]
]
parse/all content [
any [
end break
| "<%" [copy value to "%>" 2 skip | copy value to end] (eval value)
| copy value [to "<%" | to end] (append out value)
]
]
out
]
Hier sind einige Beispiele für den Gebrauch:
>> x: 1 ;global
>> context [x: 2 print build-markup/bind "a <%x%> b" self]
"a 2 b"
>> print build-markup/bind "a <%x%> b" context [x: 2]
"a 2 b"
studieren Ihre schrullige Art, Dank :) Viva R3! –