Giter Club home page Giter Club logo

mason's Introduction

Mason Templates

Need templates in your PHP project fast, but don't want to deal with the overhead of a big template library? Well fear not! Mason Templates are a lightweight solution inspired by the famous PHP Template Language which has been used by PHP developers the world over since the very first release of PHP. Mason templates make it easy to separate the concerns of your view code from your application logic, all while maintaining the expressive power of PHP itself.

Examples

Basic Operation

It's easy to get started with Mason Templates, here is a minimal example:

include("path/to/your/lib/dir/Mason.php");
use mason\Mason as Mason;

/* Tell flash where to find our templates */
$flash = new Mason("path/to/your/template/directory");

/* Subdirectories are okay here too! */
echo $flash->render("favs.tpl");

To plug your variables into a template simply change the last line in the previous example to:

echo $flash->render("favs.tpl", array("name"      => "Maria",
                                      "favorites" => array("Raindrops on roses",
                                                           "Whiskers on Kittens",
                                                           "Bright copper kettles",
                                                           ...),
                                      ));

And in your template:

<h1>A few of my favorite things</h1>
<p>Hi, my name is <?=$name?>, and here are a few of my favorite things.</p>

<ul>
    <?php foreach($favorites as $item): ?>
    <li><?=$item?></li>
    <?php endforeach; ?>
</ul>

Template Inheritance

Mason also supports (single) template inheritance. See the examples directory for a full example.

index.tpl
...
<title>
    <?php $block("title");?>
        Default Title
    <?php $end();?>
</title>
...
<body>
    <?php $block("content");?>
        Default Content.
    <?php $end();?>
</body>
...

And in your derived template:

derived_and_contrived.tpl
...
<!-- Assignment makes it obvious that you can only extend one template per file. -->
<?php $extend = "index.tpl";?>

<?php $block("title");?>
Derived and contrived
<?php $end();?>

<?php $block("content");?>
New content for derived_and_contrived.tpl.
<?php $end();?>

Why

Read the above link! No, but seriously many people forget that PHP started out as a template language and get wrapped up in trying to write a complex meta-language, when all they really need is to echo some variables into a page. Mason Templates are the minimal amount of wrapper code to take an array of values and dump them into a template file. Mason Templates save you from having to write the (albeit small) amount of boilerplate required to use PHP as your template engine. If you're starting a project from scratch, or considering replacing your template engine why not give Mason Templates a try?

mason's People

Contributors

bostrt avatar dwest avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

mason's Issues

Before and After functions

Sometimes you want to append or prepend something to the parent's content instead of replacing it outright. This is not currently possible with append/prepend because they work only in the parent template. To that end we should add before and after functions that work outside of the normal blocks and are only concatenated after the block is full processed.

For example:

===File A===

Default

===File B===

<?php $after('content')?>
<div>After</div>
<?php $end('content')?>

===File C===

<?php $before('content')?>
<div>Before</div>
 <?php $end('content")?>

<?php $block('content')?>
<div>Override</div>
<?php $end('content')?>

Would produce:

Before

Override

After

Throwing Exceptions makes render unsuitable for use in __toString

When mason cannot find a requested template file it throws an exception. Unfortunately if this happens inside a __toString method this causes a fatal error which cannot be caught and provides little information as to why the application died.

Possible solutions:

  • Have an error reporting level (EXCEPTION|WARN|IGNORE|LOG) for development, testing, and production.
  • Detect if a __toString method is anywhere on the callstack and use trigger_error instead (not a fan of this one).
  • Just always print a warning (is problematic in a production setting when errors should not appear to the user)

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.