Giter Site home page Giter Site logo

gabordemooij / stamp Goto Github PK

View Code? Open in Web Editor NEW
45.0 6.0 6.0 128 KB

Stamp is a template system for clean HTML only templates, no foreign syntax required.

Home Page: http://www.stampte.com

License: Other

JavaScript 0.91% PHP 95.40% HTML 3.60% Python 0.08%

stamp's Introduction

STAMP

Build Status

Stamp is micro template library orignally written by Gabor de Mooij.

Stamp t.e. is a new kind of Template Engine for PHP. You don't need to learn a new template language and you get 100% separation between presentation logic and your HTML templates.

How it Works

Stamp t.e. is a string manipulation based template engine. This is a different approach from most template engines which use inline templating. In Stamp t.e. you set markers in your template (HTML comments), these are then used to manipulate the template from the outside.

What does it look like

A cut point maker marks a region in the template that will be cut out from the template and stored under the specified ID.

<div>
<!-- cut:diamond -->
<img src="diamond.gif" />
<!-- /cut:diamond -->
</div>

<span>
<!-- paste:jewellery -->
</span>

Now pass the template to StampTE:

$se = new StampTE($templateHTML);

To obtain the diamond image:

$diamond = $se->getDiamond();
echo $diamond;

Result:

<img src="diamond.gif" />

And.. to put some diamonds in the jewellery box:

$se->jewellery->add($diamond);

Easy!

More info: http://www.stampte.com

Advantages

  • Clean, code-free HTML templates, No PHP in your HTML
  • Compact presentation logic free of any HTML
  • No new syntax to learn, uses basic HTML markers already in use by many frontend developers to clarify document structure
  • Templates do not have to be converted to be used with PHP logic (toll free template upgrades)
  • Templates are presentable before integration because they may contain dummy data which is removed by StampTE
  • Easy to exchange templates, templates are ready to use
  • Very suitable for advanced UI development and complex templates for games
  • Templates become self-documenting, PHP code becomes more readable (less bugs)
  • Automatically strips HTML comments
  • Integrated caching system
  • Automatically escapes strings for Unicode (X)HTML documents
  • Just ONE little file
  • Unit tested, high quality code
  • Open Source, BSD license

stamp's People

Contributors

gabordemooij avatar jensscherbl avatar slava-vishnyakov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

stamp's Issues

v2.3.1 Hash Sign # Literals & Slots

Greetings Gabor,
StampTE v2.3.1:

$html = '<style>
    div#chaz {
        color: #900;
        background-color: #333;
    }
</style>
<div id="chaz">#name#</div>';
$se = new StampTE($html);
$se->setName('Melville');
echo $se;

Results in:

<style>
div#&chaz {
    color: #900;
    background-color: #&333;
}
</style>
<div id="chaz">#name#</div>

Expected result:

<style>
div#chaz {
    color: #900;
    background-color: #333;
}
</style>
<div id="chaz">Melville</div>

StampTE v2.1.0 produced the expected output.
Thank you,
Chaz

Add the unit test with PHPUnit

As title, I think we can write the unit tests with PHPUnit at this time.
I think this can be more proper than using the customized tests.

Thanks.

Magic does not work in some circumstances

Assume the following two templates exist.

File views/layout.html

<!DOCTYPE html>
<html data-stampte="#htmlAttributes?#">
    <head data-stampte="#headAttributes?#">
        <meta charset="UTF-8">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <title data-stampte="#titleAttributes?#">#title?#</title>
        <!-- cut:css -->
        <link data-stampte="#linkAttributes?#" rel="stylesheet" type="text/css" href="#href?#" >
        <!-- /cut:css -->
        <!-- cut:link -->
        <link data-stampte="#linkAttributes?#" rel="#rel?#" type="#type?#" href="#href?#" >
        <!-- /cut:link -->
        <!-- cut:script -->
        <script data-stampte="#scriptAttributes?#" src="#src?#" ></script>
        <!-- /cut:script -->
                <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- paste:head -->
    </head>
    <body data-stampte="#bodyAttributes?#">
        <!-- paste:body -->
    </body>
