I have two fields. {{ content.field_1 }} is 2; {{ content.field_2 }} is 5.

How do I multiply them to obtain 10, in a template file?


In {{ content }} are render arrays which produce the output "2" and "5", but you can't do arithmetics with these render arrays.

You can try to do this with the database values from the node (if it is a node template):

{{ node.field_1.value * node.field_2.value }}
  • does node.field_1.value not have html markup? surprised this worked, without getting the "raw" value.
    – No Sssweat
    Jun 8 '17 at 7:34
  • @NoSssweat, .value is the raw value from the database. Or do you mean the raw filter, which would switch off any escaping or xss filtering? First this is not supposed to be used for database fields and second for numbers it wouldn't do anything.
    – 4k4
    Jun 8 '17 at 8:08
  • Wouldn't this sort of thing belong in a preprocess instead?
    – Kevin
    Jun 8 '17 at 13:08
  • @4k4 I mean raw filter, but of course don't use that, probably node.field_1.value.0 or | striptags would result non markup value. Cause I thought node.field_1.value would print something like <div class="something">2</div>
    – No Sssweat
    Jun 8 '17 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.