Comos / tage

Tage, the PHP template engine. Developing...
GNU General Public License v3.0
4 stars 2 forks source link

Show me a sample of a compiled template. #8

Closed bigbigant closed 9 years ago

bigbigant commented 9 years ago

I've to decide how to load compiled script now.

13leaf commented 9 years ago

like this

/**
*<html>
*Hello {{$name}}
* </html>
*/
use Tage\Runtime;
//_Templatexxx = _Template + md5(viewpath)
class _Templatexxx extends AbstractTemplate
{
  public function render($vars){
  echo "<html>\n";
 echo "Hello ";
 echo "$vars['name']";
echo "</html>";
 }
}
~
bigbigant commented 9 years ago

There're 3 small problems.

1.For compatibility with the old versions, a template have to use multiple variables. e.g.

<title>{{$api.current.doc.title|escape}} - {{$params.siteName|escape}}</title>

So I suppose there must be an extract($vars) action in render().

2.for compatibility with the old versions, again, a template mustn't under the namespace Tage\Runtime.

Sometimes, the system functions would be invoked directly without a \. So we have to make sure that the main rendering blocks run under global namespace.

3.As a performance consideration, to use static method is better, I guess.

13leaf commented 9 years ago
  1. Compiler can change $api.current as safeGet($vars['api'],"current").But we need to support the mixin php code,they can't easy to change,so I agree to extract $vars.
  2. Sorry,I am not familiar with namespace.As you said,under namespace can not compatible with custom class.(So where should we place the AbstractTemplate file?)
  3. I don't think static is a good idea.Static is difficult to test,difficult to composite and extend.As you like,we can singleton template instance to improve the performance.

The change version may be like this?

/**
*<html>
*Hello {{$name}}
* </html>
*/
use Tage\Runtime;
//_Templatexxx = _Template + md5(viewpath)
class _Templatexxx extends AbstractTemplate
{
  public function render($vars){
    extract($vars);
    echo "<html>\n";
    echo "Hello ";
    echo "$name";
    echo "</html>";
 }
}
bigbigant commented 9 years ago

OK. I think we have reached an agreement on the 1st and 3rd. As for the 2nd, no need to move the AbstractTemplate. Just address the Compiled classes under Global. Like this:

<?php
/**
*<html>
*Hello {{$name}}
* </html>
*/
use Tage\Runtime\AbstractTemplate;
//_Templatexxx = _Template + md5(viewpath)
class _Tage_Compiled_Template_Xxx extends AbstractTemplate
{
  public function render($vars){
    extract($vars);
    echo "<html>\n";
    echo "Hello ";
    echo "$name";
    echo "</html>";
 }
}

After all the compiled classes are not common, they wouldn't be loaded by any autoloader.

13leaf commented 9 years ago

Update the sample:

use Tage\Runtime\AbstractTemplate;
class _Tage_Compiled_Template_c21f969b5f03d33d43e04f8f136e7682 extends AbstractTemplate
{
  public function render($vars){
extract($vars);
echo <<<'TEXT'
<html>
Hello 
TEXT;
echo ($name);
echo <<<'TEXT'

</html>
TEXT;
 }
}