开发者

How can I parse place holder text in a HTML file which are then replaced with custom tags?

First a bit of background information. I create HTML emails at my work place and the whole process is very tedious. It goes a little little like this...

  1. Code markup for HTML using tables and some CSS
  2. Parse HTML and CSS using Premailer so all CSS is inline
  3. Test HTML works in all email clients
  4. Create a copy of the inline versio开发者_如何学运维n of HTML and start adding in proprietary variables to email tool used for sending emails, ie <%=constant.first_name%>, <%=unsubscribe_link%>
  5. Test in email client to see if it works and client is happy. If not repeat steps 1 through 5 again.

So as you can see it gets really tedious after a while.

What I would like to do is create a command line script similar to Premailer which allows me to parse a HTML file with variables stored in it without destroying the example text already in the HTML. That way when you are previewing the HTML it all looks dandy.

For example...

Store the first name function as a variable for own use.

$first_name = "<%=constant.first_name%>

Then tell the parser what word(s) to replace with the appropriate variable.

<p>My name is <!-- $first_name -->Gavin<!-- /$first_name --></p>

So that the final output looks something like:

<p>My name is <%=constat.first_name%></p>

Would such a thing be possible? Is there a better syntax I could, a custom tag like <first_name>Gavin</first_name>, if the browser can handle it.

Any advice is helpful. :)


I've seen this done before using a syntax like:

{assign_variable:first_name="Jesse"}

Then, you could use it like:

{first_name}

The way you'd parse this (provided you're using PHP) would be something like:

<?php
// Our Template Code
$strHTML = <<<EOT

    {assign_variable:first_name="Jesse"}
    {assign_variable:last_name="Bunch"}

    Hello, {first_name}!

EOT;

// Get all the variables
$arrMatches = array();
preg_match_all('/\{assign\_variable\:([a-zA-Z\_\-]*)\=\"([a-zA-Z0-9]+)\"\}/', $strHTML, $arrMatches);

// Remove the assign_variable tags
$strHTML = preg_replace('/\{assign\_variable\:([a-zA-Z\_\-]*)\=\"([a-zA-Z0-9]+)\"\}/', '', $strHTML);

// Combine them into key/values
$arrVariables = array_combine($arrMatches[1], $arrMatches[2]);
foreach($arrVariables as $key=>$value) {

    // Replace the variable occurrences
    $strHTML = str_replace('{' . $key . '}', $value, $strHTML);

}

// Send the parsed template
echo $strHTML;

Which outputs:

Hello, Jesse!

Note, this is a very basic example. Here are some improvements to make on this code before using it in production:

  • Edit the regex to allow the right characters.
  • Maybe implement a better replacement method than a loop
  • Check for parse errors
  • Benchmark performance

All in all, I think you get the idea. Hope this points you in the right direction.


I have a similar situation
I have created a "format template" like this:

<?php // section1 $var1/$var2  ?>
<head>
   <title>$var1</title>
   <meta name="description" content="$var2">
</head>
<?php // section2 $var1/$var2  ?>
<body>
   hello: <p>$var1</p>
   news for you: <p>$var2</p>
</body>

it is valid php code and valid html code, so you can edit it with dreamwaver or similar, and you can host it also.
then a php script replaces all ocurrences of vars in all sections.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