在html中使用php的特殊标签[关闭]

I've done a bit of research before asking this question, although i've had 0 luck.

What i'm trying to figure out is pretty much how I can make kinda of a template system. I know I could look at how others are done, but I just thought i'd ask to get a simple answer.

Pretty much I want to be able to use something like {Name} or %name% in html, which obviously comes from valuables in a PHP file.

So for example, In a PHP file I have, I have the valuable of $Name == "My Name"; Instead of putting PHP into the html which would look messy which would be <?php echo $Name; ?> I'd like to be able to use %Name% or {Name} instead.

Hopefully someone can understand what I mean, and help me out.

Thank you

As far as I understand, you are looking for str_replace, this will do the job of replacing the keyword with a actual value. I sometimes use it like so:

$template = file_get_contents('test.php');
$name = 'Mrs. Happy Dash.';

//Preps keywords in the template that needs to be replaced with our data.
$keywords = array
(
    "{TITLE}" => 'Thank you for visiting',
    "{INTRO}" => 'I hope you enjoyed your stay ' . $name,
    "{SUB_INTRO}" => '<p style="color:red;">REMEMBER TO PAY!</p>',
    "{ORDER_SUMMARY}" => '1x Sky diving class - 10 Euro',
    "{HOWTOPAY_TITLE}" => 'You can pay like this:',
    "{HOWTOPAY}" => 'Credit card',
    "{FOOTER}" => 'Bye bye'
);

//Replaces the data in the template
foreach($keywords as $search => $data){
    $template = str_replace($search, $data, $template);
}

echo $template;

You can practically use anything you want as keywords, %NAME%,#NAME#. As long as you write them in the keywords array.

The data you are using to replace the keywords can also contain HTML, which is useful if you want to show different styles depending on certain options / criteria.

The above example is of course very rough and can be improved in many ways but it should offer a simple and easy way to do it (and it's easy to adjust to a more personal need / taste).

just do a string replace:

public function _output($tpl, $values) {
    if(!is_readable($tpl)) {
        return ['err'=>'The ('.$tpl.') template is missing or unreadable!'];
    }

    $output = file_get_contents($tpl);
    if($output === false){
        return ['err'=>'Error loading template file ('.$tpl.')'];
    }

    foreach ($values as $key => $value) {
        $tagToReplace = "[@".$key."]";
        $output = str_replace($tagToReplace, $value, $output);
    }

    return $output;
}

usage:

_output('path/to/html/file', ['key'=>'value', 'key'=>'value'])

Here is the breakdown:

  1. Make sure that the html template file exists and is readable

  2. Read the template file into a string

  3. Replace known placeholders with real values

Your html file might look like this:

  <!DOCTYPE html>
<html>
  <head>
    <title>[@site_title]</title>

    <script src="[@polymer_elements]webcomponentsjs/webcomponents-lite.min.js"></script>
    <link rel="import" href="[@polymer_elements]paper-styles/paper-styles.html">
    <link rel="import" href="[@polymer_elements]paper-styles/paper-styles-classes.html">
    <link rel="import" href="[@polymer_elements]paper-header-panel/paper-header-panel.html">
    <link rel="import" href="[@polymer_elements]paper-drawer-panel/paper-drawer-panel.html">
    <link rel="import" href="[@polymer_elements]paper-toolbar/paper-toolbar.html">
    <link rel="import" href="[@polymer_elements]iron-flex-layout/iron-flex-layout.html">
    <link rel="import" href="[@polymer_elements]iron-media-query/iron-media-query.html">
    <link rel="import" href="[@custom_elements]universal/ajax-widgets-loader/ajax-widgets-loader.html">
    <link rel="import" href="[@custom_elements]universal/ajax-app-loader/ajax-app-loader.html">
  </head>

So assuming you have an array like array('polymer_elements'=>'path/to/polymer/folder/', 'site_title'=>'my site title')

Your final output from php will be like

    <!DOCTYPE html>
<html>
  <head>
    <title>my site title</title>

    <script src="path/to/polymer/folder/webcomponentsjs/webcomponents-lite.min.js"></script>
    <link rel="import" href="path/to/polymer/folder/paper-styles/paper-styles.html">
    <link rel="import" href="path/to/polymer/folder/paper-styles/paper-styles-classes.html">
    <link rel="import" href="path/to/polymer/folder/paper-header-panel/paper-header-panel.html">
    <link rel="import" href="path/to/polymer/folder/paper-drawer-panel/paper-drawer-panel.html">
    <link rel="import" href="path/to/polymer/folder/paper-toolbar/paper-toolbar.html">
    <link rel="import" href="path/to/polymer/folder/iron-flex-layout/iron-flex-layout.html">
    <link rel="import" href="path/to/polymer/folder/iron-media-query/iron-media-query.html">
    <link rel="import" href="path/to/polymer/folder/universal/ajax-widgets-loader/ajax-widgets-loader.html">
    <link rel="import" href="path/to/polymer/folder/universal/ajax-app-loader/ajax-app-loader.html">
  </head>

NB: To all the people saying "use smarty", I think smarty and all the other template engines are good. They are developed by a larger community and not just one person, but sometimes we just need a little snippet and not a whole library.