2015-10-07 28 views
13

我面臨的問題是,我不確定如何在沒有框架或模板引擎的情況下開發。我開始編寫這種方式,現在我想去基礎知識。退出Smarty手動執行它

我曾經使用這個MVC模式,使用Codeigniter和Smarty作爲模板引擎。我現在想要做的是在沒有提到這兩種工具的情況下使用原始php。

我不知道如何「複製」Smarty的「塊」和「擴展」的概念。

我用來定義一個base.tpl文件,它具有html頭部,只有body標籤,以及基本的css和js文件(總是用在網站的每個頁面中),如下所示:(snippet )

<!DOCTYPE html> 
<head> 
<meta charset="utf-8" /> 
<title>Dashboard</title> 
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" name="viewport" /> 
<meta content="" name="description" /> 
<meta content="" name="author" /> 

<!-- ================== BEGIN BASE CSS STYLE ================== --> 
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"> 
<link href="{site_url()}assets/css/animate.min.css" rel="stylesheet" /> 
<!-- ================== END BASE CSS STYLE ================== --> 

<!-- ================== BEGIN PAGE LEVEL CSS STYLE ================== --> 
{block name='custom_css'}{/block} 
<!-- ================== END PAGE LEVEL CSS STYLE ================== --> 

<!-- ================== BEGIN BASE JS ================== --> 
<script src="{site_url()}assets/plugins/pace/pace.min.js"></script> 
<!-- ================== END BASE JS ================== --> 
</head> 
<body> 
    <div id="page-container" class="fade page-sidebar-fixed page-header-fixed"> 
    <div id="header" class="header navbar navbar-default navbar-fixed-top"> 
     <div class="container-fluid"> 
      {include file='base/header.tpl'} 
     </div> 
    </div> 
    <!-- BEGIN PAGE --> 
    <div class="page-content"> 
     <!-- BEGIN PAGE CONTAINER--> 
     <div class="container-fluid"> 
      <!-- BEGIN PAGE HEADER--> 
      <div class="row-fluid"> 
       <div class="span12">       
        <!-- BEGIN PAGE TITLE & BREADCRUMB--> 
        {include file='admin/base/breadcrumb.tpl'} 
        <!-- END PAGE TITLE & BREADCRUMB--> 
       </div> 
      </div> 
      <!-- END PAGE HEADER--> 
      {block name='content'}{/block} 
     </div> 
     <!-- END PAGE CONTAINER-->  
    </div> 
    <!-- END PAGE --> 

,然後當我需要調用這個base.tpl我這樣做:

{extends file='base/base.tpl'} 

