Forum / Development Forum / Rain TPL 3 / include tag for rainTPL3 


+   Reply to Thread

Repost http://www.raintpl.com/Forum/Development-Forum/Feedback/?t=239

I think this the the right forum.

I was checking the code of rainTPL3 and it looks extremely amazing. Seems like a very interesing evolution of the rainTPL2.

I noticed that the \Rain\TPL->_check_template( $templateFile ) method returns a string with the full path to the compiled template.

The current {include} tag works this way.

1.- Obtain the class name with a global function.

2.- Create new \Rain\TPL object.

3.- Assign the variables from current TPL object to new object.

4.- Invoke the draw method which can be subdivided in other processes.

4.1.- Extract the assigned variables.

4.2.- ob_start(); //ob_start() was already invoked in the first \Rain\TPL object

4.3.- invoke _check_template method.

4.4.- return ob_get_clean();

From all those process the only one is neccessary. The _check_template method.

So my proposition is to take advantage that this is still a beta and try something different about it.

Invoke the _check_template method directly instead of making those processes described above.

 $parsed_code .= '<?php require ".
  $this->_check_template("$include_template")
  '?>';

So this is a good idea, bad idea or good but dangerous idea? I will try to apply it on my fork but I wanted to ask here first https://github.com/Faryshta/raintpl3

Good day
Hi Farshyta,
the reason why it call another RainTPL class is because the value of incude might be dynamic, ie:
{include="$page"}

Probably it could be a good to use your solution when the parameter template is static, that could improve performances.

Could you fork the RainTPL 3, apply the change and send a pull request?
Thanks so much for your help.
I already forked it and started to pull changes since three days ago https://github.com/faryshta/raintpl3

The first thing I changed was the RainTPL_Exceptions names and methods.

I will keep trying to find another solution to this include thing.
Great, I'll check your pull request next week. For the {include} I think we can use both solutions, with a preg_match like this (need to be checked):

if( preg_match( "/\$\w*/", $include_template ) ){
  ... is dynamic so use the actual code
}else{
  ... is static so use "require $include_template;"
}


What do you think about it?
I honestly don't like it :-P

I will keep trying other ways to make it dinamic and not requiring new objects.

One thing I noticed is that since the php requires happens inside the class then we can use the $this variable and access to protected and private methods in any file that is required inside the class.

For example I tested this this morning.

test.php

<?php
class test{
 private function inaccessible(){
  echo "inaccessible method has been called.\n";
 }

 public function check_include(){
  echo "about to include.\n";

  require "include_file.php";
 }
}


$a=new test;

echo "object initialized.\n";

$a->check_include();


include_file.php

<?php

echo "the file was included.\n";

$this->inaccessible();


The oout put is

object initialized.
about to include.
the file was included.
inaccessible method has been called.

So I will keep trying to find a solution. I will pull more commits during the weekend.

Good day and thanks for the responses.
I think solved it.

I pushed the changed on my github I hope you can see them later.

I tested it with static and dynamic includes and both seem to be working.

The only problem with my solution is that the _black_list method didn't worked but I think I fixed it. Still it needs more testing.

See ya.
So what do you think? Could you check my solution?
Hey, can you please post the link to your pull request? I cannot find it, thanks.
Maybe I didn't send it properly.

Which is the command to do it? I haven't used git before and only know SVN.

You can see the changes on my fork https://github.com/faryshta/raintpl3
Hi, thanks for your pull request! I merged it.
I'm having some problems with the current implementation - Here is what I'm trying to do:
$structure = array(
  array(
    'tpl' => 'text',
    'data' => array( /* ... */)
  ),
  array(
    'tpl' => 'thumbnails',
    'data' => array( /* ... */)
  )
);

$tpl->assign('structure', $structure);


So far, so good... So I'm trying to build a sort of "dynamic" template which loads other templates and fills them with my data. This is what I've done so far:
{loop="$structure"}
  {include="$value.tpl"}
{/loop}

Which results in an error, because of this rendered PHP:
... $tpl->draw( "$value1["tpl"]" ); ...

Is there any simple solution, instead of "just do it outside the template in plain PHP, duh!" ?
Hi Leo, thanks for reporting the bug, I'll fix it soon.
http://www.raintpl.com/Forum/Development-Forum/Rain-TPL-3/?t=241&new_post

+   Reply to Thread