Как вставить php в html шаблон?

Discussion in 'PHP' started by -=4ekiSt=-, 17 Oct 2012.

  1. -=4ekiSt=-

    -=4ekiSt=- Member

    Joined:
    17 Nov 2011
    Messages:
    39
    Likes Received:
    11
    Reputations:
    3
    Здравствуйте.
    Есть скрипт index.php
    PHP:
    <?php
    header
    ("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT");
    include(
    "config.php");
    include(
    "modules/login.php");

    #### Запуск сессии корзины покупателя
    session_start();
    session_register("ses_tovar");
    session_register("ses_kolvo");

    include(
    "modules/struct.php");
    $template stripslashes($template);
        
    #### Форма поиска по сайту
    if (strstr($template"{SEARCH_FORM}")) {
        
    $searchForm file_get_contents("files/temp/searchForm.html");
        
    $template str_replace("{SEARCH_FORM}"$searchForm$template);
    }

    #### Корзина покупателя
    include("modules/cart.php");

    #### Контент
    $tatel mysql_query("select `element_id`, `type` from `".$prefix."blocks` where `page_id`='$page_id' ORDER BY `turn_id`");
    while(
    $rowel mysql_fetch_array($tatel)) {
        
        if (
    $rowel['type']==AND !isset($newid) AND !isset($tshopid)) { #### Карта сайта
            
    include("modules/map.php");
        }

        if (
    $rowel['type']==AND !isset($newid) AND !isset($tshopid)) { #### Тескстовый блок/статья
            
    $tat mysql_query("select `datatext` from `".$prefix."article` where `id`='".$rowel['element_id']."' LIMIT 1");
            
    $row mysql_fetch_array($tat);    
            
    $page_content.="<table class='blockmargin'><tr><td>".stripslashes($row['datatext'])."</td></tr></table>";
        }

        if (
    $rowel['type']==AND !isset($newid) AND !isset($tshopid)) { #### Фотоальбом
            
    $tat mysql_query("select * from `".$prefix."photoalbum` where `id`='".$rowel['element_id']."' LIMIT 1");
            
    $row mysql_fetch_array($tat);    
            include(
    "modules/photoalbum.php");
        }
        
        if (
    $rowel['type']==AND !isset($newid) AND !isset($tshopid)) { #### Веб-форма
            
    $tat mysql_query("select * from `".$prefix."webforms` where `id`='".$rowel['element_id']."' LIMIT 1");
            
    $row mysql_fetch_array($tat);    
            include(
    "modules/webforms.php");
        }
        
        if (
    $rowel['type']==AND !isset($newid) AND !isset($tshopid)) { #### Гостевая книга
            
    $tat mysql_query("select * from `".$prefix."blogs` where `id`='".$rowel['element_id']."' LIMIT 1");
            
    $row mysql_fetch_array($tat);    
            include(
    "modules/blogs.php");
            
    $true_ajax 1;
        }
        
        if (
    $rowel['type']==AND !isset($tshopid)) { #### Новостная лента
            
    $tat mysql_query("select * from `".$prefix."newtapes` where `id`='".$rowel['element_id']."' LIMIT 1");
            
    $row mysql_fetch_array($tat);    
            include(
    "modules/news.php");
        }
        
        if (
    $rowel['type']==AND !isset($newid)) { #### Интернет магазин
            
    $tat mysql_query("select * from `".$prefix."shop_cats` where `id`='".$rowel['element_id']."' LIMIT 1");
            
    $row mysql_fetch_array($tat);    
            include(
    "modules/shop.php");
            
    $true_ajax 1;
            
    $this_pgshop 1;
        }
        
        if (
    $rowel['type']==AND !isset($newid) AND !isset($tshopid)) { #### Опрос
            
    $tat mysql_query("select `id` from `".$prefix."opros` where `id`='".$rowel['element_id']."' LIMIT 1");
            
    $row mysql_fetch_array($tat);    
            
    $page_content.="{OPROS_".$row['id']."}";
            
    $true_ajax 1;
        }

        if (
    $rowel['type']==AND !isset($newid) AND !isset($tshopid)) { #### Форумы    
            
    include("modules/forums.php");
        }
        
        if (
    $rowel['type']==AND !isset($newid) AND !isset($tshopid)) { #### Тестирование
            
    $tat mysql_query("select * from `".$prefix."tests` where `id`='".$rowel['element_id']."' LIMIT 1");
            
    $row mysql_fetch_array($tat);    
            include(
    "modules/tests.php");
        }


    #### Закрытая страница
    if ($accessPage==AND !strstr($accessGroup"#".$group_global."#") OR $accessPage==AND $user_global==0)  {
        
    $page_content stripslashes($loginFormContent);
    }

    #### Новости
    if (strstr($template,"{LS_NEWS}")) {
        
    $tat2 mysql_query("select * from `".$prefix."news` where `tape_id`='1' AND `newopen`='1' ORDER BY `date_id` DESC LIMIT 3");
        while(
    $row2 mysql_fetch_array($tat2)) {
            
            
    $tatsb mysql_query("select `anons` from `".$prefix."news_templates` where `id`='".$row['temp_id']."' ORDER BY `prime` LIMIT 1");
    if ((
    $rowsb mysql_fetch_array($tatsb))==false) {
        
    $tatsb mysql_query("select `anons` from `".$prefix."news_templates` where 1 ORDER BY `prime` DESC LIMIT 1");
        
    $rowsb mysql_fetch_array($tatsb);
    }        
            if (
    $row2['photo']=="") {
            
    $newimg "images/tex/spacer.gif"$newimgbig "images/tex/spacer.gif";
        } else {
            
    $newimg $row2['photo']."_small.jpg"$newimgbig $row2['photo'].".jpg";
        }
        
    $new_shablon str_replace("{NEWS_HEADER}"$row2['header'], $rowsb['anons']);
    $new_shablon str_replace("{NEWS_HREF}""NovostiNWID".$row2['id'].".html"$new_shablon);
    $new_shablon str_replace("{NEWS_DATE}"$row2['cre_date'], $new_shablon);
    $new_shablon str_replace("{NEWS_IMG}"$newimg$new_shablon);
    $new_shablon str_replace("{NEWS_BIGIMG}"$newimgbig$new_shablon);
    $new_shablon str_replace("{NEWS_ANONS}"nl2br($row2['anons']), $new_shablon);
    $new_shablon str_replace("{NEWS_FULL}"$row2['datatext'], $new_shablon);
    $new_shablon str_replace("{NEWS_TAPE}"$row['name'], $new_shablon);
    $new_shablon str_replace("{NEW_CLASS}""ans"$new_shablon);

    $all_newspage.=stripslashes($new_shablon);
        }
        
    $template str_replace("{LS_NEWS}",$all_newspage,$template);
    }

    #### Подмены в шаблоне
    $template str_replace("{PAGE_NAME}"stripslashes($page_fullname), $template);
    $template str_replace("{PAGE_TITLE}"stripslashes($page_title), $template);
    $template str_replace("{PAGE_METAKEY}"stripslashes($page_metakey), $template);
    $template str_replace("{PAGE_METADESC}"stripslashes($page_metadesc), $template);

    $template str_replace("{CONTENT}"$page_content."{AJAX}"$template);

    #### Переменная опроса
    if (strstr($template"{OPROS_")) {
        include(
    "modules/opros.php");
    }

    #### Поиск по магазину
    if ($this_pgshop==AND $r_searchn!=1) {
        
    $r_search file_get_contents("modules/scripts/shopsearch.html");
    }
    $template str_replace("{R_SEARCH}"$r_search$template);

    #### Ошибка 404
    if ($error404==1) {
        include(
    "files/temp/404.html");
        exit;
    }

    if (
    $rss==1) { $template $rss_content; }

    #### Элементы и Баннеры сайта в шаблоне
    if (strstr($template"{BANNER_")) {
        
    $tat mysql_query("select `datatext`, `tag` from `".$prefix."banners` where 1 ORDER BY `id`");
        while(
    $row mysql_fetch_array($tat)) {
            if (
    strstr($row['datatext'], "<?PHPCOD")) {
                
    $pathcod "http://".$HTTP_HOST."/modules/phpcod.php?tag=".$row['tag'];
                
    $opencod file_get_contents($pathcod);
                
    $template str_replace("{BANNER_".$row['tag']."}"nl2br(stripslashes($opencod)), $template);
            } else {
                
    $template str_replace("{BANNER_".$row['tag']."}"stripslashes($row['datatext']), $template);
            }
        }
    }

    #### Если нужен AJAX
    if ($true_ajax==AND $rss!=1) {
        
    $ajaxCod file_get_contents("modules/scripts/goajax.html");
    }
    $template str_replace("{AJAX}",$ajaxCod,$template);

    echo 
    $template;
    exit;
    ?>
    и есть шаблон к главной странице /files/temp/PGTemplate_1.html
    HTML:
    <html>
    <head>
    <title>{PAGE_TITLE}</title>
    <meta name="Description" content="{PAGE_METADESC}"> 
    <meta name="Keywords" content="{PAGE_METAKEY}">
    <link href="style.css" rel="stylesheet" type="text/css">
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
    </head>
    <body>
    
    <table width="100%" height="100%" cellpadding="0" cellspacing="0">
    <tr><td>
    
    <table width="100%" cellspacing="0" cellpadding="0">
    <tr><td width="260" height="241" background="images/bzs_01.jpg"><img src="images/tex/spacer.gif" width="260" height="1"></td>
    <td>
    
    <table width="100%" cellspacing="0" cellpadding="0">
    <tr><td width="631" height="96" background="images/bzs_02.jpg"><a href="/"><img src="images/bzs_02.jpg" alt="Базис-Сервис" width="631" height="96" border="0"></a></td>
    <td background="images/bzs_03.jpg">&nbsp;</td></tr>
    </table>
    
    <table width="100%" cellspacing="0" cellpadding="0">
    <tr><td width="267" height="80" background="images/bzs_05.jpg">&nbsp;</td>
    <td background="images/bzs_06.jpg"> </td>
    <td width="195" height="80" background="images/bzs_07.jpg">&nbsp;</td></tr>
    </table>
    
    <table width="100%" cellspacing="0" cellpadding="0">
    <tr><td width="5" height="65" background="images/bzs_08.jpg">&nbsp;</td>
    {MENU_MAIN}
    <td width="22" height="65" background="images/bzs_16.jpg">&nbsp;</td></tr>
    </table>
    
    </td></tr>
    </table>
    
    </td></tr>
    <tr><td height="100%">
    
    <table width="100%" height="100%" cellpadding="0" cellspacing="0">
    <tr><td width="249">
    
    <table width="100%" height="100%" cellpadding="0" cellspacing="0">
    <tr><td>{MENU_GR}</td></tr>
    <tr><td height="131" background="images/bzs_35.jpg">&nbsp;</td></tr>
    <tr><td height="100%" background="images/bzs_37.jpg">&nbsp;</td></tr>
    </table>
    
    </td>
    <td valign="top" background="images/bzs_18.jpg" class="xrep" style="padding:15px;"><h1>{PAGE_NAME}</h1>{CONTENT}</td></tr>
    </table>
    
    </td></tr>
    </table>
    
    </td></tr>
    <tr><td>
    
    <table width="100%" cellspacing="0" cellpadding="0">
    <tr><td width="70" height="117" background="images/bzs_39.jpg">&nbsp;</td>
    <td background="images/bzs_40.jpg" style="padding:10px;">
    
    <table width=100%>
    <tr><td width=250>{BANNER_RAN}</td>
    <td align=right>{BANNER_RAN2}</td></tr>
    </table>
    
    </td></tr>
    </table>
    
    </td></tr>
    </table>
    
    <div style="position:absolute;top:15px;right:15px;"><a href="/"><img src="images/home.gif" alt="На главную" border="0" style="margin-right:25px;"></a><a href="mailto:{BANNER_MAIL}"><img src="images/mail.gif" alt="Написать письмо" border="0" style="margin-right:25px;"></a><a href="?printPage=1"><img src="images/print.gif" alt="Распечатать" border="0" style="margin-right:25px;"></a></div>
    
    <div style="position:absolute;top:110px;left:600px;">{BANNER_PHONE}</div>
    
    </body>
    </html>
    Подскажите как вставить в этот шаблон php код или
    как заставить php исполнятся в этом шаблоне?
    Буду очень благодарен!
     
  2. mironich

    mironich Elder - Старейшина

    Joined:
    27 Feb 2011
    Messages:
    733
    Likes Received:
    73
    Reputations:
    19
    -=4ekiSt=-, у тебя там пример есть.
    Потом выводишь получившейся код.
     
    1 person likes this.
  3. -=4ekiSt=-

    -=4ekiSt=- Member

    Joined:
    17 Nov 2011
    Messages:
    39
    Likes Received:
    11
    Reputations:
    3
    Можно подробнее? Я ничего в этом примере не понял, вот чтобы мне проинклудить файл 1.php в шаблон что нужно в index.php прописать?
     
  4. necroleptick

    necroleptick Member

    Joined:
    3 Jan 2011
    Messages:
    105
    Likes Received:
    11
    Reputations:
    0
    Шаблоны уже не в моде, ща на нативном пыхе хуярят
     
  5. GAiN

    GAiN Elder - Старейшина

    Joined:
    2 Apr 2011
    Messages:
    2,550
    Likes Received:
    172
    Reputations:
    99
    PHP:
    <?php
     
    include_once("index.php");
     
    ?>
    а дальше настраиваем в .htaccess и он обрабатывать сможет php в html файле
    PHP:
    RewriteEngine on
    RewriteRule 
    ^(.*)\.html $1\.php
    http://www.umade.ru/log/2004/07/use-php-in-html-pages/
     
  6. VY_CMa

    VY_CMa Green member

    Joined:
    6 Jan 2012
    Messages:
    917
    Likes Received:
    492
    Reputations:
    724
    PHP:
    ob_start ("callback");

    function 
    callback($buffer){
    $buffer str_replace('{{title}}',$title,$buffer);
    return 
    $buffer;

    ob_end_flush(); 
     
    _________________________
    1 person likes this.
  7. foxinvisible

    foxinvisible Banned

    Joined:
    14 Oct 2012
    Messages:
    48
    Likes Received:
    13
    Reputations:
    10
    А почему нельзя просто заинклудить ?
     
  8. Billar

    Billar Banned

    Joined:
    9 Jul 2012
    Messages:
    15
    Likes Received:
    5
    Reputations:
    5
    Потому-что HTML.
     
  9. barnaki

    barnaki Elder - Старейшина

    Joined:
    2 Nov 2008
    Messages:
    676
    Likes Received:
    140
    Reputations:
    4
    AddType application/x-httpd-php .php .htm .html
    AddHandler x-httpd-php .php .htm .html
    в конфиге апача или htaccess
    и можно инклюдить.
     
    #9 barnaki, 23 Oct 2012
    Last edited: 23 Oct 2012
  10. b3

    b3 Banned

    Joined:
    5 Dec 2004
    Messages:
    2,170
    Likes Received:
    1,155
    Reputations:
    202
    шаблонизатор берет шаблон и html файла функцией file_get_contents и отдает обработаный шаблон через echo
    Ну а теперь подумайте зачем
    Читайте внимательно пост #2