منتدى استراحات زايد

منتدى استراحات زايد (http://vb.ma7room.com/index.php)
-   منتدى أخبار المواقع والمنتديات العربية والأجنبية (http://vb.ma7room.com/forumdisplay.php?f=183)
-   -   Styles Templates: Allow object method calls (http://vb.ma7room.com/showthread.php?t=279393)

محروم.كوم 11-30-2009 04:00 AM

Styles Templates: Allow object method calls
 
Before you chew me out for suggesting that we allow PHP in templates* please hear me out (because that's not what I'm suggesting).

The new vB Template syntax is a good step in the right direction. It creates more verbose code that is easier to read. But we're still lacking something that would be solved by allowing PHP in templates. However* as so many people seem to be dead-set against that* how about a compromise?

What about allowing the Template to call a method of an object that's passed to it? Something like the following:

PHP Code:
class Formatter {
function
renderHeader () { /* standard header code* probably an include */}
function
renderBanner () { /* standard banner code */ }
function
renderFooter() { /* standard footer code* probably an include */}
function
render () {}

class
MyObjFormatter extends Formatter {
..
function
render () {
..
some code
}
function
renderFooter () { /* override std footer code */ }
}

$myobj = new FormatterObject($param1*$param2);

$templater - vB_Template::create('GENERIC_SHELL');
$templater->register('mycustomobj'*$myobj);
print_output($templater->render());

And then you could do the following to the template...

PHP Code:

{
vb:call_raw $myobj->renderHeader () }

{
vb:call_raw $myobj_renderNavbar() }
{
vb:exec $myobj->render() }
{
vb:call_raw $footer->renderFooter() }



In this case* vB_Template would call the functions of the object that you pass to it. In my example above* I'm taking a very OO approach and I'm providing a base Formatter class that handles the rendering of the header* footer* content and pagetitle for me. (By doing this we could easily remove 99% of the container templates (FORUMDISPLAY* STANDARD_** etc) and just use GENERIC_SHELL for just about every page. This would reduce alot of the duplicated code in the templates and with some judicious splitting up of the current templates* it would allow us to re use some of the existing code that's already out there simply by making a call to it.)

But that's just a side benefit of this idea. What I'd like to be able to do is not have all of these monolithic scripts that marry the display code and the processing logic in the same file. I'd like to see the different display portions of the code split up into smaller portions and turned into objects and then have be responsible for their own rendering.

For example* I've often had to duplicate the code for "userinfo" from the postbit template. I'd rather just require_once (class_postbit_userinfo.php)* instantiate the object with a userid passed to the constructor and then call render. This would allow me to do all sorts of things like re-use templates AND code... without using copy-paste.

Similarly* if I need to add a new kind of post (for example* vbulletin.org's plugin format)* I could do so by extending the postbit rendering class* making my own changes and then just sending that kind of object to the template instead.

Now* I get that most vB admins aren't PHP or OO savvy* but the fact is that most of those will never do anything more than install plugins and ask questions in the community forums on how to edit templates. But the rest of us would truly benefit from having the ability to add dynamics behaviors to the templating system.

Note: Some are likely to ask how this differs from the existing code base where we can just call render on a template* fetch the results and then print_output.

Well* the magic is in the ability to separate custom code from the vB code base. Because each Formatter class could be responsible for it's own rendering* you'd almost never have to change the vB code and/or templates. And if you did* those changes would be minimal and would only pertain to those portion of the templates where you actually made changes.

It's not the custom code that is the biggest vulnerability to vB users* it's the fact that upgrading to a later version is such a PITA if you've done extensive template & code hacks. Because updates are so painful* many people skip releases and updates and so are left with known security holes issues.

Therefore* I'm for any idea that helps me reduce the number of mods that I have to make to the stock vB system (and I'm also for partitioning the templates to a more granular level) :).

Cheers*
Dave.


الساعة الآن 06:10 AM

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.5.2 TranZ By Almuhajir


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