Overview

Packages

  • tipy

Classes

  • Tipy
  • TipyApp
  • TipyCli
  • TipyCliSession
  • TipyConfig
  • TipyController
  • TipyCookie
  • TipyDAO
  • TipyEnv
  • TipyFlash
  • TipyInflector
  • TipyInput
  • TipyIOWrapper
  • TipyLogger
  • TipyMailer
  • TipyModel
  • TipyOutput
  • TipyRequest
  • TipySession
  • TipyTestCase
  • TipyTestRunner
  • TipyView

Exceptions

  • AssertionFailedException
  • CompileErrorException
  • CompileWarningException
  • CoreErrorException
  • CoreWarningException
  • DeprecatedException
  • NoMethodException
  • NoticeException
  • ParseException
  • RecoverableErrorException
  • StrictException
  • TipyDaoException
  • TipyException
  • TipyModelException
  • TipyRollbackException
  • TipyValidationException
  • UserDeprecatedException
  • UserErrorException
  • UserNoticeException
  • UserWarningException
  • WarningException
  • Overview
  • Package
  • Class
  • Deprecated
  • Todo
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 
<?php
/**
 * TipyView
 *
 * @package tipy
 */

/**
 * V in MVC. HTML template engine based on PHP output buffer and PHP itself
 *
 * - Templates are located in app/views directory
 * - Templates are simple php files with .php extension.
 * - Templates have variables defined in controller's TipyOutput instance and don't<br/>
 *   have access to application context except TipyView instance represented by $this (see note below)
 *
 * <code>
 * // app/controllers/BlogController.php
 * class BlogController extends TipyController {
 *     public function article() {
 *         $this->out('title', 'Hello');
 *         $this->out('message', 'World!');
 *     }
 * }
 * </code>
 *
 * <code>
 * // app/views/blog/article.php
 * <!DOCTYPE html>
 * <html>
 * <head>
 *     <title><?= $title ></title>
 * </head>
 * <body>
 *     <p><?= $title.' '.$message ?></p>
 * </body>
 * </html>
 * </code>
 *
 * ## Custom template names
 *
 * You can explicitely render views with custom names
 *
 * <code>
 * // app/controllers/BlogController.php
 * class BlogController extends TipyController {
 *     public function article() {
 *         $this->out('title', 'Hello');
 *         $this->out('message', 'World!');
 *         $this->renderView('path/to/custom_template');
 *     }
 * }
 * </code>
 *
 * ## Note about $this
 *
 * $this is available inside template and gives access to
 * TipyView instance which renders current template.
 *
 * <code>
 * <ul>
 *     <li>
 *         <? $this->includeTemplate('item') ?>
 *     </li>
 * </ul>
 * </code>
 */
class TipyView {

    /**
     * variables assigned to templates
     * @internal
     */
    private $assigns;

    /**
     * absolute path to template files
     * @internal
     */
    private $templatePath;

    /**
     * These two stacks are used when processed template (or its part)
     * is nested into other template.
     * @see TipyView::applyTemplateStart()
     * @see TipyView::applyTemplateEnd()
     * @internal
     */
    private $templateStack;
    private $contentStack;


    public function __construct() {
        $this->assigns          = new TipyIOWrapper();
        $this->contentStack     = [];
        $this->templateStack    = [];
    }

    /**
     * Assign variables map to template.
     * This will replace all output data.
     */
    public function bind(array $map) {
        $this->assigns->bind($map);
    }

    /**
     * Get value of the variable assigned to template by variable name.
     * If $key does not exists may return $defaultValue.
     *
     * @param string $key
     * @param mixed $defaultValue
     * @return mixed
     */
    public function get($key) {
        if (func_num_args() > 1) {
            return $this->assigns->get($key, $func_get_arg(1));
        } else {
            return $this->assigns->get($key);
        }
    }

    /**
     * Assign variable to template
     *
     * @param string $key
     * @param mixed $value
     */
    public function set($key, $value) {
        $this->assigns->set($key, $value);
    }

    /**
     * Get all assigned variables
     *
     * @return array
     */
    public function getAll() {
        return $this->assigns->getAll();
    }

    /**
     * Set path to templates
     *
     * @param string $path
     */
    public function setTemplatePath($path) {
        $this->templatePath = $path;
    }

    /**
     * Compile template and return result as a string
     *
     * @param string $templateName
     * @return string
     */
    public function processTemplate($templateName) {
        $templateFile = $this->expandTemplatePath($templateName);
        $vars = $this->assigns->getAll();
        extract($vars);
        $output = "";
        ob_start();
        include($templateFile);
        $output = ob_get_clean();
        return $output;
    }

    /**
     * Get full path to template file
     *
     * @param string $templateName
     * @return string
     */
    public function expandTemplatePath($templateName) {
        return $this->templatePath . "/" . $templateName . ".php";
    }

    /**
     * Include template (child) to currently rendering template (parent).
     * All parent template variables will be available to its children as
     * they exist in one context.
     *
     * Should be called from template via **$this**.
     *
     * <code>
     * <ul>
     *     <li>
     *         <? $this->includeTemplate('item') ?>
     *     </li>
     * </ul>
     * </code>
     * @param string $templateName
     */
    protected function includeTemplate($templateName) {
        $templateFile = $this->expandTemplatePath($templateName);
        $vars = $this->assigns->getAll();
        extract($vars);
        include($templateFile);
    }

    /**
     * Wrap another template (layout) around currently rendering template or even its part (child).
     *
     * Layout template should have **$this->childContent()** call to specify where child
     * template will be inserted.
     *
     * All defined variables will be available in both templates as they exist in one context.
     *
     * Should be called from template via **$this**
     *
     * <code>
     * // app/views/child.php
     * <p>
     * <? $this->applyTemplateStart('layout') ?>
     *     TipyView is cool!
     * <? $this->applyTemplateEnd('layout') ?>
     * </p>
     * </code>
     * <code>
     * // app/views/layout.php
     * <strong>
     *     <? $this->childContent() ?>
     * </strong>
     * </code>
     * Will be rendered as
     * <code>
     * <p>
     * <strong>
     *     TipyView is cool!
     * </strong>
     * </p>
     * </code>
     *
     * @param string $templateName
     */
    protected function applyTemplateStart($templateName) {
        // Put template name into stack. We will use it
        $this->templateStack[] = $templateName;
        // And start processing
        ob_start();
    }

    /**
     * Indicates the end of layout
     * @see applyTemplateStart()
     */
    protected function applyTemplateEnd() {
        // Get what we have processed and put it into stack
        $this->contentStack[] = ob_get_contents();
        ob_end_clean();
        // Get template name from stack and process it
        $output = $this->processTemplate(array_pop($this->templateStack));
        print $output;
        array_pop($this->contentStack);
    }

    /**
     * Insert caller template's content into layout template
     * @see applyTemplateStart()
     */
    protected function childContent() {
        $stacksize = sizeof($this->contentStack);
        // if we have something in stack then return last value
        if ($stacksize > 0) {
            return $this->contentStack[$stacksize-1];
        } else {
            return '';
        }
    }

}
tipy API documentation generated by ApiGen