Forum

Thread tagged as: Question, Add-on-development

can i call custom php function in template (html)?

I have a function that formats a string in different way. I am calling that function in php files using below code

echo CustomFormatString('This is my string')

But I want to call this function from template (.html) files too, some thing like below:

<perch:custom function="CustomFormatString"><perch:content id="style_long_desc" /></perch:custom>

The function is written as an addon, and works on php pages (layout/.php and pages/.php)

do you think something like this is possible

sawan ruparel

sawan ruparel 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, you could do that with a custom template handler - you can see examples in both Members and Shop.

A simpler way might be to format it before the template in an each callback.

Simon Clay

Simon Clay 127 points

You might be able to do it by simply using template layouts:

In your template:

<perch:layout path="my.function" string="This is my string to be formatted" />

Then, create a file: perch/templates/layouts/my.function.php containing:

<?php echo CustomFormatString('perch_layout_var('string') ?>
Simon Clay

Simon Clay 127 points

To show that it can be done with editor input:

<perch:layout path="my.function" string="<perch:content id="text" type="text" label="Text" />" />

this worked great guys...

thank you Simon so much for the example code. I could not have figured out without it :-)