2

Is there a way to do so including creating an other build-markup function ?

4

2 回答 2

1

可悲的是,构建标记仅使用全局变量:链接文本说:请注意,标签中使用的变量始终是全局变量。

这是使用内部对象的一种稍微古怪的方法(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 有 reword不是build-markup。那要灵活得多。

于 2009-08-28T22:34:13.543 回答
1

我已经修补了 build-markup 函数以便能够使用本地上下文:

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"
于 2013-04-20T22:38:17.770 回答