Forum

Thread tagged as: Question, Problem, Field-Types

Replace/insert variable within template tag output

May be asking a bit much here but client would like to be able to edit a line of text in the CMS that actually needs to contain a computed value, e.g. "Your booking number is [[computed]]. You will receive an email."

I know I can pass the booking number into the template using PerchSystem::set_var but trying to figure out how to place this within a CMS-editable line of text.

Two options that sprang to mind:

1) Create a field for the content before and after the computer value, but this feels a little clunky:

<perch:content id="textPrefix" type="text" label="Text prefix" />
<perch:content id="bookingID" type="hidden" label="Text prefix" />
<perch:content id="textSuffix" type="text" label="Text suffix" />

2) Create a form of custom markdown, then replace it. I imagine I would need to use perch_content_custom and a PHP find/replace, unless there was a way to use the Perch 'replace' tag attribute?

"Your booking number is [[BOOKINGID]]. You will receive an email."

3) Something I haven't thought of?

Thanks :)

Keir Moffatt

Keir Moffatt 0 points

  • 5 years ago

Read a little about custom field types - not sure if it could pick up a system variable from within a template?

Ooh, actually I recall being able to take content out of a template, process it, then send it back to the template for render - just need to find the documentation or example of when I did it in the past!

Yep, that worked:

$booking_id = '920819021';
perch_content_custom('Title', array(
  'each' => function($item) { // manipulate template data...
    global $booking_id;
    $item['titleText'] = str_replace('[BOOKINGID]',$booking_id,$item['titleText']);      
    return $item; // ...then send it back to template
  }
));
Drew McLellan

Drew McLellan 2638 points
Perch Support

All sorted?