2013-08-21 20 views
0

我使用以下SQL獲取內容位置。格式化與識別結果

$leftBlock ='l'; 
$rightBlock ='r'; 
$topBlock = 'c'; 
$bottomBlock = 'd'; 

$blockposition = array(); 

$result2 = $db->sql_query("SELECT bposition FROM {$prefix}_blocks_manager WHERE module_title ='$name'"); 
while($row2 = $db->sql_fetchrow($result2)) { 
$blockposition[] = $row2['bposition']; 

} 

$blockposition2 = array_unique($blockposition);//remove duplicates becouse db output can be like:lllrrrd 
if (in_array($leftBlock, $blockposition2) AND in_array($rightBlock, $blockposition2)) { 
    $mytestresult = "We go use a template with left and right blocks"; 
} 
if (in_array($leftBlock, $blockposition2) AND !in_array($rightBlock, $blockposition2)) { 
    $mytestresult = "We go use a template with just left blocks"; 
} 
if (!in_array($leftBlock, $blockposition2) AND in_array($rightBlock, $blockposition2)) { 
    $mytestresult = "We go use a template with just right blocks"; 
} 
if (!in_array($leftBlock, $blockposition2) AND !in_array($rightBlock, $blockposition2)) { 
    $mytestresult = "We go use a template with just content"; 
} 

$name代表我正在查看的網頁。

$mytestresult會去告訴嫩枝加載特定的模板:

$template = $twig->loadTemplate('mytestresult_file.phtml');

但對於每個模板上面,我可以創造內容,增加了現有內容的上方或下方的新內容。

$topBlock$bottomBlock

裏面我的劇本我加載所有的左/右/上/下塊的功能blocks($side) { .....

blocks('l') blocks('r') blocks('c') blocks('d')

我需要一種方法來告訴TWIG如何渲染,如果上述4個模板中的一個還包含$topBlocks和/或$bottomBlock的內容。

// render template 
    echo $template->render(array (
       'title' => $title1, 
       'metaDesc' => $metaDesc, 
       'metaKeywords' => $metaKeywords, 
       'navContent' => $navContent, 
       'leftContent' => $leftContent, 
       'defaultContent' => $module_content, 
       'rightContent' => $rightContent, 
       'footerContent' => $copyright 
)); 

但樹枝呈現在這種情況下也rightContent的HTML,即使心不是任何內容:

默認情況下並沒有在上面我呈現這樣加載任何的4個模板。

+0

此:http://twig.sensiolabs.org/doc/recipes.html? – hakre

+0

對不起,這不是我的意思。我只是想告訴TWIG它應該加載什麼樣的模板。我不想告訴TWIG。 – HenryW

+0

現在在你的代碼中,你根本不會加載任何*模板。你真的知道你將如何加載一個特定的模板? – hakre

回答

0

我由開始創建4個默認模板解決我的問題。

  1. 默認的內容只有
  2. 左內容與默認內容
  3. 左內容默認的內容和正確的內容
  4. 默認內容右內容

我的方式解決了這個問題頂部和底部的內容是我簡單地添加相關瓦爾 到默認的內容,如:

$moduleContent = $topContent . $module_content . $bottomContent; //self explaining no?

,我想分享我的代碼作爲一個例子,所以可能可以被他人利用:

//generate template variables 
$title1 = "This site is powered by smarty"; 
$metaDesc = "this is the coolest site on the net"; 
$metaKeywords = "cool,site,internet,number one,ever,created with smarty"; 

$module_content = $getContent['module_content']; 

$leftContent = blocks2('left'); 
$rightContent = blocks2('right'); 
$topContent = blocks2('c'); 
$bottomContent = blocks2('d'); 
$footerContent = $copyright; 

//name the blockpositions (can be extended but requires hardcoding: $sliderBlock $headerBlock, $addsBlock etc etc) 
$leftBlock ='l'; 
$rightBlock ='r'; 
$topBlock = 'c'; 
$bottomBlock = 'd'; 

$blockposition = array(); 

$result2 = $db->sql_query("SELECT bposition FROM {$prefix}_blocks_manager WHERE module_title ='$name'"); 
while($row2 = $db->sql_fetchrow($result2)) { 
$blockposition[] = $row2['bposition']; 
} 

//remove duplicates becouse db output can be like:lllrrrd 
$blockposition = array_unique($blockposition); 

//////////////////////////////////////////////////////////////// 
///We go check if there are top or bottom blocks are active//// 
////////////////////////////////////////////////////////////// 

//We go add top blocks before module content and bottom blocks after module content 
if (in_array($topBlock, $blockposition) AND in_array($bottomBlock, $blockposition)) { 
$moduleContent = $topContent . $module_content . $bottomContent; 
} 
//We go add top blocks before module content 
if (in_array($topBlock, $blockposition) AND !in_array($bottomBlock, $blockposition)) { 
$moduleContent = $topContent . $module_content; 
} 
//We go add bottom blocks after module content 
if (!in_array($topBlock, $blockposition) AND in_array($bottomBlock, $blockposition)) { 
$moduleContent = $module_content . $bottomContent; 
} 

///////////////////////////////////////////////////////////////////////////////////// 
/// We go check if we need left blocks and/or right blocks or no blocks at all. //// 
/////////////////////////////////////////////////////////////////////////////////// 

//We go use a template with left and right blocks 
if (in_array($leftBlock, $blockposition) AND in_array($rightBlock, $blockposition)) { 
echo $twig->render('left_right.phtml', array('title' => $title1,'metaDesc' => $metaDesc,'metaKeywords' => $metaKeywords,'navContent' => $navContent,'leftContent' => $leftContent,'defaultContent' => $moduleContent,'rightContent' => $rightContent,'footerContent' => $copyright)); 
} 
//We go use a template with just left blocks 
if (in_array($leftBlock, $blockposition) AND !in_array($rightBlock, $blockposition)) { 
echo $twig->render('left.phtml', array('title' => $title1,'metaDesc' => $metaDesc,'metaKeywords' => $metaKeywords,'navContent' => $navContent,'leftContent' => $leftContent,'defaultContent' => $moduleContent,'footerContent' => $copyright)); 
} 
//We go use a template with just right blocks 
if (!in_array($leftBlock, $blockposition) AND in_array($rightBlock, $blockposition)) { 
echo $twig->render('right.phtml', array('title' => $title1,'metaDesc' => $metaDesc,'metaKeywords' => $metaKeywords,'navContent' => $navContent,'defaultContent' => $moduleContent,'rightContent' => $rightContent,'footerContent' => $copyright)); 
} 
//We go use a template with just content 
if (!in_array($leftBlock, $blockposition) AND !in_array($rightBlock, $blockposition)) { 
echo $twig->render('content.phtml', array('title' => $title1,'metaDesc' => $metaDesc,'metaKeywords' => $metaKeywords,'navContent' => $navContent,'defaultContent' => $moduleContent,'footerContent' => $copyright)); 
}