使用 pug mixin 结果作为属性值 [英] Use pug mixin result as attribute value

查看:75
本文介绍了使用 pug mixin 结果作为属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要完成的工作的简化版本:

Here is a boiled-down version of what I'm trying to accomplish:

mixin foo(bar)
    = bar + ".html"

a(href= +foo("baz")) test

我希望将锚标记编译为 <a href="baz.html">test</a>,但我得到的是类型错误, foo 不是函数.虽然我确实看到它在技术上不是一个函数,但这不是一个混合有用的场景吗?我在 pug 文档中搜索了与我类似的用例场景,但没有成功.

I'd like to have the anchor tag be compiled as <a href="baz.html">test</a>, but what I'm getting instead are type errors, on foo not being a function. Although I do see that it technically isn't a function, is this not a scenario where a mixin would be useful? I've searched the pug documentation for use-case scenarios similar to mine, but without success.

我在这里尝试使用 mixin 可以实现吗?或者这只能通过作为上下文变量传递的常规 JS 函数实现?

Is what I'm trying to achieve here possible with mixins? Or is this only possible with regular JS functions passed as context variables?

推荐答案

我认为您想为此使用无缓冲 Javascript.对于您的用例,代码将如下所示.

I think you want to use unbuffered Javascript for this. For your use case, the code would be like so.

-
    function foo(bar) {
        return bar + ".html";
    }

a(href=foo("baz")) test

这将产生以下 HTML:

This would result in the following HTML:

<a href="baz.html">test</a>

说明

Unbuffered Javascript 是模板逻辑,不会在最终结果中发出.未缓冲的 Javascript 由破折号 (-) 注释.多行无缓冲 Javascript 由带有单个制表符缩进的破折号定义.

Unbuffered Javascript is template logic which will not be emitted in the final result. Unbuffered Javascript is annotated by a dash (-). Multi line unbuffered Javascript is defined by a dash with a single tab indent.

单行

-var foo = "bar"; 

多行

-
    function randomNumber() {
        return 4;
    }

文档:https://pugjs.org/language/code.html

这篇关于使用 pug mixin 结果作为属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