简体   繁体   中英

smarty template

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.

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 :)

Don't know if this helps. but you can also return a 'rendered' tpl to your AJAX. The display-function is normaly used for the frame of the page. (sort of like the basic placeholder for everything). and is used with a page-refresh, not AJAX.

In data.php you can use

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

before that, you can do the needed assigns in Smarty.

In AJAX you can then place the snippet of returned HTML in the right place.

I don't understand why do you reload the whole page with ajax. If the data that changes is ie a list, can't you just create a template for that list? so...

index.tpl

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

and then in data.tpl

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

The first time you enter index.php it will render both index.tpl and data.tpl, and then you just have to add javascript code to refresh #ajax_list contents with data.php, that will only process data.tpl with

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM