Kevin Kopf
Kevin Kopf

Reputation: 14210

Smarty get the while template from the class

I'm totally new to smarty... and it's creeping me out :)

I got the following class in /inc/class/search.php:

Class search
{
    function __construct($request) {
        global $dbconn;
        $request = htmlspecialchars($request);

        $sql = "SELECT * FROM table WHERE id LIKE '%{$request}%'";
        $res = $dbconn->Query($sql);
        $entity = $res->fetchArray();
        return $entity;
    }
}

I have this in php_head.php:

if (isset($_REQUEST['search']) && !empty($_REQUEST['search'])) {
    $is = new search($_REQUEST['search']);
    $smarty->assign("searchValues", $is);
}

This code in php_head is designed to be called by ajax later on. But when I run index.php?search=string I get the whole smarty template. Please help.

Upvotes: 0

Views: 73

Answers (2)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111829

What you need to do is displaying only some part of output when search is in URL.

So you could modify your code this way:

if (isset($_REQUEST['search']) && !empty($_REQUEST['search'])) {
    $is = new search($_REQUEST['search']);
    $smarty->assign("searchValues", $is);
    $smarty->display('searchvalues.tpl'); // custom base template
    exit; // stop execution of later code
}

And you should create searchvalues.tpl template and display here only this part that you want to display and not the whole base template.

Upvotes: 1

Angel Bonev
Angel Bonev

Reputation: 64

You need to clear template that you need for ajax, and if you want to include it in some other template

{include file="path_to_template.tpl"}

And when you need only the result from this template use

echo $smarty->fetch('path_to_template.tpl');

For example you have :

$smarty->display('index.tpl');// this will return index.tpl

And in index.tpl :

<div id="result_ajax">
    {include file="ajax_template.tpl"}
</div>

And in ajax.php :

//Do some stuff
echo $smarty->fetch('ajax_template.tpl');

Upvotes: 0

Related Questions