php - 退出 Smarty 手动完成

标签 php html include smarty extends

我面临的问题是我不确定如何在没有框架或模板引擎的情况下进行开发。我开始以这种方式编码,现在我想学习基础知识。

我曾经使用这个 MVC 架构,使用 Codeigniter 和 Smarty 作为模板引擎。我现在想做的是在没有提到这两种工具的情况下使用原始 php。

我不知道如何“复制”Smarty的“阻塞”和“扩展”的概念。

我曾经定义一个 base.tpl 文件,它有 html head,只有 body 标签,以及基本的 css 和 js 文件(那些总是在网站的每个页面中使用的文件),就像这样:(片段)

 <!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>

我一直在搜索,但我担心我找不到正确的搜索词,因为我找不到答案。

我想得到指导!

最佳答案

另一种方法可能是做这样的事情。我觉得这可能更接近于模板引擎的最终结果,但使用了 {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
});

template.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>

关于php - 退出 Smarty 手动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32982048/

相关文章:

html - 将 div 的高度设置为其父项的高度

php - 如何将 PHP 应用程序限制在它们自己的目录和它们自己的 php.ini 中?

jsp - 在包含文件中检索文件的相对 url 路径,jsp

php - 在 websocket 握手时使用 session 数据

没有 Web 表单的 PHP POST 数据

php - 编辑 Laravel Azure Blob 存储的 x-ms-version

php - Laravel 集合循环到另一个集合

jquery - Tumblr 导航问题

javascript - 为什么 PHP echo 返回完整标记的 html 而不是 echo 中的内容?

include - Shopify Liquid 有条件地包含部分