• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Better view files

#11
(08-04-2015, 08:32 AM)iamthwee Wrote: Thanks, I think I definitely made an elementary school boy error in rushing the prototype CMS system by writing very poor view files.  

Take a look at the template I use to handle about 4,000 web-pages:

PHP Code:
<?php 
require VIEWPATH .'_atemplate/incs/__doctype-strict.php';

echo 
"\n<head>\n";
 
 require VIEWPATH .'_atemplate/incs/__header.php';
 
 if(isset($vids)):
 
   $css base_url() .VERPATH .'assets/css/lazyYT.css';
 
   echo NN.'<link rel="stylesheet" href="' .$css .'">';
 
   echo $tmp = <<< ______TMP
      <style>
      .grid .item {display: inline-block; 
                   width:47%; min-width:280px; margin: 0 1% 8% 1%; padding:0;}
      </style>
______TMP;
 
 endif;
 
 echo isset($css2Add) ? $css2Add NULL;
echo 
"\n</head>\n";

?><body>

  <div class="logo por tac"> <?php # (isset($HAS_PICTURE) && $HAS_PICTURE) ? 'bg6' : 'bg4'; ?>

    <div class="clr w88 mga">
      <?php 
        if
LOCALHOST || MYRA ): 
 
         # Hunky Dory  
 
         if(1) require VIEWPATH .'_atemplate/incs/__DS_DEBUG_HEADERS.php'
 
       endif;

 
       require VIEWPATH .'_atemplate/incs/__menu-btns-2015-06-17.php'

 
       if( isset($HAS_PICTURE) && $HAS_PICTURE ):
 
         echo '<i class="poa pbr">Has Pictures</i>';
 
       else
 
         $tmp 'http://www.google.com/webmasters/tools/mobile-friendly/'
 
                .'?url=' .$canonical;
 
           echo '<a class="poa pbr tdn fg6" href="' .$tmp .'">';
 
             echo 'Google Mobile Friendy';
 
           echo '</a>';
 
       endif;
 
     ?>
    </div>
  </div><?php #logo ?>
  <?php 
      if
(LOCALHOST):
 
       echo '<div class="fll p42">
                <a href="/c_edit"> <b>edit</b> </a>       
              </div>'
;
 
     endif 
  ?>


 
 <div id="layout"><!-- Menu toggle -->

   <div id="main">
      <div class="content">
        <div class="header">
          <br />
          <h1 class="ooo tac"> <?= ucwords($joke_title); ?></h1>
          <?php $titleH2 = isset($titleH2) ? $titleH2 '&nbsp;';?>
          <h4 class="ooo tac fg4"> <?=$titleH2;?> <br /><br /> </h4>
        </div>


        <?php
          if