</html>

File views/components/table.html

<table>
    <th>
        <!-- cut:headerCell -->
        <td>#content#<td>
        <!-- /cut:headerCell -->
        <!-- paste:header -->
    </th>
</table>

Execute the following php:

File index.php

<?php

require __DIR__ . '/vendor/stampte/StampTemplateEngine/StampTE.php';

use StampTemplateEngine\StampTE as StampTE;
use StampTemplateEngine\StampTEException as StampTEException;

$table = StampTE::fromFile("views/components/table.html");
$headerCell = $table->get('headerCell');
$headerCell->inject('content', 'ID');
$table->add($headerCell, 'header');

$htmlDocument = StampTE::fromFile("views/layout.html");
$script = $htmlDocument->get('script');
$script->inject('src', 'js/index.js');
$htmlDocument->add($script, 'script');
$htmlDocument->add($table, 'body'); // Replace this line with $htmlDocument->body->add($table); and it works.
$htmlDocument->setTitle('Dior Data Management');
echo $htmlDocument;

Expected result:
HTML table is shown with a single header row with the text "ID" in it. Or at least some sort of error.

Actual result:
The $table template is not rendered. The layout template is rendered. No error is emitted.

Extending filter method

Current filter() implementation doesn't allow you to add nl2br() function for text with line breaks.

#note#

$line->inject('note', nl2br($note));

gives:

Some note<br /> in two lines

which renders as:
Some note
in two lines

The easiest way to fix this is to update filter() to return:
return nl2br(htmlspecialchars($data,ENT_COMPAT,'UTF-8'));

But it would be better to give coders an opportunity to add their own filters or to extend/replace default function.
Using behavior pattern would be an excellent solution here :)

Injecting String with $ in Value

Stamp TE v2.1.0. Injecting a string with a dollar sign causes odd results.

$template = '<table>
        <thead>
            <tr><th>Pizza</th><th>Price</th></tr>
        </thead>
        <tbody>
        <!-- cut:pizza -->
            <tr><td>
            #name#
            </td><td>
            #price#
            </td></tr>
        <!-- /cut:pizza -->
        </tbody>
    </table>';

$data = array(
  'Margherita' => '$7.00',
  'Funghi' => '7.50',
  'Tonno' => '8.00'
);

$priceList = new StampTE($template);
foreach($data as $name=>$price) {
  $pizza = $priceList->getPizza();
  $pizza->setName($name);
  $pizza->setPrice($price);
  $priceList->add($pizza);
}
echo $priceList;

Expected table with "$7.00" for margherita pizza. But Stamp TE's result was ".00".

Better composer support

Package is missing from packagist.org. Also, release tags are missing since 2.1.0, so composer can't find the proper version when adding the repository directly.

Notice when gluing a snippet to undefined glue point.

Hi again!

If you try to glue a snippet to a glue point that isn't defined, you get a notice:

Notice: Undefined offset: 0 in D:\z\out\i-wish\lib\Stamp\StampTE.php on line 182

I think it would be helpful if Stamp just ignores such situations.
In my example i'm validating a form. And each error message under each field in my form template is a snippet:

<!-- cut:fieldError -->
<small class="error" rel="#fieldId#">#message#</small>
<!-- /cut:fieldError -->

<label for="url">URL:</label>
<input id="url" name="url" value="#url#" type="url"/>
<!-- paste:urlError -->
<br />

<label for="price">Price:</label>
<input id="price" name="price" value="#price#" type="text"/>
<!-- paste:priceError -->
<br />

My validation logic is encapsulated inside a model, so it checks all attributes that should be validated to store an item.
After validation not passes i have a stack of errors with keys and i try to glue every message to its place.
Sometimes i don't want to display a message for a particular field on a particular form, but i still want this field to be validated as others.

So it would be helpful if I could just try to glue a snippet. If there's no appropriate found โ€” OK, just do nothing.

Also it would be nice to have something like $myTemplate->hasPaste('some_paste') which returns true or false.

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.