Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quitting Smarty to do it manually

I am facing the problem that I'm not really sure how to develop without a framework or a template engine. I started coding that way and now I want to go to basics.

I used to work with this MVC schema, using Codeigniter and Smarty as a template engine. What I want to do now is to use raw php without both tools mentioned.

I don't know how to "copy" the concept of Smarty's "block" and "extends".

I used to define a base.tpl file which had html head, only the body tag, and the base css and js files (the ones that are always used in every page of the site), like this: (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 -->

and then when I need to call this base.tpl I did this:

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

I have been searching but I am affraid I'm missing the right words to search because I am not finding answers.

I would like to be guided please!

like image 313
Limon Avatar asked Oct 07 '15 00:10

Limon


1 Answers

Another way could be to do something like this. I feel like this is probably closer to what a template engine ends up with, but with using the {block} syntax instead.

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>
like image 189
Antony Thompson Avatar answered Nov 02 '22 23:11

Antony Thompson