英文:
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:
<a
{% if options.style %}
style="{{ options.style }};
padding: {{data.linkPadding}}"
{% endif %}>some Text</a>
This was answered by
Bob on apostrophe Discord community.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论