2011-05-23 56 views
1

我想加載smarty的TPL與jquery一個div內部部分,我得到:負載smarty的TPL與jquery

JS:

function load_new_partial() { 

jQuery("#ajax_zone").fadeIn('slow').load('/views/partials/partil.tpl'); 

}

TPL呼叫者內:

<a href="#" onclick="load_new_partial();">{$language_array.lang_new_campaign}</a> 
<div id="ajax_zone">initial div yadadada</div> 

叫做tpl:

{$user_name_smarty}{$account_array} 

我必須顯示在點擊partial.tpl沒有問題,問題是,加載partial.tpl似乎並沒有得到任何我已經分配給智者的變量。

什麼我做錯了什麼?我如何加載部分,使其有機會獲得已經建立的Smarty的變量?

$smarty->assign('user_name_smarty', $user_name_smarty); 

在此先感謝。

回答

5

如果您加載它這樣,你加載它作爲一個文本文件。您希望它請求一個使用該TPL作爲模板的PHP文件。

通常情況下:

  • 請求PHP
  • 解析TPL(做的東西跟變量)
  • 將HTML
  • 顯示在瀏覽器現在

  • 請求PHP
  • 解析TPL(如果PHP使用TPL)
  • 將HTML
  • 顯示在瀏覽器
  • 瀏覽器解析JQuery的
  • 負載額外TPL(不會被解析!)

正如你所看到的,有沒有「解析TPL」 STAP加載額外的第三方物流

你可以要求使用Ajax的PHP文件後,並補充說,r的結果equest到你的div。

+0

哦,聽起來像***的痛苦...有時我愛smarty,有時我只是希望它...更聰明..!該死的......謝謝兄弟。 – 2011-05-23 20:27:13

+1

嗯,請記住,這裏的問題並不聰明,但它是serverside(php,smarty)和clientside(html,javascript/jquery)之間的「衝突」。祝你好運! – Nanne 2011-05-23 20:32:04

+0

感謝他......現在轉到別的東西......義務「衝突」管:http://www.youtube.com/watch?v=GqH21LEmfbQ&feature=fvst:P謝謝 – 2011-05-23 22:09:00

2

檢查它...它是可以做到這一點。

首先在服務器端:

if ($_GET['action'] == "educational_resources"){ 

    if(isset($_GET['kind'])){ 
     header("Content-Type: text/xml"); 
     header("Cache-Control: no-cache"); 
     header("Pragma: no-cache"); 
     $smarty->compile_check = true; 
     $smarty->force_compile = 1; 
     $kind = $_GET['kind']; 
     $reply = true; 
     if($kind == "get_classroom_resources"){ 
      $class_id = $_SESSION['user']['class_id']; 
      $classData = $curriculum->get_class_data($class_id); 
      $units = $curriculum->get_units(); 
      $chapters = $curriculum->get_modules(); 
      $smarty->assignGlobal('classData', $classData); 
      $smarty->assignGlobal('units', $units); 
      $smarty->assignGlobal('chapters', $chapters); 
      $smarty->display('classroom.tpl'); 
     } 
    } 

    if(!isset($reply)){ 
     $file[1] = "educational_resources.tpl"; 
     $file[2] = "footer.tpl"; 
    } 
} 

設置我的行動處理器包含了所有我的Ajax請求處理程序了。如果「$ _GET [‘種’]設置我知道這是一個Ajax請求。因此,如果$種設置,設置頭配置,並告訴智者強制編譯(這是確保了返回什麼解析HTML)。

下使你的TPL文件。

<h1>Period: {$classData.period} - {$classData.description}</h1> 
<h4>{$classData.name} - {$classData.city}, {$classData.state}</h4> 
<p>The content area below shall povide a convienient way to browse all the curriculum, and provide a detailed view that summarizes the student's performance 
as he/she progresses through each chapter.</p> 
<ul> 
    {foreach from=$units name=units key=k item=i} 
     <li> 
      Unit {$smarty.foreach.units.iteration} - {$i.title} 
      <ul> 
       {foreach from=$chapters name=chapters key=c item=v} 
        {if $i.id == $v.unit_id} 
         <li>Chapter {$smarty.foreach.chapters.iteration} - {$v.descriptor}</li> 
        {/if} 
       {/foreach} 
      </ul> 
     </li> 
    {/foreach} 
</ul> 

最後,在jQuery的事件處理程序,使Ajax調用(您想返回的HTML):

$('#classroom').css({'background-image': 'url("./images/classroom.png")', 'margin': '0 .5em 0 0'}) 
       .live('click', function(event){ 
        //window.location="?action=classroom"; 
        $('#stupid_wrapper').css({'width': '106px', 'overflow': 'auto', 'float': 'left', 'margin': '0'}); 
        $('#stupid_table').css({'padding': '1em', 'width': '80px'}); 
        $('#classroom').css({'margin': '0 0 .5em 0', 'width': '80px', 'height': '54px', 'background-image': 'url("./images/classroom_icon.png")'}); 
        $('#project').css({'margin': '0 0 .5em 0', 'width': '80px', 'height': '54px', 'background-image': 'url("./images/project_icon.png")'}); 
        $('#reportcard').css({'width': '80px', 'height': '54px', 'background-image': 'url("./images/reportcard_icon.png")'}); 
        $('.cell').css({'float': 'none'}); 

        var classroomHTML = '<div id="icon_panel"></div>'; 
        $(classroomHTML).appendTo('#content'); 
        $('#icon_panel').css({'float': 'left', 'width': '75%', 'margin': '0 0 0 1em', 'background-color': '#f6f6f6', 'border': '1px solid #cdcdcd', 'padding': '2em', 'border-radius': '1em', '-moz-border-radius': '1em'}) 
        getClassroomResources(); 
       }); 

function getClassroomResources(){ 
        $.ajax({ 
         type: 'GET', 
         url: '?action=educational_resources&kind=get_classroom_resources', 
         dataType: 'html', 
         beforeSend: function(xhr){ 
          xhr.setRequestHeader("Ajax-Request", "true"); 
         }, 
         success: function(reply){ 
          $('#icon_panel').html(reply); 
         } 
        }); 
        return false; 
       } 

感謝IBM爲這個! http://www.ibm.com/developerworks/opensource/library/wa-aj-smarty/index.html?ca=drs-