How to include PHP calls in templates?

175 views
Skip to first unread message

OK3

unread,
Oct 28, 2010, 5:30:50 AM10/28/10
to H2o template PHP
I'm using h2o to parse and display templates within Kohana 3
framework. That's all well and good..
..but I'd like to include some php calls into my templates - e.g. to
call some helper functions like

// template.tpl
{% block content %}
<p>Hello, {{ username }}</p>
<p><?php echo HTML::anchor('/', 'Home'); ?></p>
{% endblock %}

How can I do that? I understand this may go against the templating
philosophy at all, but I don't want to sacrifice the framework's view-
related helper methods..

Thx for advice!

Taylor Luk

unread,
Oct 29, 2010, 12:25:36 AM10/29/10
to h2o-temp...@googlegroups.com

{{ 'home' | links_to '/' }}

:)



--
You received this message because you are subscribed to the Google Groups "H2o template PHP" group.
To post to this group, send email to h2o-temp...@googlegroups.com.
To unsubscribe from this group, send email to h2o-template-p...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/h2o-template-php?hl=en.


OK3

unread,
Oct 29, 2010, 3:39:17 AM10/29/10
to H2o template PHP
Taylor, thanks for your reply!

I'm afraid this is not solution I'm looking for. The link helper was
only an example. I know there are many helper functions in H2O, but
there are also many helper function s in the framework (Kohana, Zend
etc.). I was simply asking whether there is a way how to call *any*
PHP (class) function in the template.

There may be 2 solutions:
1) I can somehow run PHP code in the templates - but I don't know how
(this is what I asked for)
2) I could extend H2O class with some more helper functions that
simply wrap the framework's functions (this seems weird :)


On Oct 29, 6:25 am, Taylor Luk <subject...@gmail.com> wrote:
> {{ 'home' | links_to '/' }}
>
> :)
>
> On Thu, Oct 28, 2010 at 8:30 PM, OK3 <sir.ota...@gmail.com> wrote:
> > I'm using h2o to parse and display templates within Kohana 3
> > framework. That's all well and good..
> > ..but I'd like to include some php calls into my templates - e.g. to
> > call some helper functions like
>
> > // template.tpl
> > {% block content %}
> > <p>Hello, {{ username }}</p>
> > <p><?php echo HTML::anchor('/', 'Home'); ?></p>
> > {% endblock %}
>
> > How can I do that? I understand this may go against the templating
> > philosophy at all, but I don't want to sacrifice the framework's view-
> > related helper methods..
>
> > Thx for advice!
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "H2o template PHP" group.
> > To post to this group, send email to h2o-temp...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > h2o-template-p...@googlegroups.com<h2o-template-php%2Bunsu...@googlegroups.com>
> > .

Taylor Luk

unread,
Oct 29, 2010, 10:21:24 PM10/29/10
to h2o-temp...@googlegroups.com
You can register existing help into H2o as a filter by adding a callable type , see h2o/filters.php source code for detail

Example:

H2o::AddFilter('anchor', array('HTML', 'anchor'))

{{ '/' | anchor 'home' }}

To unsubscribe from this group, send email to h2o-template-p...@googlegroups.com.

OK3

unread,
Oct 30, 2010, 5:06:53 AM10/30/10
to H2o template PHP
Cool, I added new class to filters (h2o/filters.php):

class KohanaFilters extends FilterCollection {
static function anchor($link, $text, $attributes=null,
$protocol=null) {
return HTML::anchor($link, $text, $attributes, $protocol);
}
}
/* Add some Kohana filters */
h2o::addFilter(array('KohanaFilters'));

In template:

{{ '/controller/action' | anchor 'My action' }}

Well this works, however, I see two problems:
1) The helper HTML::anchor() returns string which includes HTML tags
and the tag boundaries '<' and '>' get escaped (htmlspecialchars?)
2) I need to pass an array of attributes to the 'anchor' filter - how?

{{ '/' | anchor 'Home', *array_of_attributes* }}

On 30 říj, 04:21, Taylor Luk <subject...@gmail.com> wrote:
> You can register existing help into H2o as a filter by adding a callable
> type , see h2o/filters.php source code for detail
>
> Example:
>
> H2o::AddFilter('anchor', array('HTML', 'anchor'))
>
> {{ '/' | anchor 'home' }}

Luke, I've also tried what you suggested:

H2o::AddFilter('anchor', array('HTML', 'anchor'))

This escapes htmlspecialchars too. And I still need to pass an array
somehow.

Is there a solution? I really appreciate your help and patience! :-)
> > <h2o-template-php%2Bunsu...@googlegroups.com<h2o-template-php%252Buns...@googlegroups.com>
Reply all
Reply to author
Forward
0 new messages