erusev/parsedown

Use PHP Variables in Markdown

uvulpos opened this issue · 3 comments

Hey,
I use Parsedown for my Imprint and Privacy Policy so my lawyer can read it and I have to specify some sources from my database. These documents are in one file. How can I insert them? Maybe something like this could be a nice to have feature. At least I found no documentation 🤷🏻‍♂️

<?php
      $current_date = date("Y-m-d");
      $Parsedown = new Parsedown();
      $Parsedown->setVariables(array(
           'current_date' => $current_date
      ));
      echo $Parsedown->text("Today is: {current_date}");
    ?>
<!-- output -->
Today is: 2020-12-15

Using plain str_replace() to replace variable after parsed:

$text = $Parsedown->text('Today is: {current_date}');

$vars = [
    'current_date' => date('Y-m-d'),
    'client' => 'Tom',
    // ...
];

foreach ($vars as $var => $value) {
    $text = str_replace('{' . $var . '}', $value, $text);
}

echo $text;

This would make sense yes, right now I'm doing it this way but I wish there was something like this as a feature in Markdown

My opinion is that this is not what Markdown was created for. You should use a templating engine for this... something like Mustache.