Sunday 15 January 2012

closures - Using clousers to handle templates in PHP -


Similar to a million template engine for PHP (blade, jogging, sensible, mouth, ...), and I Just hitting the idea of ​​creating a new syntax and compiler for writing PHP inside the HTML! I think it's not just smart (but it's not that I'm here to discuss), is it wrong to write PHP + HTML in a normal way - not for logic - you know all the variables and loops and Defines that you want to experiment without this {{%%}} or {:: ::}! At least for the performance!

Now, I'm using Larreue these days, and this is great; It provides a simple PHP template system (except Blade and any other third party engine) which uses ob_start / include / ob_get_clean and eval. I was very pleased to know that I have a new syntax for writing PHP in HTML I can escape learning.

Here's what I am suggesting; Instead of using ob_ * functions instead, do we use closers? Here is a simple class that I put to make a point :

  class templates {static public $ templates = array}; Make a static public function ($ name, $ code) {self: $ templates [$ name] = $ code; } Fixed public function run ($ name, $ data) {if (! Isset (self: $ templates [$ name]) || is_callable (self: $ templates [$ name]) returned incorrect; Return call_user_func (auto :: $ templates [$ name], $ data); }   

}

and how it is used:

  Templates :: create ('test', function ($ Data) {return "hi". $ Data ['name']. '!';}); {Print Template :: Run ('test', array ('name' = & gt; 'zone')) for ($ I = 0; $ i & lt; 10; $ i ++); }   

I think this is much better in this way, because I do not need to use any output buffering or eval. And for "different concerns" here, we can make the code in a separate file to keep template :: things clean; in fact, things can be more simple and elegant in this way; We can create another method to load the template file:

  Fixed public function load ($ name) {self :: create ($ name, include ($ name .php) ); }   

and the content of the template file will be as simple as this:

  return function ($ data) {return "hi" $ Data ['name'] '! '; };   

What do you think about this? Is there a problem in displaying this kind of approach or closure?

I do not think there are any problems besides this, If you put the function in the array, this would mean that the functions are basically doing the same stuff.

What I mean by this:

In your example, you have only one task to accept 1 parameter. Therefore, all the functions that you have created to not glitch will accept the same set of parameters and will return the same type of data .

While declared, apart from this, the functions should make something different and unique.

Why such a solution is appropriate: When some engine is used, there may be many different functions already declared, to solve the conflict, they "hide" inside the arrays May be.

In addition to this, some people also say that anonymous tasks can generally be better in terms of performance. But we have to check it first: To call the function:

  1. Call a constant function run
  2. Check the function for existence
  3. See the function for the compatibility
  4. and then use call_user_func , which returns the return of your function. Therefore, 3x returns

    update

    My recommendation code for me:

    Possible check it will be very impressive performance.

      create a static public function ($ name, $ code) {if (! (Self: $ templates [name $]); {if (is_callable ($ code)) {self: $ Templates [$ name] = $ code;} Else {// func is not worth calling, an exception}} and {// function already exists} Enter an exception}   < P> This is how you can increase the performance by 2x:  
      Fixed public function run ($ name, $ data) {if (isset (auto :: $ template [$ name] )) {Auto :: $ templates [$ name] ($ data); // Make a direct call} and {// throw a new exception (0, "the function is not defined");}}    

No comments:

Post a Comment