Rebol:如何将局部变量与构建标记函数一起使用?

发布于 2024-08-02 10:27:26 字数 31 浏览 7 评论 0原文

有没有办法做到这一点,包括创建其他构建标记函数?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木森分化 2024-08-09 10:27:26

遗憾的是,build-markup 仅使用全局变量:链接文本 说:请注意,标签内使用的变量始终是全局变量。

这是使用内部对象(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 而不是 构建标记。这样就灵活多了。

Sadly,build-markup uses only global variables: link text says: Note that variables used within tags are always global variables.

Here's a slightly cranky way of doing it using an inner object (bm-1 demonstrates the problem: a and b are printed with their global values; bm-2 is the cranky work around):

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 has reword rather than build-markup. That is much more flexible.

咆哮 2024-08-09 10:27:26

我已经修补了构建标记函数,以便能够使用本地上下文:

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"

I've patched the build-markup function to be able to use local contexts:

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
]

Here are some example usages:

>> 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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文