Code Monkey home page Code Monkey logo

phptemplate's Introduction

PhpTemplate

2016-02-03 -> 2021-03-05

Simple php template system.

PhpTemplate is part of the universe framework.

Install

Using the planet installer via light-cli

lt install Ling.PhpTemplate

Using the uni command.

uni import Ling/PhpTemplate

Features

  • simple
  • tag replacement
  • conditional

How does it work?

  1. Create one directory and put all your php templates in it
  2. When you need a template, call the write method

Tutorial

  1. Choose and create a directory where you will put all your php templates.

Mine will be phptemplates directory at the root of my app.

  1. Now create a template inside the phptemplates dir. Your template is a regular php file.

Mine is named headerBar.ptpl.

I use the ptpl extension because I think it makes it cleaner to know the type of file I'm working with. I also setup my IDE to interpret ptpl files as php (for syntax interpretation).

But if your prefer so, you can stick with a php extension.

The content of my file is the following:

<div id="header_hook">
    <div id="header">
        <div class="leftbar">
            <a data-featherlight="#login_popup" href="/login" class="wllogin wlbutton">LOGIN</a>

            <form class="search" method="get" action="/search">
                <input name="q" type="search" value="" placeholder="Rechercher un élément">
                <input class="icon-magnifier" type="submit" value="&#xe905;">
            </form>
            

            <?php if ($p->opt('icons')): ?>
                <a href="/"><i class="icon-prozaic"></i></a>
                <a href="/"><i class="icon-channel"></i></a>
                <a href="/"><i class="icon-pro"></i></a>
                <a href="/"><i class="icon-calendar"></i></a>
            <?php endif; ?>
        </div>
        <a class="$channelLogoClass" href="$channelLink"><span>$channelName</span></a>
    </div>
</div> 

The syntax is explained in the template syntax section below, but let's move continue.

  1. Now let's use it.

Actually, before we can use it, we need to tell PhpTemplate where the template directory is. So in your application init file, write this:

PhpTemplate::$templateDir = __DIR__ . "/phptemplates"; 

Now we can use it. Somewhere in your php code, put the following:

PhpTemplate::write('headerBar.ptpl', [
    'channelLogoClass' => 'wllogo icon-logo',
    'channelLink' => '/',
    'channelName' => 'Actarus prod',
], [
    'icons' => true,
]);

Read more about the write method in the phpTemplate methods section below.

Template syntax

The template syntax is very simple. It provides the following mechanisms

  • tag replacement
  • options

Tag replacement

Tag replacement is done by prefixing the placeholder name by a dollar symbol ($). It's just a string nicely that integrates with your html code.

Options

Options allows you to use booleans variables in your templates. A typical use for boolean variable is to choose whether or not a certain portion of your template will be displayed. You simply use the php language to create the condition block.

You access the boolean variable (aka option) via the pilot object (see pilot section below), using the opt method. See an example in the mini tutorial above.

Pilot

The pilot object is the only object available in your template code. You access it via the reserved variable $p.

Its methods are the following.

opt

bool        opt  ( str:optionName )

Return whether or not the given option is set to true.

Php Template methods

write

void    write( str:tpl, array:tags=[], array:options=[] )

The tpl parameter is the template relative path, from the template directory.

The tags parameter defines the placeholders available inside the template code. It's a simple key to value array.

The options parameter defines the options available inside the template code. Options are accessed via the pilot.opt method, see the pilot section for more details.

Related

Ptpl is nice for static templates. But if you want more dynamic (fetched via ajax for instance) templates, you can use php template's companion: htmltemplate.

History Log

  • 1.0.4 -- 2021-05-31

    • Removing trailing plus in lpi-deps file (to work with Light_PlanetInstaller:2.0.0 api
  • 1.0.3 -- 2021-03-05

    • update README.md, add install alternative
  • 1.0.2 -- 2020-12-08

    • Fix lpi-deps not using natsort.
  • 1.0.1 -- 2020-12-04

    • Add lpi-deps.byml file
  • 1.0.0 -- 2016-02-03

    • initial commit

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.