(10):
 
           $pix    = array('pix''funny-pix''funny-pictures''health-and-safety'
 
                           'health-and-safety-pictures''overloaded');
 
           $groups = array('top-10-jokes''top-20-jokes''favourites','blondes' 
                            
'blonde-jokes''chuck-norris-jokes'
 
                           'iphone''little-johnny-jokes''sex-jokes');
 
           $blogs  = array('debug''admin','subscribe','spend_ways'
 
                           'about''blog','contact','getdow','terms');

 
           $segs   strtolower ($this->uri->segment(1));

 
           if( isset($jotd) ) {
 
             require VIEWPATH .'_atemplate/specials/jotd.php';

 
           }elseif( isset($page_kid) && 'v-001/login' === $page_kid ) {
 
             require VIEWPATH .'_atemplate/specials/login.php';

 
           }elseif( in_array($segs, ['joke-of-the-day''calendar']) ) {
 
             require VIEWPATH .'_atemplate/specials/joke-of-the-day-calendar.php';

 
           }elseif( 'c_tabs'===$theClass ) {
 
             require VIEWPATH .'_atemplate/tabs/' .$segs .'.php';

 
           }elseif( strpos($canonical'all-the-jokes') ) {
 
             require VIEWPATH .'_atemplate/specials/all-the-jokes.php';

 
           }elseif( in_array($segs, ['eliza']) ) {
 
             require VIEWPATH .'_atemplate/specials/eliza.php';

 
           }elseif( in_array($segs, ['the-frogs']) ) {
 
             require VIEWPATH .'_atemplate/specials/the-frog.php';

 
           }elseif( isset($amjuggle) ) {
 
             require VIEWPATH .'_atemplate/specials/amjuggle.php';

 
           }elseif( isset($thepix) ) {
 
             require VIEWPATH .'_atemplate/specials/pix.php';

 
           }elseif( isset($thelot) ) {
 
             require VIEWPATH .'_atemplate/specials/thelot.php';

 
           }elseif( isset($vids) ) {
 
             require VIEWPATH .'videos/video-001.php';

 
           }/* ALWAYS SET */ elseif( isset($joke_memo) ) {
 
             require VIEWPATH .'_atemplate/specials/joke-memo-string.php';

 
           }elseif( 'videos'===$segs ) {
 
             echo '<h3 class="tac">Coming Back Soon</h3>';

 
           }else {
 
             echo 'Problem ?'
 
           }
 
         endif;
 
       ?>

      </div><?php # content ?>
    </div><?php # main ?>
  </div><?php # layout ?>

  <?php /* if(0): ?>
    <div class="tac ba_468x60">
      < ?php # require VERPATH .'assets/js/propellerads-banner-468x60.js';? >
    </div>
  <?php endif; */ 
?>


  <?php
    if
( isset($quotes) && ('jotd' !== $this->uri->segment(1)) ):
 
     #echo '<hr />';
 
     echo '<div class="clb w88 mga tac">';
 
       echo '<dl class="dib tal p42 bd2 bdr bg1">';
 
         echo '<dt class="fs2"><strong>
                Quotes for Today:
               </strong></dt>'
;
 
         foreach($quotes as $quote)
 
         {
 
           echo '<dd><br />'.$quote .'</dd>';
 
         }
 
       echo '</dl>';
 
     echo '</div><br />';
 
   endif;
 
 ?>

  <div id="XXXfoot" class="bg5 fg4 tac">
    <?php require VIEWPATH .'_atemplate/incs/__footer.php';?>
  </div>

<?php    
  
# PROFILER: PREVENTS DOUBLE <body></html>
 
   #if(! $_SESSION['jjj']['profiler']): 
 
     # echo '</body></html>'; 
 
   #endif;

echo '</body></html>'
Reply

#12
Hmm, yeah I certainly don't want to echo out html using php Wink but thanks...

I started a simple test with the html parser.

PHP Code:
class Template_test extends CI_Controller {

    public function 
index()
    {
        
$this->db->select('*');
        
$this->db->from('pages');
        
$query $this->db->get();
        
        
$this->load->library('parser');

        
$url1 site_url('admin/template_test');

        
$data = array(
                
'url1'         => $url1,
                
'page_title'   => 'My Page Title',
                
'page_heading' => 'My Page Heading',
                
'pages' => $query->result_array()
        );

        
$this->load->view('admin/header');
        
$this->load->view('admin/body');
        
$this->parser->parse('admin/template/template-test'$data);
        
$this->load->view('admin/footer');
    }



And more view files looks a lot better:

Code:
<div class="pmf-container" >
    <div class="row pad" >
        <div class="col-sm-12">
            <header class="panel-heading font-bold">{page_title}</header>
            <section class="panel">
                <div class="panel-body">
                    {pages}
                        {name}
                    {/pages}

                </div>
            </section>
        </div>
    </div>
</div>

Like we discussed before I don't think there should be a great deal of logic in your views other than for loops and some simple if else statements. I like the idea of passing these vars to the view in a multi-dimensional array. Even removing the site_url() from the view and setting this as a one word variable and passing it to the view seems better. I'm also going to get rid of all the codeigniter form_helpers in the view, well most of them.
Practical guide to IgnitedCMS - Book coming soon
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.