PHP variable between message template

I have used a PHP variable inside a message template:

$BASE_PHONE = '1500 etc';

How to use inside message template as it was working inside old PHP but inside a new template it is not shown a value:

<table border="0" cellpadding="0" cellspacing="0" width="100%" style="table-layout: fixed;">
 <tr>
<span class="" style="color:000;"><br>Phone: #BASE_PHONE#</span>
</tr>
</table>

Is there any other PHP needed to work #BASE_PHONE#?

Well that’s not PHP at all. That’s your templating engine.

What’re you using as a template engine?

I am testing manually template engine.
If I understand I can use my example if I use str_replace and it will be used inside template engine?

$message = str_replace("#BASE_PHONE#", $BASE_PHONE, $message);  

What template engine are you using?

I am using manual HTML/CSS coding as I test before using template engine.

Assuming you have defined $BASE_PHONE in a place that the str_replace command can read it, then it would work.

How to manage SUDO element inside a function:

function formatContent($message) {
    $message = <<< SUDO
    
$message .= '<table>
        <tr>
        <td bgcolor="#FFF">

        </td>
        </tr>';
$message .= '</table>';
SUDO;
}

PHP validation will report an error as there is $message variable inside a message. How to modify function in this case as $message neeed inside SUDO.

I’m confused. What are you trying to do there? It looks like you’re trying to put a PHP command inside a string for some reason.

The PHP Docs are surprisingly useful in cases like this:
https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
Example #8 Heredoc string quoting example

function formatContent($message) 
{
    $formattedMessage = <<< SUDO
<table>
  <tr><td bgcolor="#FFF">{$message}</td></tr>
</table>
SUDO;

    return $formattedMessage;
}

I had to guess exactly where you wanted the contents of $message to go. Looks like you are using some kind of old school table formatting. But hopefully you get the idea.

The {} brackets are not technically needed in this case but I usually use them. You do need them for dealing with objects and arrays.

Also you need to html escape output data like $message. Otherwise random characters like < or > can mess up the generated html and cause all kinds of interesting problems in the browser.

I like to add also variable to manage HTML:

$message .= '<table>
        <tr>
        <td bgcolor="#FFF">

        </td>
        </tr>';
$message .= '</table>';

How to place into your example?

function formatContent($message) 
{
    $formattedMessage = <<< SUDO
<table>
  <tr><td bgcolor="#FFF">{$message}</td></tr>
</table>
SUDO;

    return $formattedMessage;
}

so in my case

I need also within SUDO: $message .= '<table>

No offense but I feel like I am talking to a theist on an atheist forum. Different brain wiring.

Please add to your question what you want the final contents of $message to be. Because:

<table>
  <tr><bg color="#FFF"></td></tr>
Some Message
</table>

Makes no sense at all.

1 Like

Please find the whole template engine and see function and template within SUDO element:

                 function formatMailcontent($message)
            {

            $message = <<< SUDO
            
                    $message .= '<table border="0" cellpadding="0" cellspacing="0" width="100%" style="table-layout: fixed;">
        <tr>
        <td bgcolor="#FFF">
            <div align="center" style="padding: 0px 15px 0px 15px;">
                <table border="0" cellpadding="0" cellspacing="0" width="500" class="wrapper">
                    <tr>
                        <td style="padding: 20px 0px 30px 0px;" class="logo">
                            <table border="0" cellpadding="0" cellspacing="0" width="100%">
                                <tr>    
                                    <td bgcolor="#FFF" width="100" align="left"><a href="' '" target="_blank"><img alt="" src="'  '/logo.jpg" width="270" height="100" style="display: block; font-family: Helvetica, Arial, sans-serif; color: #666; font-size: 16px;" border="0"></a></td>
                                    <td bgcolor="#FFF" width="400" align="right" class="mobile-hide">
                                        <table border="0" cellpadding="0" cellspacing="0">
                                            <tr>
                                                <td align="right" style="padding: 0 0 5px 0; font-size: 14px; font-family: Arial, sans-serif; color: #666; text-decoration: none;"><span style="color: #666; text-decoration: none;"><br></span></td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </div>
        </td>
        </tr>';
        $message .= '</table>';
        

            SUDO;

            return $message;
            }

So there is $message = <<< SUDO which is fine but need also additional variable like you see $message .= '<table border=“0” bla bla

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.