Forum

Thread tagged as: Question, Runway

Help with Template Filters

Hi,

First go with template filters and I am a little stumped. What I want to do with the filter is pretty simple but I can't work it out.

I have this in my template:

<perch:content id="base_price" default="0" type="number" min="0" size="s" label="Base Price (£)" />

I want to automatically generate a version with a 17% markup. I think what I need to do is:

template.html - this is an entry in a collection

<perch:content id="base_price" default="0" type="number" min="0" size="s" label="Base Price (£)" />
<perch:content id="base_price" type="number" filter="markup"  />

filters.php

<?php
  class PerchTemplateFilter_markup extends PerchTemplateFilter
  {
      public function filterAfterProcessing($value, $valueIsMarkup = false)
      {
          $markupRate = 17;
          $value = $value *= (1 + $markupRate / 100);
          $value = round($value, 2);
          return $value;
      }
  }
?>

But when I look on showall in my perch_collection I only have the base_price there. I need to have both the original and filtered values available to be used on my page.

Any help would be much appreciated as I am not sure what to try next.

Mike

Diagnostics are below

Perch Runway: 3.0.9, PHP: 5.6.30, MySQL: mysqlnd 5.0.11-dev - 20120503 - $Id: 76b08b24596e12d4553bd41fc93cccd5bac2fe7a $, with PDO
Server OS: Darwin, apache2handler
Installed apps: content (3.0.9), assets (3.0.9), categories (3.0.9)
App runtimes: <?php $apps_list = [ ];
PERCH_LOGINPATH: /admin
PERCH_PATH: /Users/mikeharrison/Google Drive/Client Work/G H Davies/Site/admin
PERCH_CORE: /Users/mikeharrison/Google Drive/Client Work/G H Davies/Site/admin/core
PERCH_RESFILEPATH: /Users/mikeharrison/Google Drive/Client Work/G H Davies/Site/admin/resources
Image manipulation: GD
PHP limits: Max upload 32M, Max POST 32M, Memory: 128M, Total max file upload: 32M
F1: 3b606135b33e6a102526838f4152a807
Resource folder writeable: Yes
HTTP_HOST: ghd.dev
DOCUMENT_ROOT: /Users/mikeharrison/Google Drive/Client Work/G H Davies/Site
REQUEST_URI: /admin/core/settings/diagnostics/
SCRIPT_NAME: /admin/core/settings/diagnostics/index.php
Mike Harrison

Mike Harrison 37 points

  • 4 years ago
Ryan Gittings

Ryan Gittings 1 points
Registered Developer

The filter should output the adjusted value in the template whilst the non filtered version should do nothing.

It won't show in the showall as it's modified on template render, not passed to the template (I think!)

Duncan Revell

Duncan Revell 78 points
Registered Developer

I would guess that as

<perch:content id="base_price" default="0" type="number" min="0" size="s" label="Base Price (£)" /> 
<perch:content id="base_price" type="number" filter="markup" />

is referencing the same id, you'll only ever get one value returned - the last value.

The template filter code lets you call other content items ($this->content['other_item']), so maybe try:

Template:

<perch:content id="base_price" default="0" type="number" min="0" size="s" label="Base Price (£)" /> 
<perch:content id="markup_price" type="checkbox" value="1" filter="markup" label="Show markup price?" />

Filter:

class PerchTemplateFilter_markup extends PerchTemplateFilter 
{ 
public function filterAfterProcessing($value, $valueIsMarkup = false) 
{ 
$markupRate = 17; 
        if (isset($this->content['base_price'])) {
            $value = $this->content['base_price'] * (1 + $markupRate / 100); 
            $value = round($value, 2); 
            return $value; 
        }
} 
}

Thanks both that's super - will take another run at this tomorrow, fingers crossed I get somewhere!

Duncan I have tried what you suggested and no luck unfortunately. It looks like it should work to me but there is just a 1 output for markup_price, which I guess is just the checkbox value.

I will keep playing, maybe Drew or Rachel might have some insight, as this sort of manipulation to produce an additional value is something I can see myself wanting to use in the future :)

Drew McLellan

Drew McLellan 2638 points
Perch Support

Are you able to demonstrate your filter is working at all? Is it being invoked?

Duncan Revell

Duncan Revell 78 points
Registered Developer

As Drew implies, have you called PerchSystem::register_template_filter('markup', 'PerchTemplateFilter_markup'); anywhere?

I've tried the code I pasted (including the line above) and it works as expected...