1 (edited by IceFrog 2008-12-09 12:45:02 PM)

Topic: Help with smarty plz

Sorry for my English.

The script works without a smarty:

<?php

error_reporting(E_ALL);
require_once("xajax/xajax_core/xajax.inc.php");
$xajax = new xajax();
//$xajax->setflag('debug', true);
$test = $xajax->register(XAJAX_FUNCTION,'myFunction');
$xajax->processRequest();
function myFunction($get) {
    global $error;
    $error = '';
    $objResponse = new xajaxResponse();
    $msg = $get['msg'];
    $objResponse->assign("query", "innerHTML","$msg");
    return $objResponse;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<?php
if(isset($_GET['work']))
{
$xajax->printJavascript('xajax');
echo "
<html>
<head>
<title>Form Validation Sample</title>
</head>
<body>
<form id=\"query_form\">
<input id=\"msg\" name=\"msg\"><br>
<input type=\"submit\" value=\"Submit\" onclick=\"xajax_myFunction(xajax.getFormValues('query_form'));return false;\">
</form>
<br>
<div id=\"query\"></div>
</body>
</html>";
}
?>

But the smarty does not work.
Help me his rebuilt under smarty, as very much want to use xAJAX + smarty

My Smarty Code + TPL:

<?php
require('libs/Smarty.class.php');

$smarty = new Smarty;
$smarty->caching = false;

$smarty->template_dir =   'templates/';
$smarty->compile_dir =    'templates_c/';
$smarty->config_dir =     'configs/';
$smarty->cache_dir =      'cache/';

?>

template

<td valign="top">
==> THIS_xAJAX_RESULT <==
</td>

Re: Help with smarty plz

See http://community.xajaxproject.org/viewtopic.php?id=4358

You just do a $smarty->fetch() into you're $msg, setting any $smarty->assign() content beforehand as normal.

Re: Help with smarty plz

Sorry for audacity, but could you give the finished code, because this is not a difficult task, an example of the administrator very hard for me.
Many thanks in advance

Re: Help with smarty plz

This should do it for you.  You can even save the template output into a database with this if you wanted.

function myFunction($get) {

    global $error;

    require('libs/Smarty.class.php');
 
    $smarty = new Smarty;
    $smarty->caching = false;
 
    $smarty->template_dir =   'templates/';
    $smarty->compile_dir =    'templates_c/';
    $smarty->config_dir =     'configs/';
    $smarty->cache_dir =      'cache/';


    $error = '';
    $objResponse = new xajaxResponse();
    $msg = $get['msg'];

    $smarty->assign("your_tag",$msg);
    $output = $smarty->fetch("your_template");

    $objResponse->assign("query", "innerHTML",$output);

    return $objResponse;

}

Not sure why you are clearing the $error global after declaring it a global.  You can probably drop that since you are not using it.

Re: Help with smarty plz

thx, i'm try this next morning. and say result.

Re: Help with smarty plz

thx dannic, script it work in simple template, but:

(more hard construction)
What should I do if the file already contains test.php:

// LOAD Smarty
require('libs/Smarty.class.php');

$smarty = new Smarty;
$smarty->caching = false;

$smarty->template_dir =   'templates/';
$smarty->compile_dir =    'templates_c/';
$smarty->config_dir =     'configs/';
$smarty->cache_dir =      'cache/';
........................
.......................
........................

And if I add below(The script contains a condition)

function myFunction($get) {
 
    global $error;
    require('libs/Smarty.class.php');
 
    $smarty = new Smarty;
    $smarty->caching = false;
 
    $smarty->template_dir =   'templates/';
    $smarty->compile_dir =    'templates_c/';
    $smarty->config_dir =     'configs/';
    $smarty->cache_dir =      'cache/';

 
    $error = '';
    $objResponse = new xajaxResponse();
    $msg = $get['msg'];
 
    $smarty->assign("test",$msg);
    $output = $smarty->fetch("test.tpl");
 
    $objResponse->assign("query", "innerHTML",$output);
 
    return $objResponse;
 
}

Then this not work sad
I'm trying, trying, LAMER sad

This MY SCRIPT:

admin.php

<?

error_reporting(E_ALL);
require_once("xajax/xajax_core/xajax.inc.php");
$xajax = new xajax();
//$xajax->setflag('debug', true);
$test = $xajax->register(XAJAX_FUNCTION,'myFunction');
$xajax->processRequest();


// LOAD Smarty
require('libs/Smarty.class.php');

$smarty = new Smarty;
$smarty->caching = false;

$smarty->template_dir =   'templates/';
$smarty->compile_dir =    'templates_c/';
$smarty->config_dir =     'configs/';
$smarty->cache_dir =      'cache/';

$pageTitle = "CONFIG MENU";
$smarty -> assign('pageTitle', $pageTitle);

//GENERAL CONFIG
if(isset($_GET['general_conf'])) 
{
$deystvie = "GENERAL CONFIG";
$smarty -> assign('deystvie', $deystvie);

$spisok = '
<a href="admin.php?general_conf&work">Work site</a>
';
$smarty -> assign('spisok', $spisok);

if(isset($_GET['work']))
//============START IF
{ 
function myFunction($get) {
 
    global $error;
    require('libs/Smarty.class.php');
 
    $smarty = new Smarty;
    $smarty->caching = false;
 
    $smarty->template_dir =   'templates/';
    $smarty->compile_dir =    'templates_c/';
    $smarty->config_dir =     'configs/';
    $smarty->cache_dir =      'cache/';

 
    $error = '';
    $objResponse = new xajaxResponse();
    $msg = $get['msg'];
 
    $smarty->assign("spisok",$msg);
    $output = $smarty->fetch("admin.tpl");
 
    $objResponse->assign("query", "innerHTML",$output);
 
    return $objResponse;
 
}
$xajax->printJavascript('xajax');
$spisok = "<form id=\"query_form\">
<input id=\"msg\" name=\"msg\"><br>
<input type=\"submit\" value=\"Submit\" onclick=\"xajax_myFunction(xajax.getFormValues('query_form'));return false;\">
</form>
<div id=\"query\"></div>";
}
//============END IF

$smarty -> assign('spisok', $spisok);
$smarty -> assign('deystvie', $deystvie);
}

$smarty->display('admin.tpl');
?>

admin.tpl

<table>
    <tr>
        <td width="300">MENU:</td>
        <td></td>
    </tr>
    <tr>
        <td valign="top">
        <a href="admin.php?general_conf">General Config</a><br/>
        </td>
        <td valign="top">
        {$deystvie}
        <br/><br/>
        {$spisok}
        </td>
    </tr>
</table>

Re: Help with smarty plz

Hi dannic, IceFrog

Sorry IceFrog but it's not obvious to me what your application is trying to do.

However in general one technique when using Smarty and xajax is to split your Smarty templates into nested parts that one includes, eg have the main template admin.tpl:
<html>
<head>
<title>{$pageTitle}</title>
{$xajax_script} {* This is $smarty->assign('xajax_script', $xajax->printJavascript()); *}
</head>
<body>
      <div id="i_content">
{include file="admin_content.tpl"}
      </div>
</body>
</html>

and then have a separate template admin_content.tpl:
    <table>
          <tr>
              <td width="300">MENU:</td>
              <td></td>
          </tr>
          <tr>
              <td valign="top">
              <a href="admin.php?general_conf">General Config</a><br/>
              </td>
              <td valign="top">
              {$deystvie}
              <br/><br/>
              {$spisok}
              </td>
          </tr>
    </table>

that one can then $objResponse->assign('i_content', 'innerHTML', $smarty-fetch('admin_content.tpl')) in the xajax response function (after $smarty->assigning stuff).

8 (edited by IceFrog 2008-12-10 3:50:58 PM)

Re: Help with smarty plz

Smarty and Xajax impossible things for me sad

9 (edited by IceFrog 2008-12-11 2:31:43 AM)

Re: Help with smarty plz

i do it!!!!!! (thanks all) HELLO FROM RUSSIA smile))

This example for beginners:

index.php
[code]<?
    include("index.common.php");
    $smarty->assign("xajax_javascript",$xajax->getJavascript('/xajax/'));
    $smarty->display("index.tpl");
   
?>[/code]
index.common.php
[code]<?
require_once ("xajax/xajax_core/xajax.inc.php");
require_once("libs/Smarty.class.php");

$smarty = new Smarty;


// put in your server configuration here
$smarty->cache="cache";
$smarty->compile_dir="compile";
$smarty->template_dir="templates";

// turn caching off for dynamic pages and/or development
$smarty->force_compile=1;
$smarty->caching=0;

$xajax = new xajax("index.server.php");
$xajax->registerFunction("datafun");

?>[/code]
index.server.php
[code]<?

include("index.common.php");

function datafun($aFormValues) {
    // new xajaxResponse Object
    $objResponse = new xajaxResponse();
    $objResponse->setCharacterEncoding('utf-8');
   
    //clear error fields
    $objResponse->assign("msg","innerHTML","