Giter Club home page Giter Club logo

php-template-class's Introduction

PHP Template Engine

A PHP Template Engine built for MVC.

Overview

Simple PHP template engine inspired by PUG.js and personal experiences.


How to use

It uses a MVC style to render pages. The view files are usually .html files. I didn't tested with the .phtml ones (yet).

Instantiate Class

First, we must create a new class instance using:

$layout = new Template();

The __construct method of this class accepts an array within options like:

$layout = new Template(array("dir" => "our/dir/to/views", "file" => "which_file_to_load.html", "vars" => array("var_key"=>"var_value")));

Yay! We made a new class instance!

But wait... Whats the purpose of this "vars" ?

Setting vars on HTML file

We can add reference for vars on the HTML files. It uses the engine syntax for it. The engine will put the var automatically on the var reference.

You can use:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>{{title}}</title>
    <!-- One method for var reference -->
  </head>
  <body>
    {{$nav}}
    <!-- Another method for var reference -->

    <_template:body_var/>
    <!-- Another method for var reference -->

    <_template:$footer_var/>
    <!-- Another method for var reference -->
  </body>
</html>

And you have two methods to add vars on the engine:

  • The array within the new class instance (intializing);
  • The method addVar, like $layout->addVar("var_key","var_value").

Oh yeah! You can render the template with $layout->render() or $layout->render(true). The argument will "echo" the result on the page.

ATTENTION: Note that the var reference is the var_key on the definition.

Managing Blocks

Sometimes we just want to use one single file to keep repeating data within the block HTML. To achieve this, we first need to use the getBlock method, like this:

$layout = new Template();
$layout->loadFile("path/to/file.html");
$block = $layout->getBlock("loop"); // loop block must exists on the file
if($block){ // important to check if the block really exists on the file
        // from now on, we have a template block
        if(true){
                $block->loadFile("path/to/block/file.html"); // we can load an specific file
        }else{
                // we can just ignore it. The file HTML will be what was inserted on the parent file
        }
        $block->setBlock(array("var_key"=>"var_value")); // we can pass a new array to render the template partial within our data (arrays for additional information)
        $layout->setBlock($block); // now we pass back the block we've got to the original template file to render it (the block itself to render)
}

And the best: we can set MULTIPLE CHILD BLOCKS, WITHIN MULTIPLE BLOCKS!

You can set blocks with <_template:block(blockName)></_template:block>. ATTENTION please remember to give a name for the blocks. Each name should be unique (for now).

Here's an example:

<_template:block(childBlock)>
  <!-- here I can use as block the HTML I'll insert here, or I can load a HTML file -->
</_template:block>

You can also UNSET blocks. For this, just user: $layout->unsetBlock(blockName).


And does it have any condditional operations?

Yes it have! We can set Ifs for the template. Of course, false conditions will remove the whole code inside a IF block in the end.

Here's how we can set it:

  • On the HTML file:
<_template:if(hasVar)>
        Some code here.
</_template:if>
  • On PHP
$vars = array(
  "title" => "Title",
);
$layout->setIf("hasVar",(isset($vars)));

Wow, simple... Thats just it? Yes. We can also unset IFs, calling the unsetIf("name") method. We can also set an IF to false, and then the IF will be removed. Oh yeah, if you're using IFs within BLOCKs, be sure to set an if to false AFTER the block is setted on the main instance

I want to add a custom code there. How can I do it?

Custom codes will be setted at the end of the to be rendered HTML. With the code method, you can insert more blocks or ifs or vars, and it will be rendered on the end.

Use it like this:

$layout->code("My custom <b>code</b> <em>here</em>");

Okay. You explained almost everything so far, but can we use functions?

Of course!

We can both:

  • Set a var with the function output
  • Use the code to insert a result of a function
  • Use the template tag to automatically call a function, like this: <_template:function.time()/> or this: <_template:function.time/> Or even so: <_template:function.date(Y-m-d)/>

One more thing.

To make things easier and faster, you can automatically sets many template operations only on the HTML. For example: You can automatically call loadFile:

<_template:loadFile(path/to/file.html)>


Enjoy!

If you find any bug, please report.

php-template-class's People

Contributors

matheus2212 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.