2
有沒有辦法做到這一點,包括創建其他構建標記功能?Rebol:如何在構建標記函數中使用局部變量?
有沒有辦法做到這一點,包括創建其他構建標記功能?Rebol:如何在構建標記函數中使用局部變量?
可悲的是,集結標記只使用全局變量:link text說:注意標籤中使用的變量始終是全局變量。
下面是一個使用內對象做這件事的一個稍微暴躁方式(BM-1演示此問題:a和b都印有它們的全局值; BM-2是圍繞胡思亂想工作):
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有改寫而不是集結標記。這更加靈活。
我已經修補集結標記功能能夠使用當地的具體情況:
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
]
下面是一些示例用法:
>> 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"
會研究你胡思亂想的方式,謝謝:)萬歲R3! – 2009-08-29 11:16:54