Skip to content

Instantly share code, notes, and snippets.

Created January 27, 2011 20:58
Show Gist options
  • Select an option

  • Save anonymous/799260 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/799260 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Jan 27, 2011.
    62 changes: 62 additions & 0 deletions attemplate.inc.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    <?php

    //
    // attemplate.inc.php
    //
    // Created by Kenneth Ballenegger on 2009-06-30.
    // Copyright (c) 2009 Arure Talon. All rights reserved.
    //

    /*
    *
    * ATTemplate
    *
    * A template wrapper, based on the simplicity of using PHP itself for templating.
    *
    */

    class ATTemplate
    {
    private $proprieties;

    // constructor
    // optional $props: initial proprieties array
    function ATTemplate($props=null)
    {
    if(isset($props))
    if(is_array($props))
    $this->proprieties = $props;
    else
    $this->proprieties = array();
    }

    // set $key propriety as $value
    function set($key, $value)
    {
    $this->proprieties[$key] = $value;
    }

    // push array contents into proprieties
    // $props: proprieties array
    function push($props)
    {
    if(is_array($props))
    foreach($props as $key => $value)
    $this->set($key, $value);
    }

    // parse template $file
    // returns parsed text
    function parse($file)
    {
    $unique_id = uniqid();
    $file_{$unique_id} = $file; // this is to protect us from the eventuality the user has a propriety named file
    foreach($this->proprieties as $key => $value)
    ${$key} = $value;
    ob_start();
    require $file_{$unique_id};
    return ob_get_clean();
    }
    }

    ?>