Raw Template Engine用のMojavi2 Renderer作りました。

やっつけ仕事用に使います。

RawRenderer.class.php

<?php
//
// RawRenderer - Renderer for Raw Template Engine.
//
// Copyright (C) 2005 Masaki Komagata <komagata@p0t.jp> 
//     All rights reserved.
//     This is free software with ABSOLUTELY NO WARRANTY.
//
// You can redistribute it and/or modify it under the terms of 
// the PHP License, version 3.0.
//
require_once "Raw.php";

class RawRenderer extends Renderer {
    function RawRenderer() {
        parent::Renderer();
        $this->engine =& new Raw;
    }

    function execute(&$controller, &$request, &$user) {
        if ($this->template === null) {
            trigger_error("A Raw template has not been specified", E_USER_ERROR);
        }

        $this->setAttributeByRef("controller", $controller);
        $this->setAttributeByRef("mojavi", $controller->getMojavi());
        $this->setAttributeByRef("request", $request);
        $this->setAttributeByRef("user", $user);

        if ($this->isPathAbsolute($this->template)) {
            $dir = dirname($this->template)."/";
            $this->template = basename($this->template);
        } else {
            $dir = ($this->dir === null) ? $controller->getModuleDir()."templates/" : $this->dir;
            if (!is_readable($dir . $this->template) && is_readable(TEMPLATE_DIR . $this->template)) {
                $dir = TEMPLATE_DIR;
            }
        }

        if (is_readable($dir.$this->template)) {
            $this->engine->template_dir = $dir;
            if ($this->mode  RENDER_VAR || $controller->getRenderMode()  RENDER_VAR) {
                $this->result = $this->engine->fetch($this->template);
            } else {
                $this->engine->display($this->template);
            }
        } else {
            trigger_error(””Template file $dir${this}->template does not exist or is not readable””, E_USER_ERROR);
        }
    }

    function &getAttribute($name) {
        $attribute =& $this->engine->template_vars[$name];
        return $attribute;
    }

    function removeAttribute($name) {
        unset($this->engine->template_vars[$name]);
    }

    function setArray($array) {
        $this->engine->assign($array);
    }

    function setArrayByRef(&$array) {
        $this->engine->assign_by_ref($array);
    }

    function setAttribute($name, $value) {
        $this->engine->assign($name, $value);
    }

    function setAttributeByRef($name, &$value) {
        $this->engine->assign_by_ref($name, $value);
    }
}
?>

FooView_success.php(使い方):

<?php
require_once(BASE_DIR."opt/renderers/RawRenderer.class.php");

class IndexView extends View {
    function &execute(&$controller, &$request, &$user) {
        $renderer = new RawRenderer();
        $renderer->setTemplate("Index.php");
        $renderer->setAttribute("message", $request->getAttribute("message"));
        return $renderer;
    }
}
?>

テンプレはRawと同じで。
デフォルトのRendererと違うところは、

  • 変数がグローバルに展開される。(Rendererは$template)
  • $controller, $request, $userが最初からassignされてる。(SmartyRendererと同じ)

デフォルトのRendererでその2点だけが気に入らなかったんですよね!

ref: Raw Template Engine

Comments


Option