Jump to content

RockReplacer - Easy Tag Replacements and simple IF-Blocks


Recommended Posts

I built this module because I needed a versatile solution to replace tags and simple if-blocks in some E-Mails and PDF documents.

If you only need to replace static tags (no if-conditions), then you can use default PW api and need no module:

$str = "My favourite color is {color}.";
$texttools = $sanitizer->getTextTools();
echo $texttools->populatePlaceholders($str, ['color' => 'red']);
// output: My favourite color is red.


See the two example Files in the folder /replacements




  • replacementsTable()
    Renders an overview of all available replacements (see the example in the Module's config file:


Create new Replacements:

Simply copy the sample file and adopt to your needs.




  • Like 8
Link to comment
Share on other sites

one more complex example:

in the pdf creating module:

$mpdf->WriteHTML(modules('RockReplacer')->replace($block->body, ['report' => $this->report]));

the users edit-screen:


the users help-section:


the resulting pdf:


and the replacementfile:

<?php namespace RockReplacer;
class dataoverview extends Replacer {

  public function desc() {
    return $this->_('Fügt eine Tabelle mit einer Zusammenfassung über alle Feedbacks + Rollen ein');
  public function replace($vars) {
    $report = $vars['report']; 
    $project = $report->closest('template=project');
    $fn = $report->fn;

    ob_start(); ?>
    <div class="dataoverview">
      <h3><?= $this->_('Feedbackübersicht'); ?></h3>
      <table class="size-psmall striped">
          <th class="text-center"><?= $this->_('Rolle'); ?></th>
          <th class="text-center"><?= $this->_('Eingeladen'); ?></th>
          <th class="text-center"><?= $this->_('Abgegeben'); ?></th>
          <th class="text-center"><?= $this->_('Rücklaufquote'); ?></th>
        <?php foreach($report->getRoles() as $role): ?>
            <td><?= $role->title ?></td>
            <td class="text-center">
              $invited = $this->wire->pages->count(
              echo $invited;
            <td class="text-center">
              $done = $this->wire->pages->count(
              echo $done;
            <td class="text-center"><?= $invited ? round(($done/$invited)*100, 0) : 0 ?>%</td>
        <?php endforeach; ?>


    return ob_get_clean();



  • Like 4
Link to comment
Share on other sites

17 hours ago, dragan said:

Looks nice!

How is this different to Hanna Code? In the old days, when I used MODX, I used a lot of such tags (and MODX Evo snippets were all basically using similar syntax). Can RockReplacer generally speaking do more, i.e. is it more versatile?



i'm not sure but i think hannacode does not support the if statements like

[myifstatement]this is shown only to superusers for example[/myifstatement]

second, hannacode is a textformatter module that works only as a formatter for fields. i use my module to replace tags in emails and pdfs (ok that would also be possible with the textformatter and i'm thinking of adding a textformatter to call all my replacements, but i just wanted to have my own way of doing it...).

and third, i prefer to define things with single files and not to write code into some field's config field. you can then use git for version control.

next on the roadmap is to support else-statements like this:




sorry, only for superusers :)



  • Like 5
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...