Forum

Thread tagged as: Add-on-development, Discussion

Template filter example - calculator

Hello everyone (Rachel, Drew - this isn't a support request).

I had an issue (here, if you're interested) - the reason I was passing the data into perch_template() was that I needed the total number of items and I needed to be able to take that total and apply some simple math(s) operations to the number.

As you can't perform math(s) inside a template by default, I thought I would knock together a quick template filter to provide the functionality - here's the code for others to use, should you need similar functionality:

class PepperjackTemplateFilter_calc extends PerchTemplateFilter 
{
    public function filterAfterProcessing($value, $valueIsMarkup = false)
    {
        $value = (int) $value;
        if ($this->Tag->calc) {
            $calcs = explode(',', $this->Tag->calc);
            foreach ($calcs as $calc) {
                $parts = explode(' ', $calc);
                $value = $this->calculator($value, $parts[0], $parts[1]);
            }
        }
        if ($this->Tag->round) {
            switch (true) {
                case ($this->Tag->round == 'up'):
                    $value = (int) ceil($value);
                    break;
                case ($this->Tag->round == 'down'):
                    $value = (int) floor($value);
                    break;
                case ((int) $this->Tag->round > 0):
                    $value = round($value, (int) $this->Tag->round);
                    break;
            }
        }
        return $value;
    }

    private function calculator($operand1, $operator, $operand2)
    {
        $result = 0;
        switch ($operator) {
            case 'add':
                $result = $operand1 + $operand2; 
                break;
            case 'sub':
                $result = $operand1 - $operand2;
                break;
            case 'mul':
                $result = $operand1 * $operand2;
                break;
            case 'div':
                $result = $operand1 / $operand2;
                break;
        }
        return $result;
    }
}

How to use

Enable template filters in your config file:

define('PERCH_TEMPLATE_FILTERS', true);

You will need to register the filter somewhere - up to you where:

PerchSystem::register_template_filter('any_name_eg_calc', 'PepperjackTemplateFilter_calc');

In your template, add the filter to the field you would like to manipulate:

Example - quick calculation:
<perch:content id="perch_item_count" filter="any_name_eg_calc" calc="div 3">

Example - multiple calculations:
<perch:content id="perch_item_count" filter="any_name_eg_calc" calc="div 3,add 10">

Example - control rounding of final value:
<perch:content id="perch_item_count" filter="any_name_eg_calc" calc="div 3,add 10" round="2">

Available options

calc actions: addition; subtraction; multiplication; division
calc="action number" e.g. "add 10" (separate with space) - one calculation
calc="action number,action2 number" e.g. "div 3,add 10" - multiple calculations separated with commas
The actions simply happen one after the other.

round options: up PHP ceil; down PHP floor; number number of decimal places

I will whack this up on GitHub at some stage, but hopefully this is useful.

Duncan Revell

Duncan Revell 78 points

  • 3 years ago

Genius. Thanks Duncan.