Forum

Thread tagged as: Question, Problem

Editable text passed into a PHP function

In my PHP page I use a PHP function to encode email addresses so that they are not easily harvested from my pages. My original pages had repeated sections like this:

<div class="staff-member">
<div class="fullname">Firstname Lastname</div>
<div class="contact">
     <? cryptoMailTo("XXXX@mydomain.edu"); ?>
</div>
</div>

I would like to make these editable, and repeatable for an unknown number of staff members. I found I was able to make the email address an editable field by simply passing a perch_content() call as an argument to my cryptoMailTo function, like this:

<? cryptoMailTo(perch_content("unique_email_id ")); ?>

This works great for single fields, defined with their own ID. The editable data is passed right into my function. If the staff roster was a fixed size, my problem would be solved. However, I need to make this block of text into a repeatable template which can be used for a variable number of staff members. But since perch templates are HTML files, I don’t see how I can possibly use my php function within the template.

Is there a way to accomplish this?

Thanks

Perch: 3.0.13, PHP: 7.1.9, MySQL: mysqlnd 5.0.12-dev - 20150407 - $Id: b396954eeb2d1d9ed7902b8bae237b287f21ad9e $, with PDO
Server OS: WINNT, apache2handler
Installed apps: content (3.0.13), assets (3.0.13), categories (3.0.13)
App runtimes: <?php $apps_list = [ ];
PERCH_LOGINPATH: /cms
Image manipulation: GD
PHP limits: Max upload 2M, Max POST 8M, Memory: 128M, Total max file upload: 2M
F1: 3b606135b33e6a102526838f4152a807
Resource folder writeable: Yes
HTTP_HOST: wade
DOCUMENT_ROOT: D:/WEB/SITES/wade
REQUEST_URI: /cms/core/settings/diagnostics/
SCRIPT_NAME: /cms/core/settings/diagnostics/index.php
Dominic Tocci

Dominic Tocci 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

cryptoMailTo(perch_content("unique_email_id", true));

Thanks for the reply. I'm not sure that answers my question though.

The problem is that I want to setup a template for each staff member but I need to call a PHP function on one piece of data within the template. So for example:

staffmember.html

<div class="staff-member">
<div class="fullname">
     <perch:content id="name" type="smarttext" label="First Name Last Name" required="true" />
</div>
<div class="contact">
     <? cryptoMailTo("<perch:content id="email" type="text" label="Email" required="true" />"); ?>
</div>
</div>

The PHP code inside the html template file is not processed and outputs directly to the page. I suppose by adding the "true" argument to the perch_content() call for the template data, I could then call an eval() on the resulting string, which would then parse the PHP.

$myvar = perch_content("staff_member",true);
eval(' ?>'.$myvar.'<?php ');

But that seems like a hack and introduces performance and security concerns. The only other alternative I can think of would be to change my cryptoMailTo function to parse a block of HTML code and encode any mailto: links within it.

Are these my only options or is there something else built into Perch for these situations?

Drew McLellan

Drew McLellan 2638 points
Perch Support

I think you need to use a template filter.

https://docs.grabaperch.com/api/template-filters/

Dominic, if you use a template filter for this, could you post it here? Thanks!

Thanks, Drew.

Montgomery, this is the filter I made. It's just a block of code in the file addons/templates/filters.php

class PerchTemplateFilter_mailto extends PerchTemplateFilter 
{
    public $returns_markup = true;
    public function filterAfterProcessing($value, $valueIsMarkup = false)
    {
        return cryptoMailTo($value);
    }
}
PerchSystem::register_template_filter('mailto', 'PerchTemplateFilter_mailto');

In my research I also realized that I could just grab the data from perch as an associative array and then loop over it in my php code. It's a little more back-to-basics but it gives a lot more freedom to format the data.

https://docs.grabaperch.com/perch/content/functions/get-content-before-templating/

Thanks, Dominic!