0

I am creating a small templating engine for our internal purpose. We have a several HTML templates that contains tokens in the form {token.name.value}. We submit it to the backend which then does its thing and replaces the tokens with their corresponding value. But, that is a long process and there is a lot of back n forth in case a template is not working.

So, I am thinking of having all the tokens as {key:value} pair in the database or a json file. Then we can select the template and the json dictionary file and the application can replaces the tokens in the template using the dictionary file and show us the output.

I can do the rest but I am not sure how can I go about replacing the tokens in the template. How should I parse the html template? Is using preg_replace function recommended?

Thanks.

Blueboye
  • 1,374
  • 4
  • 24
  • 49
  • 1
    something wrong with the many existing template engines? –  Oct 17 '13 at 20:37
  • Which one do you recommend? I went through few but I can't decide if they are the best route to go with. If yes, then which one will be best for what I am trying to do. – Blueboye Oct 17 '13 at 20:47
  • I don't want to say "why do you want to reinvent the wheel?", but seriously it doesn't worth it to create a self made template engine unless you have some really good point. http://www.smarty.net is a pretty good one. – MahanGM Oct 17 '13 at 20:47
  • I'm a smarty fan either...in my life as a php dev, there is a 'before and after smarty' point. – Hackerman Oct 17 '13 at 20:54
  • I understand I can use a templating engine but don't they have their own format for tokens? I need to put tokens similar to this format {token.name.value}. Do they let me chose the formatting of the tokens? – Blueboye Oct 18 '13 at 14:33

1 Answers1

0

IHMO it seems that you need a real template engine for your use case, like the excellent Twig or Smarty. They'll make your life easier by not having to worry about how the template code is parsed and moved around, letting you focus on your application logic.

But sometimes it may be practical to not have to deal with a full-blown template language and still be able to replace simple key:value pairs. For those specific, narrow use cases, here is a little helper function I use.

function replace_tokens($html) {
  $token_format = '/{([^}]+)}/';

  preg_match_all($token_format, $html, $matches, PREG_SET_ORDER);

  foreach ($matches as $match) {
    switch ($match[1]) {
      case 'token.one':
        $html = preg_replace('/' . $match[0] . '/', 'Value One !', $html, 1);
        break;

      case 'token.two':
        $html = preg_replace('/' . $match[0] . '/', 'Value Two.', $html, 1);
        break;

      default:
        break;
    }
  }

  return $html;
}

It is quick and dirty, but it does the parsing job. The switch part is optional, there are many better ways to compare your {key} matches with the actual keys.

Thibaud Colas
  • 1,248
  • 1
  • 16
  • 24
  • The problem is if I have hundred tokens then I will have to write hundred switch cases? I want to put those tokens in a JSON file and then use it as the dictionary. I checked out smarty but Smarty token format is a bit different from what I am trying to use. Though I think there might be a way of doing it there too. – Blueboye Oct 18 '13 at 16:40
  • @Leon No, you don't have to use `switch` at all. You could use an associative array and in the for loop check for the presence of the `$match[1]` key inside the array, replacing the token by the associated value whenever a match is found. But if you are talking about _hundreds_ of tokens, you really should use a full-blown template engine. For the sake of performance and not reinventing the wheel. – Thibaud Colas Oct 19 '13 at 18:39