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将告诉 Twig 加载特定的模板:

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

但是对于上面的每个模板,我可以创建在现有内容之上或之下添加新内容的内容。

$topBlock$bottomBlock

在我的脚本中,我将所有左/右/上/下块加载到一个函数中blocks($side) { .....

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

$topBlocks如果上面的 4 个模板之一还包含和/或$bottomBlocks 内容,我需要一种方法来告诉 TWIG 如何渲染。

默认情况下,不加载上面的 4 个模板中的任何一个,我渲染如下:

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

但在这种情况下,即使没有任何内容,twig 也会渲染 rightContent 的 html。

4

1 回答 1

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));
}
于 2013-08-24T22:42:10.060 回答