dongyuxiao6295 2012-08-29 20:27
浏览 30

聪明的模板

So here is the problem, I have an index.php (with all the php code) and I have an index.tpl file with all the html stuff. But now because I am using ajax I have another php file that should output some data (data.php). The problem is that I don't know how to select the template in the data.php file, all I know is on the index.php I have a function that displays the tpl ($Smarty->display($filename);) but I don't want to display the template (again) in the data.php file I just want to assign some variables that need to show on index.tpl

EDIT:

Ok this is going to be long: First I need to explain what do I want to accomplish. I have the index.php and the data.php. The index.php:

<?php
include("../include/config.php");
include("../include/functions/import.php");

$thebaseurl = $config['baseurl'];

    $query ="SELECT name FROM contacts";
    $results = $conn->execute($query);
    $select-names = $results->getrows();
    STemplate::assign('select-names',$select-names);

$templateselect = "index.tpl";
STemplate::display($templateselect);
?>

The index.tpl is a little long so I will post the important part:

xmlhttp.open("get","data.php?q="+str,true);

This is AJAX code, this code sends +str value in GET method to the data.php file that then uses the value and pulls some data from the database.

data.php:

$q=$_GET["q"];

$sql="SELECT * FROM contacts WHERE name = '$q'";

$result = mysql_query($sql);


while($row = mysql_fetch_array($result))
  {
    $name = $row['name'];
  }

STemplate::assign('name',$name);
$templateselect = "index.tpl";
STemplate::display($templateselect); //the second display
?>

I use that class here STemplate for the smarty functions, but you get what is the code.

I hope you understand what is the problem now. How can I assign variables to a template without displaying the template file again. This way the $name variable is accessible in the index.tpl (the name is shown from the db) but the whole content is shown again because of the dispaly function in the data.php.

  • 写回答

3条回答 默认 最新

  • duanmao1319 2012-08-29 20:38
    关注

    Use $smarty->assign('var', 'value'); to assign values.

    For more information read more here.

    EDIT

    The idea behind the .tpl is to enter the variables using assign, and when the page is ready display it using display. you can set multiple variables before displaying:

    <?php
    
    $smarty = new Smarty();
    
    $smarty->assign('firstname', 'Doug');
    $smarty->assign('lastname', 'Evans');
    $smarty->assign('meetingPlace', 'New York');
    
    $smarty->display('index.tpl');
    
    ?>
    

    If you see the text twice that means that somewhere you're calling $smarty->display('index.tpl'); once too many. To find exactly where I'll have to see your source. please post the files or the problematic bit.

    Good luck anyhow :)

    评论

报告相同问题?

悬赏问题

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