Sensation Energy

Tutorials


TPL PHP

TPL template with PHP

Introduction

TPL is a template file which is a common text file that contains user-defined variables that are entitled to be overridden by user-defined output content when a PHP web application

  • Unzip the zip file and insert it on the folder server (Wamp. Xamp, Lamp)
  • Run via localhost - Example: http://localhost/tpl_php/example.php
  • Below I will show you how to make and use through TPL nad PHPthe code.

    Basic

    Here we will describe our TPL template which consists of:

    • example.php - PHP code
    • tem.tpl - HTML, CSS and JS code

    This is a very simple example where we connect PHP with TPL

    How it is work:

    • 1. First make exp.php which looks like this:
    • 
      Name: {{name}}
      Email: {{email}}
      Age: {{age}}
      
      
    • 2. Then make exp.tpl look like this:
    • 
      //everything that is specified in ekp.php is written here
      //Example: Instead of {{neme}} write Admin
      {{neme}}
      {{email}}
      {{age}}
      <h2>
      
      
    • 3. Add the following code below:
    • 
      < ?php
      //Here you assign values to the fields
      //Example $exit['name'] indicates that the name Admin
      $exit['name'] = 'Admin';
      $exit['email'] = 'admin@mail.com';
      $exit['links3'] = '47';
      //This indicates to which TPL are assigned information
      $tem = file_get_contents(dirname(__FILE__).'/exp.tpl');
      $out = preg_replace_callback('/(\{\{(.*?)\}\})/si', 'compute_replacement', $tem);
      //echo $out; - indicates output
      echo $out;
      function compute_replacement($test) {
      global $exit;
      //$exit; means $exit['name']
      //The order in which the data is sent
      return isset($exit[$test[2]]) ? $exit[$test[2]] : '['.$test[2].']';
      }
      ?>
      
      

    Function

    This is complete PHP code that just does as an example to show you how PHP and TPL work

    In PHP we create and add values. We also add functionality for printing and the path where it will be printed (exp.tpl)

    This is an example PHP code for our template

    
    &lt;?php
    $exit['title'] = 'Title';
    $exit['header'] = 'This is header';
    $exit['header_subtitle'] = 'Subtitle';
    $exit['footer'] = 'Copyright @ footer';
    $exit['left'] = 'Left';
    $exit['center'] = 'Center';
    $exit['right'] = 'Right';
    $exit['home'] = 'Home';
    $exit['link1'] = 'Index';
    $exit['link2'] = 'Admin';
    $exit['link3'] = 'Blog';
    $exit['link4'] = 'Contact';
    $exit['user'] = 'User';
    $exit['content']= '<p>  What is Lorem Ipsum?
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
    <br>
    <p>  What is Lorem Ipsum?
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>';
    $exit['left_content'] = '    <p>  What is Lorem Ipsum?
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>';
    $exit['link_title'] = 'Link';
    $exit['links1'] = 'Link1';
    $exit['links2'] = 'Link2';
    $exit['links3'] = 'Link3';
    $exit['links4'] = 'Link4';
    $exit['r_link1'] = 'Link A';
    $exit['r_link2'] = 'Link B';
    $exit['r_link3'] = 'Link C';
    $exit['r_link4'] = 'Link D';
    $exit['right_link'] = 'Right Link';
    $exit['left_link'] = 'Left Link';
    $exit['right_content'] = '<p>This is right content</p>
    <p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>';
    $tem = file_get_contents(dirname(__FILE__).'/tem.tpl');
    $out = preg_replace_callback('/(\{\{(.*?)\}\})/si', 'compute_replacement', $tem);
    echo $out;
    function compute_replacement($test) {
    global $exit;
    return isset($exit[$test[2]]) ? $exit[$test[2]] : '['.$test[2].']';
    }
    ?>
    
    

    TPL template

    In TPL, the value set from ekp.php is written, which is previously written with the addition of HTML and CSS elements and their order and layout.

    This is an example TPL code for our tem

    
    <div class="card">
    <title>{{title}}</title>
    <h1>{{header}}</h1>
    <h2>{{header_subtitle}}</h2>
    <div class="left">
    <a href="index.php"  class="link">{{home}}</a>
    <a href="#"  class="link">{{link1}}</a>
    <a href="#"  class="link">{{link2}}</a>
    <a href="#" class="link">{{link3}}</a>
    <a href="#"  class="link">{{link4}}</a>
    </div>
    <div class="right">
    <a href="#"  class="link">{{user}}</a>
    </div>
    <div align="center">
    <h2>{{left}}</h2>
    </div>
    <div class="card">
    {{left_content}}
    </div>
    <br>
    <div align="center">
    <h2>{{left_link}}</h2>
    </div>
    <div class="card">
    <a href="#"  class="link">{{links1}}</a><br>
    <a href="#"  class="link">{{links2}}</a><br>
    <a href="#" class="link">{{links3}}</a><br>
    <a href="#"  class="link">{{links4}}</a><br>
    </div>
    <div align="center">
    <h2>{{center}}</h2>
    </div>
    <div class="card">
    {{content}}
    </div>
    <div align="center">
      <h2>{{right}}</h2>
    </div>
    <div class="card">
    {{right_content}}
    </div>
    <div align="center">
    <h2>{{right_link}}</h2>
    </div>
    <div class="card">
    <ul>
    <li><a href="#"  class="link">{{r_link1}}</a></li>
    <li><a href="#"  class="link">{{r_link2}}</a></li>
    <li><a href="#" class="link">{{r_link3}}</a></li>
    <li><a href="#"  class="link">{{r_link4}}</a> </li>
    </ul>
    </div>
    </div>
    </div>
    
      

    Content


    Documentation



    Answer the following question:

    You must be logged in


    Advertising