在apostrophe CMS的视图文件(宏)中如何添加另一个样式属性?

huangapple go评论92阅读模式
英文:

How do I add another attribute to style in a view file (macro) in apostrophe CMS?

问题

Sure, here's the translated code:

所以我有一个类似这样的代码:
<a {% if options.style %} style="{{ options.style }}" {% endif %} href="{{ path }}">一些文本</a>

现在我想要为`padding`添加一个额外的属性,比如来自另一个属性的`data.linkPadding`。我应该怎么做?

我尝试了这种方式,但它没有生效:
<a {% if options.style %} style=" {{ {...options.style, padding: {{data.linkPadding}}} }}" {% endif %}>一些文本</a>

Is there anything else you'd like me to translate?

英文:

So I have a code that's like this:

  <a {% if options.style %} style="{{ options.style }}" {% endif %} href="{{ path }}">some text</a>

Now I want to add an additional property tp style, say for padding that's coming from another attribute. How can I do that?

I tried this way but it didn't work.

<a {% if options.style %} style=" {{ {...options.style, padding: {{data.linkPadding}}} }}" {% endif %}>some Text</a>
 
  

</details>


# 答案1
**得分**: 1

由于这是 nunjucks(它不像扩展 JavaScript 对象那样工作,正如我试图做的那样),要实现这个目标的方法如下:

<a
{% if options.style %}
style="{{ options.style }};
padding: {{data.linkPadding}}"
{% endif %}>一些文本</a>


这是由
[Bob][1] 在 apostrophe Discord 社区上回答的。


[1]: https://stackoverflow.com/users/19515646/bob-means
英文:

Since this is nunjucks (it doesn't work like just extending javascript object as I was trying to do), the way to do this would be following:

&lt;a
     {% if options.style %} 
        style=&quot;{{ options.style }}; 
                 padding: {{data.linkPadding}}&quot;
     {% endif %}&gt;some Text&lt;/a&gt;

This was answered by
Bob on apostrophe Discord community.

huangapple
  • 本文由 发表于 2023年4月10日 19:39:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定