erusev / parsedown

Better Markdown Parser in PHP
https://parsedown.org
MIT License
14.8k stars 1.12k forks source link

Use PHP Variables in Markdown #797

Open uvulpos opened 3 years ago

uvulpos commented 3 years ago

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
taufik-nurrohman commented 3 years ago

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;
uvulpos commented 3 years ago

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

acicali commented 3 years ago

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