Re: Including files for templates
| From: | Eric Butera | Date: | Mon, 24 Mar 2008 16:06:25 +0000 |
| Subject: | Re: Including files for templates | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-272035@lists.php.net to get a copy of this message | ||
On Sun, Mar 23, 2008 at 7:32 AM, Terry Burns-Dyson
<hammerstein_02@msn.com> wrote:
> I'm trying to write a template system, my template is the HTML layout, and
> my content is fetched from another source. However I don't quite understand
> how to output the template so that all the variables are parsed by PHP.
> Simple version of what I'm trying to do;
>
> ob_start( );
>
> extract( $params, EXTR_PREFIX_SAME, "am_");
>
> $pageContent = file_get_contents( "page_to_display.html");
>
> include( "template.html");
>
> echo ob_get_clean( );
>
>
> $pageTitle is in the template, it's replaced, $pageContent is in the
> template, it's replaced. But any variables within the page_to_display are
> simply output into the page rather than processed by PHP. I realise that
> file_get_contents is basically returning a string and I"m just doing string
> replacement when I include the template.html, so my only question is, what's
> the actual way of doing this?
>
> Thanks
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
I used to use this extract/include methodology before I really thought
about the performance aspect of it. I would create a template object
that could have variables injected into it and then tell it to
render(). The render process would extract everything in the data
array & include a template file.
After I found out about xdebug I stopped doing this.
Now my approach is more in line of the Zend_View[1] template engine.
Just a generic class to define the path to the template, a way to put
variables into it, and a render function. The big difference this
time is that instead of extract I just use $this-> inside the
template.
[1] http://framework.zend.com/manual/en/zend.view.html