努力格式化和识别结果

时间:2013-08-21 21:36:53

标签: php templates mysqli twig

我使用以下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')

如果以上4个模板中的一个还包含$topBlocks和/或$bottomBlock内容,我需要一种告诉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渲染也是右边内容的html,即使没有任何内容。

1 个答案:

答案 0 :(得分:0)

我通过开始创建4个默认模板解决了我的问题。

  1. 仅限默认内容
  2. 保留默认内容的内容
  3. 保留默认内容和正确内容的内容
  4. 内容正确的默认内容
  5. 我解决顶部和底部内容问题的方法是我只是添加了相关的变量 默认内容,如:

    $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));
    }