【问题标题】:PHP foreach in TinyMCETinyMCE 中的 PHP foreach
【发布时间】:2021-08-21 09:47:12
【问题描述】:

我有一个从 db 加载的电子邮件 (html) 模板,其中包含 foreach 循环(或者可能是其他一些 PHP 操作),用于动态加载/处理 API 响应的内容。

$content = html_entity_decode($template); //$template loaded from db

$content 的 html

<p style="text-align:center">Hi {name},</p>
<p style="text-align:center">This is header</p>
<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Qty</th>
            <th>Price</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($response as $item){ ?>
        <tr>
            <td><img src="<?php echo $item['img']; ?>" atl="<?php echo $item['name']; ?>" /></td>
            <td><?php echo $item['name']; ?></td>
            <td><?php echo $item['qty']; ?></td>
            <td><?php echo $item['price']; ?></td>
        </tr>
        <?php } ?>
    </tbody>
</table>

API 的响应

$response = array(
    0 => array(
        'name' => 'abc',
        'img'  => 'linkToImg',
        'price'=> '12',
        'qty'  => 10
    ),
    1 => array(
        'name' => 'xyz',
        'img'  => 'linkToImg',
        'price'=> '15',
        'qty'  => 2
    ),
);

在发送电子邮件之前,我使用 str_replace('{name}', $name, $content); 替换令牌,但我不知道如何执行 $content 的 foreach(或者可能是其他一些 PHP 操作)来填充 $response 的所有动态数据和然后发送电子邮件。

我的尝试

我也尝试将令牌用于 $response,但是,如果我需要 $template 的多种设计和 API 的多种响应(用于其他目的的电子邮件),这种方法并不好。例如,在这个模板中,我有 Table对于产品,但在其他模板中,我需要 &lt;ul&gt; &lt;li&gt; 和内联 style 来列出一些功能等。

请建议我如何实现这一目标。

PS:我在前端使用 TinyMCE 来设计电子邮件模板并存储在 DB 中。

【问题讨论】:

  • 你昨天问过这个问题,但他们提供了更多信息,你能再补充一下吗

标签: php html html-email


【解决方案1】:

我找到了解决办法。

简而言之,我需要使用 输出缓冲 例如ob_start();

说明

第 1 步:我将模板存储在 .html 文件中。

第2步:API响应后,输入ob_start();

第 3 步:使用 include('path/to/file.html') 包含模板

第 4 步:获取缓冲区的输出并作为电子邮件消息发送。

我的代码

//Consider HTML template in question is saved as template.html

//Response from API 
$response = array(
    0 => array(
        'name' => 'abc',
        'img'  => 'linkToImg',
        'price'=> '12',
        'qty'  => 10
    ),
    1 => array(
        'name' => 'xyz',
        'img'  => 'linkToImg',
        'price'=> '15',
        'qty'  => 2
    ),
);

ob_start();
include('path/to/template.html');
$message = ob_get_clearn();

//now use $message in sending email
$this->emnail->message($message);

ob_start() 将打开缓冲区并保存 HTML 的输出,在其中执行 PHP,然后 ob_get_clean() 将输出返回到 $message 并清理缓冲区。

好处

通过这种方法,我可以在sendEmail() 中使用any html 模板。我只需要设计Email Templates,剩下的就交给Output Buffer

【讨论】:

    【解决方案2】:

    据我了解,您希望在电子邮件模板中添加更多信息/数据。


    解决方案 #1 假设“$content”具有您想要包含在模板中的相同消息,您可以执行以下操作:

    <?php
    $content = " Thank You!!!";   // some data
    foreach($response as $item){ ?>
            <tr>
                <td><img src="<?php echo $item['img']; ?>" atl="<?php echo $item['name']; ?>" /></td>
                <td><?php echo $item['name']; ?></td>
                <td><?php echo $item['qty']; ?></td>
                <td><?php echo $item['price']; ?></td>
                <td><?php echo $content; ?></td>  // just add the variable.
            </tr>
            <?php } ?>
    

    解决方案 #2:假设 $content 本身是一个数组,那么您可能想要使用:

    foreach (array_combine($courses, $sections) as $course => $section)
    

    查看帖子以获取有关多个数组上的 foreach 循环的更多详细信息:Multiple index variables in PHP foreach loop 希望对你有帮助?

    【讨论】:

      猜你喜欢
      • 2012-01-27
      • 1970-01-01
      • 2017-09-27
      • 1970-01-01
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多