dev-master
9999999-dev https://github.com/kennstphn/SWD-RenderRendering Engine for rendering objects into templates
WTFPL
The Requires
- php >=5.3.0
by Ken Sherman
templating template mvc oop render
Rendering Engine for rendering objects into templates
Scope, (*1)
-Implementation, (*2)
--Templates, (*3)
---Placeholders, (*4)
---Template Defining, (*5)
--Objects, (*6)
--Arrays, (*7)
--Strings, (*8)
--Methods, (*9)
The scope of this project is to create a templating solution that conforms to the following standards, (*10)
The solution incorporates my favorite aspect of twig, placeholders for values in html, with OOP while retaining the logical power of PHP that you are familiar with., (*11)
When \SWD\Render::render()
is passed an object and template variable, it looks for a template string in the following places, and stops looking further when it finds one., (*12)
$allTemplates
passed variable (assuming you didn't pass null, and the rendered object has a class name)
$allTemplates->Your\Name\Spaced\ClassName
property which is a string$yourObject->template
\SWD\Render::render()
's caller, and code continues running.BEGINNERS -- Beginners are suggested to use 1.ii and 1.iii until you understand controllers and want to get into that logic, (*13)
MVC Programmers -- You can see how this allows clear access to a MVC model using the
\SWD\Render::render($application, $yourController)
style, especially by 1 -implementing a controller in the$allTemplates->__call($classname, $object)
, joined with being able to nest objects for rendering their own templates conditionally. (see section on Methods below), (*14)
Templates consist of normal html strings with placeholders. The only syntax to know is that placeholders can only consist of the aphabet, numbers, or underscores. This follows the patterns for php properties/variables and methods/functions. For example - {{twitter_section}}
or {{my2ndObject}}
., (*15)
Templates can be defined (as a default) within a class itself by assigning a string to the 'template' property. Whether a default template is assigned or not, we always look for an override within the global $swdTemplates object. See further details on the options for this under the "Objects" section, (*16)
NOTE: The global $swdTemplates can have objects assigned to the template areas. If \SWD\Render::render() finds an object at $swdTemplate->MyClass, it calls the
get_template($object);
method of $swdTemplates->MyClass (notMyClass->get_template($object);
). This allows for easy extension into conditional templating, but keeps the logic in php where it belongs!, (*17)
From Template section..., (*18)
When
\SWD\Render::render()
is passed an object, it looks for a template string in the following places, and stops looking further when it finds one., (*19)
- The global object $swdTemplates for a property named 'Namespace_Slashes_Use_Underscores_MyClassName' that is a string
- The global object $swdTemplates for a method named 'Namespace_Slashes_Use_Underscores_MyClassName' that returns a string
- The object itself for a 'template' property.
- If no template is found, a User Notice level error is thrown to the
\SWD\Render::render()
's caller.
Once the template string has been defined, \SWD\Render::render()
grabs all {{placeholder}} patterns within the template string, and replaces it with the first option from the following list that applies., (*20)
Rendering Recursion then kicks in, and \SWD\Render::render()
is then called again and passed the evaluated property or method. Nested Objects, Arrays, and strings are thus rendered as long as (they are called by the parent object's template) and (objects have a template)., (*21)
NOTE: You can call
\SWD\Render::render($object);
on objects that are from other developers. Since templates assigned via the global $swdTemplates override (not replace) the $object->template property, no class/object hacking is necessary to non-destructively access the public properties/methods of the object;, (*22)
As just mentioned, \SWD\Render handles arrays as a "foreach" loop, rendering each value. If you want to conditionalize this loop, then you want logic. You'll want to check out the Methods section below, (*23)
if \SWD\Render::render();
is called on a string, it returns the string as is. This allows for us to call \SWD\Render::render($arraysOfObjectsAndStrings);
and receive output that is handled appropriately., (*24)
This section allows templating logic that does not conflict with our design scope! If you need the result of a function in your template, simply call that function from within the template string as {{your_method_name}}. This is replaced by the result of your function, whether it be a string, another Object (define a template in your $allTemplates variable!) or even an arrayOfStringsAndOrObjects. This becomes a highly powerful tool that enables sophisticated templating, while never once allowing markup language to become logical language., (*25)
NOTE: the method is passed the $allTemplates object/array/whateveryoupassedInOriginally. You can use this to flip switches in your controller as desired by, (*26)
- Calling a public function in your template (i.e. {{use_controller}})
- accepting the passed variable like so --
public function use_controller($controller){}
in your rendered object.- dont forget that you can propogate the changes back up the chain of reference to other templates by using an ampersand in your function declaration
Rendering Engine for rendering objects into templates
WTFPL
templating template mvc oop render