{block name='custom_css} 
    <link href="{site_url()}assets/css/pages/blog.css" rel="stylesheet" type="text/css"/> 
{/block} 

{block name='content'} 
    <div class="row"> 
     <div class="col-md-3 col-sm-6"> 
     <div class="widget widget-stats bg-green"> 
     <div class="stats-icon stats-icon-lg"><i class="fa fa-globe fa-fw"></i></div> 
     <div class="stats-title">TODAY'S VISITS</div> 
     <div class="stats-number">7,842,900</div> 
     <div class="stats-progress progress"> 
      <div class="progress-bar" style="width: 70.1%;"></div> 
     </div> 
     <div class="stats-desc">Better than last week (70.1%)</div> 
     </div> 
    </div> 

我一直在尋找,但我affraid我錯過了合適的詞來搜索,因爲我沒有找到答案。

我想請指導!

+0

這只是一些*如果這個回聲*包裝 – 2015-10-07 00:54:38

+0

對不起,我沒有跟着你 – Limon

+0

所有上面所做的(取第一個塊)是根據標準'name ='custom_css''確定在頁面上回顯什麼,所以它不超過'if($ name =='custom_css'){echo'some css here';}' – 2015-10-07 00:59:58

回答

0

我不知道它有多聰明,實際上從來沒有使用過模板引擎。但也許這可能是如何完成的。

說我們有:

index.php 
page.php 
template.php 

當我們去到index.php它可以啓動和輸出緩衝器包括page.php文件。在include之後,捕獲輸出緩衝區並使用一些正則表達式來匹配和找到其中的塊,並將它們放入變量中。

現在對template.php也做同樣的事情,並找到其中的所有塊,並用page.php中的塊替換塊。

我其實不認爲這就是模板引擎如何做到這一點。但這是一種可能的方式。

1

另一種方式可能是做這樣的事情。我覺得這可能更接近模板引擎的結果,但是使用{block}語法代替。

的index.php

<?php 
$blocks = array(); 
function add_block($name, $callback){ 
    global $blocks; 
    ob_start(); 
    $callback(); 
    $output = ob_get_flush(); 
    $blocks[$name] = $output; 
} 

function get_block($name){ 
    global $blocks; 
    if(!empty($blocks[$name])){ 
     return $blocks[$name]; 
    } 
    return ''; 
} 

//stop any errors from being output. 
ob_start(); 

//include the page code 
include 'page.php'; 
$cleanup = ob_end_clean(); 

//now output the template 
include 'template.php'; 

page.php文件

<?php 

add_block('head', function(){ 
    ?><script type="text/javascript">/* Some JS */</script><?php 
}); 

add_block('body', function(){ 
    ?><p>Some body text goes here</p><?php 
}); 

模板。PHP

<html> 
    <head> 
     <title>Site Title</title> 
     <?php echo get_block('head'); ?> 
    </head> 
    <body> 
     <?php echo get_block('body'); ?> 
    </body> 
    <?php echo get_block('after_body'); ?> 
</html> 
0

一個非常微小的自制,模板引擎 基於正則表達式替換使用陣列鉤 「手工」解析模板

(但我承認,smarty的是多個官能) 要任何問題,請隨時在這裏回答我

正則表達式匹配整個文件的術語像{term-_}, 並將其替換爲將在渲染時執行的php條件。

如果一個標記」不$瓦爾發現,它將通過一個空字符串

引擎

function parse($vars, $tpl) 
{ 
    return preg_replace 
    (
     '#\{([a-z0-9\-_]*?)\}#Ssie', 
     '((isset($vars[\'\1\'])) 
      ? $vars[\'\1\'] 
      : \'\' 
     );', 
     file_get_contents(TEMPLATE_DIR.$tpl) 
    ); 
} 

指數的一部分

 <html> 
      <head>...</head> 
      {body} 
    </html> 

身體

的部分替代
<body> 
     <div class='ui'>{username}</div> 
    </body> 

用法

<?php 
    // include engine function 
    define("TEMPLATE_DIR", __DIR__."templates/"); 
    require_once("library/parse.php"); 

    // just init $vars on top of index 
    $vars = []; 

    // and access and fill it anywhere 
    $vars = ["username" => $_SESSION["user"]]; 

    // prepare the body including previous $vars declarations 
    $vars["body"] = parse($vars, 'body'); 

    echo parse($vars, 'index'); 

輸出

<html> 
     <head>...</head> 
     <body> 
      <div class='ui'>Stack Overflow :)</div> 
     </body> 
    </html> 

您可以通過不斷改進,防止雙包標記{{}}以上,或放置調試跟蹤...

此加入啓動引擎以防止包含對象括號的模板可能被錯誤地解釋爲模板標記:

$out = file_get_contents(TEMPLATE_DIR.$tpl); 
$out = str_replace("{{}}", "{}", $out); 

要使用常量,您可以使用執行像:

$empty = (DEBUG) ? "_EMPTY_" : ""; 
    return preg_replace 
    (
     '#\{([a-z0-9\-_]*?)\}#Ssie', 
     '((isset($vars[\'\1\'])) 
      ? $vars[\'\1\'] 
      : (defined(\'_\'.strtoupper(\'\1\').\'_\') 
       ? constant(\'_\'.strtoupper(\'\1\').\'_\') 
       : $empty 
      ) 
     );', 
     $out 
    ); 

注:

__DIR__ 

在我的代碼使用的是有效的PHP> = 5.3.0嘗試 但你可以使用

dirname(__FILE__) 

對於PHP < 5.3.0試試