PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, February 18, 2022

[FIXED] Allow users to choose strings to be used in email templates in CakePHP

 February 18, 2022     cakephp, cakephp-3.0     No comments   

Issue

We have a few email templates in our and a very basic template looks like this:

<p> Hi <?= $name?>,</p></br>
<p> A new task has been assigned to you. Please check your tasks list. </p>

We need to allow users to insert their own text if they would like to translate the basic texts.

The easiest way would obviously store two strings/translations for every user:

"Hi"
"A new task has been assigned to you. Please check your tasks list."

and then just to pick them, but the problem arises when a user would for example only like to have the email say: "New task has been assigned" without any greating.

Questions is: how can we allow users to store strings including the html tags and names of php variables?

All of the templates are short and include 3 php variables at max.

If there is any other information that is valuable and I can provide, just ask.


Solution

We need to allow users to insert their own text if they would like to translate the basic texts.

..we have decided to store them in a database

Simple solution.

On frontend allow user to create own email text, and store in db, like:

<p> Hi {{name}},</p></br>
<p> A new task has been assigned to you. Please check your tasks list. </p>
<hr>
<p>Best regards,</p>
<p>{{admin}}</p>
<p>{{email}}</p>

As you see here is {{name}},{{admin}},{{email}} as variable placeholders.

On backend before render email or html page use str_replace() php function to find variable placeholders and replace with other values:

$tpl = $user->template; // load from db
$tpl = str_replace("{{name}}", $user->name, $tpl);
$tpl = str_replace("{{admin}}", $admin->name, $tpl);
$tpl = str_replace("{{email}}", $admin->email, $tpl);

echo $tmpl;

or that answered by drmonkeyninja

   $tpl = str_replace(
      [
         '{{name}}', 
         '{{admin}}', 
         '{{email}}'
      ],
      [
         $user->name,
         $admin->name,
         $admin->email
      ],
      $user->template // load from db
   );
   echo $tpl;


Answered By - Salines
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing