Jump to content

RockReplacer - Easy Tag Replacements and simple IF-Blocks


bernhard
 Share

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.

Usage:

See the two example Files in the folder /replacements

597c7019b1202_2017-07-2913_12_17-ModulesProcessWire360v2_dev.png.a290a52740203e9bbc2ef7124050c403.png

 

Methods:

  • replacementsTable()
    Renders an overview of all available replacements (see the example in the Module's config file:
    597c70180ec43_2017-07-2913_12_50-ModulesProcessWire360v2_dev.thumb.png.214ab59ba593e18b675b7b28b3d0d59b.png

 

Create new Replacements:

Simply copy the sample file and adopt to your needs.

 

Download:

https://gitlab.com/baumrock/RockReplacer

  • 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:

597c78919b6c1_2017-07-2913_55_28-EditPage_DefaultReport360v2_dev.png.85c7a9fa49472bf87b6d7e8ab1cb104e.png

the users help-section:

597c7904cd1dc_2017-07-2914_00_32-EditPage_addedpdf360v2_dev.png.c0278d6b149221fffe27a5828e86709f.png

the resulting pdf:

597c788f0b552_2017-07-2913_56_11-EditPage_addedpdf360v2_dev.thumb.png.06fd6273b1c37a3a3063b010088d57af.png

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">
        <tr>
          <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>
        </tr>
        <?php foreach($report->getRoles() as $role): ?>
          <tr>
            <td><?= $role->title ?></td>
            <td class="text-center">
              <?php
              $invited = $this->wire->pages->count(
                "template=feedback".
                ",has_parent=$project".
                ",fn=$fn".
                ",fbrole=$role"
                );
              echo $invited;
              ?>
            </td>
            <td class="text-center">
              <?php
              $done = $this->wire->pages->count(
                "template=feedback".
                ",has_parent=$project".
                ",fn=$fn".
                ",fbrole=$role".
                ",fbdone=1"
                );
              echo $done;
              ?>
            </td>
            <td class="text-center"><?= $invited ? round(($done/$invited)*100, 0) : 0 ?>%</td>
          </tr>
        <?php endforeach; ?>
      </table>

      [...]

    </div>
    <?php
    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?

http://modules.processwire.com/modules/process-hanna-code/

 

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:

[superuser]

access

[else]

sorry, only for superusers :)

[/superuser]

 

  • 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
 Share

×
×
  • Create New...