Skip to content

Conversation

@intoeetive
Copy link
Contributor

@intoeetive intoeetive commented Aug 3, 2024

Enabled performing mathematical operations in templates using layout:set:math tag
Added :math modifiers on variables and exp:math module

This PR is allowing to perform mathematical operations prior to assigning the result to layout variable. To achieve that, use {layout:set:math} tag

The following code will produce something like 1409.24202

{exp:channel:entries channel_id="1" entry_id="1"}
{layout:set:math name="math"}{entry_date}/1000000{/layout:set:math}
{/exp:channel:entries}

Under the hood, it's using EvalMath library, which is the same one that mx_calc is using.

Another two options are using variable modifier and module

Variable modifier takes the variable (e.g. segment_1) as first part of expression and optional expression parameter as its second part. Additionally, function can be applied to the result. So if we want to divide to entry date by by 10, add 5 to the result and then take square root of it we can do the following

{entry_date:math expression="/10+5" function="sqrt"}

Module tag works the same as variable modifier, but it expects complete expression. It can be used as single tag as a tag pair. When used as tag pair, the result is in result variable

{exp:math expression="/10+5" function="sqrt"}
{result}
{/exp:math}

Both modifier and tag return the result formatted. They accept same parameters as :number_format modifier

The documentation is here: ExpressionEngine/ExpressionEngine-User-Guide#994

@intoeetive intoeetive added enhancement New feature or request needs documentation labels Aug 3, 2024
@intoeetive intoeetive added this to the 7.x milestone Aug 3, 2024
@intoeetive intoeetive changed the title Enabled performing mathematical operations in templates using layout:set:math tag Enabled performing mathematical operations in templates using layout:set:math tag Aug 3, 2024
# Conflicts:
#	build-tools/build.json
#	composer.lock
#	system/ee/ExpressionEngine/Tests/bootstrap.php
#	system/ee/installer/controllers/wizard.php
#	system/ee/legacy/libraries/Core.php
#	tests/cypress/support/config/config.php
#	vendor-bin/php-scoper/composer.lock
@intoeetive intoeetive changed the title Enabled performing mathematical operations in templates using layout:set:math tag Enabled performing mathematical operations in templates using layout:set:math tag, :math variable modifier and exp:math module May 27, 2025
@intoeetive intoeetive modified the milestones: 7.x, 7.6.0 May 27, 2025
@intoeetive intoeetive marked this pull request as draft May 28, 2025 05:30
@intoeetive intoeetive marked this pull request as ready for review May 28, 2025 05:30
@intoeetive intoeetive marked this pull request as draft May 28, 2025 13:21
@intoeetive intoeetive marked this pull request as ready for review May 28, 2025 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants