7 Replies - 347 Views - Last Post: 07 May 2020 - 10:53 AM Rate Topic: -----

#1 unpocoloco   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-May 20

How to merge data into HTML using PHP

Posted 06 May 2020 - 01:18 PM

I have created a PHP object which assembles pre-defined HTML email messages. The environment is PHP 7.2 with no frameworks. SwiftMailer is the email transport mechanism.

Right now, each template is in a dedicated method within my object and when I call that method, it does the equivalent of a mail-merge and returns the result, which is then inserted as the body of the email msg being composed. The merge inserts a title, logo graphic path, a URL, etc. into it (each template requires different items). The mail-merge tokens occur in both the <head> and <body> sections of each template.

Because the template is right there in the method as text, I use
$this->title
,
$this->url
,
$this->logo
, etc. wherever I need the merged info to appear in the HTML.

This brute-force approach seems overly clunky to me and is difficult to maintain because of the length of each HTML script.

I want to modify it to:

1. Read the HTML from a static file into a PHP variable with placeholders already in place.
2. Perform the merge on that variable using the instantiated data.

Suggestions on the best/simplest way to achieve my goal?

Sample HTML viewable at: Sample Page

Thanks!

This post has been edited by Dormilich: 06 May 2020 - 01:40 PM
Reason for edit:: fixed URL


Is This A Good Question/Topic? 0
  • +

Replies To: How to merge data into HTML using PHP

#2 Splashsky   User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 562
  • Joined: 25-August 13

Re: How to merge data into HTML using PHP

Posted 06 May 2020 - 01:21 PM

The simplest way is to use a framework. A simpler way is to use a lightweight templating engine/library to have your HTML template and have that engine parse your data in. These can be found in abundance on Github, including my own project, Modello. It's ultra lightweight, very limited but very simple to use.

For your purposes, it'll work great. Feel free to ask if you need any further help.
Was This Post Helpful? 0
  • +
  • -

#3 unpocoloco   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-May 20

Re: How to merge data into HTML using PHP

Posted 06 May 2020 - 01:39 PM

OK, downloaded and installed. What's next? I can't find any documentation
Was This Post Helpful? 0
  • +
  • -

#4 Splashsky   User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 562
  • Joined: 25-August 13

Re: How to merge data into HTML using PHP

Posted 06 May 2020 - 01:44 PM

Yeah, I never quite got around to that part lol. Unfortunately I can't myself remember how to use it - when I get home I'll write some examples.

In the meantime, check out nice projects like this one
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6864
  • View blog
  • Posts: 31,627
  • Joined: 10-May 07

Re: How to merge data into HTML using PHP

Posted 06 May 2020 - 03:10 PM

Twig templating is pretty straightforward. There was a while where I was using directus (a headless crm, like wordpress) with twig templates to build small simple sights.
Was This Post Helpful? 0
  • +
  • -

#6 Splashsky   User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 562
  • Joined: 25-August 13

Re: How to merge data into HTML using PHP

Posted 06 May 2020 - 03:11 PM

I wish Blade could exist independently like Twig. Gosh dang it.
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3835
  • View blog
  • Posts: 13,977
  • Joined: 08-August 08

Re: How to merge data into HTML using PHP

Posted 06 May 2020 - 03:57 PM

View Postunpocoloco, on 06 May 2020 - 02:18 PM, said:

I want to modify it to:

1. Read the HTML from a static file into a PHP variable with placeholders already in place.
2. Perform the merge on that variable using the instantiated data.

An untested (but should work with little effort) poor man's template:
$replace = ['{title}'=>$this->title, '{url}' => $this->url,'{logo}'=> $this->logo];
$tags = array_keys($replace);
$replacements = array_values($replace);
$output = file_get_contents('anHTMLtemplate.html');
$output = str_replace($tags, $replacements, $output);
echo $output;

It's just a matter of making sure that your html will use the data correctly.
Was This Post Helpful? 3
  • +
  • -

#8 unpocoloco   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-May 20

Re: How to merge data into HTML using PHP

Posted 07 May 2020 - 10:53 AM

This was the BEST answer I received on ANY forum. THANK YOU!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1