微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

树枝/木材操作员在 WordPress 中对我不起作用

如何解决树枝/木材操作员在 WordPress 中对我不起作用

我已将 productpricesalestax 设置为变量,但在使此运算符正常工作时遇到了一个真正的问题 {{ productprice * 0.salestax }}wordpress 中使用树枝/木材,但它没有计算什么?

然而,这运行良好,完全没有问题 {{ productprice * 0.06 }} 并执行 {{ dump(salestax) }} = string '06' (length=2) 的转储

感谢任何帮助!

解决方法

因为您的转储显示 salestax return string - 它是 NOT 可数的 - 没有数学可以应用。它也不能以这种方式连接 - twig 使用 ~ 进行连接。

为了首先工作,您需要做的是将字符串值转换为整数。你可以在 PHP 端和树枝模板上做到这一点

PHP: 我个人推荐这种方式,因为您的逻辑将应用于应用程序的后端部分。

// *.php

// Get Timber context
$ctx = Timber::context();

$productPrice = 1000; // it is integer
$salesTax = '06'; // it is a string as you would get it now (from post meta or whatever)

// set new variable
$salesPrice = $productPrice * ( 1 - (int) $salesTax / 100 ); // (int) string will return integer - so math is works

//pass it to context
$ctx['salesprice'] = $salesPrice;

// Render twig
Timber::render( 'view.twig',$ctx );

现在在你的树枝文件中你可以访问它(在我的例子中它应该返回 940)

{{ salesprice }} 

树枝: 但是,如果您希望在树枝中处理逻辑,您可以使用 number_format 过滤器

转换字符串
{{ productprice * ( 1 - salestax|number_format / 100 ) }}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。