dongzhi7641 2013-08-26 20:37
浏览 115
已采纳

如何在HTML文件中显示PHP文件中的变量

im having this function to call templates with .html extension from templates folder because i want to separate html from php

ok this is my function for calling template

class Template {

public $temp;

// function for reading the template from a directory
function gettemplate($template) {

    // check if path is folder
    if (is_dir('templates/')) {

        // check if file exists and if is readable
        if (is_readable('templates/' . $template . '.html')) {

            $this->temp = 'templates/' . $template . '.html';

            return $this->temp;

        } else {
            return false;
        }

    } else {
        return false;
    }

}

This class calling template from templates folder

now what i need is to make example profile.html and profile.php

in profile.html just make variables like First name : <?php echo $first_name; ?>

and in profile.php to define varibales here is profile.php

if (isset($_GET['id']) && !empty($_GET['id']) && is_numeric($_GET['id'])) {

$id     = $filters->validate_url($_GET['id']);
$data   = $users->userdata($id);
$id     = $data['user_id'];

$first_name = $data['first_name'];
$last_name  = $data['last_name'];
$username   = $data['username'];
$email      = $data['email'];
$country    = $data['country'];
$date       = $data['date'];

include ( $template->gettemplate('profile') );

and here is profile.html

<ul>
<li>First name : <?php echo $first_name; ?></li>
<li>Last name : <?php echo $last_name; ?></li>
<li>Username : <?php echo $username; ?></li>
<li>Email : <?php echo $email; ?></li>
<li>Country : <?php echo $country; ?></li>
<li>Member since : <?php echo $date; ?></li>

now my question is how do i make class to call template file just with

$template->gettemplate('profile');

and not with include ($template->gettemplate('profile'));

and to display variables from php file in html file

  • 写回答

1条回答 默认 最新

  • doujiu8918 2013-08-26 21:33
    关注

    From php.net

    Question 1 - display variables from php file in html file

    Use the extract() function " to display variables from php file in html file". It will create a local variable with the name of the array key and the value of the array value

    $data   = $users->userdata($id);
    extract($data, EXTR_SKIP);
    include $template->gettemplate('profile');
    

    Question 2 - call template file just with $template->gettemplate('profile');

    I don't believe there is a way to get around using an include. You could put the include in gettemplate and pass the data array. There are several really good template engine on the net. I recommend looking at a few. I recommend looking at Mustache

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度