【问题标题】:smarty template聪明的模板
【发布时间】:2012-08-29 20:27:10
【问题描述】:

所以问题来了,我有一个 index.php(包含所有 php 代码),我有一个包含所有 html 内容的 index.tpl 文件。但现在因为我使用的是 ajax,所以我有另一个 php 文件应该输出一些数据(data.php)。问题是我不知道如何在 data.php 文件中选择模板,我只知道index.php 我有一个显示tpl ($Smarty->display($filename); 的函数但我不想显示data.php 文件中的模板(再次)我只想分配一些需要在 index.tpl 上显示的变量

编辑:

好的,这会很长: 首先,我需要解释我想要完成什么。我有 index.php 和 data.php。 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);
?>

index.tpl 有点长,所以我将发布重要部分:

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

这是 AJAX 代码,此代码将 GET 方法中的 +str 值发送到 data.php 文件,然后使用该值并从数据库中提取一些数据。

数据.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
?>

我在这里使用 STemplate 的那个类作为 smarty 函数,但你知道代码是什么。

我希望你明白现在的问题是什么。如何在不再次显示模板文件的情况下将变量分配给模板。这样 $name 变量可以在 index.tpl 中访问(名称从 db 中显示)但由于 data.php 中的显示功能,整个内容再次显示。

【问题讨论】:

  • 要么我误解了这个问题,要么你问的是不可能的。您想让 ajax 调用的 data.php 刷新显示的页面吗?内容如何显示两次?请出示您的 JavaScript 代码。

标签: php smarty


【解决方案1】:

使用$smarty-&gt;assign('var', 'value'); 分配值。

更多信息请阅读here

编辑

.tpl 背后的想法是使用assign 输入变量,并在页面准备好时使用display 显示它。您可以在显示之前设置多个变量:

<?php

$smarty = new Smarty();

$smarty->assign('firstname', 'Doug');
$smarty->assign('lastname', 'Evans');
$smarty->assign('meetingPlace', 'New York');

$smarty->display('index.tpl');

?>

如果您看到该文本两次,则表示您在某处拨打$smarty-&gt;display('index.tpl'); 的次数过多。要找到确切的位置,我必须查看您的来源。请发布文件或有问题的位。

祝你好运:)

【讨论】:

  • 感谢您的链接。有些事情现在很清楚了。但我仍然有一个问题。 assign 工作正常,但前提是它后面跟着 $smarty->display('index.tpl');问题是如果我将该行放在 data.php 和 index.php 中,整个内容会显示两次。
  • 嗨@VladimirSabo,如果这有帮助请accept this answer
【解决方案2】:

不知道这是否有帮助。但您也可以将“渲染”的 tpl 返回到您的 AJAX。显示功能通常用于页面的框架。 (有点像所有东西的基本占位符)。并且与页面刷新一起使用,而不是 AJAX。

在data.php中你可以使用

$answer = $smarty->fetch("ajaxreturn.tpl");
echo $answer;
die();

在此之前,您可以在 Smarty 中进行所需的分配。

在 AJAX 中,您可以将返回的 HTML 的 sn-p 放在正确的位置。

【讨论】:

    【解决方案3】:

    我不明白你为什么用 ajax 重新加载整个页面。如果改变的数据是一个列表,你不能为那个列表创建一个模板吗?所以...

    index.tpl

    <body>
    ... content ...
    <div id="ajax_list">
    {include file="data.tpl"}
    </div>
    ... content ...
    </body>
    

    然后在data.tpl中

    <ul>
    {foreach $rows as $row}
    <li>{$row.name}</li>
    {foreach}
    </ul>
    

    第一次输入 index.php 时,它会同时渲染 index.tpl 和 data.tpl,然后你只需要添加 javascript 代码来用 data.php 刷新 #ajax_list 内容,它只会处理 data.tpl

    $smarty->display('data.tpl');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多