Welcome Guest, Not a member yet? Register   Sign In
Which template system should I use ?
#1

[eluser]miau[/eluser]
Hello,

I'm about to start a new project and I need to choose a template system for it.
I used Template Parser Class from CI and it was fine, but now I'll need some more functionality, like simple template logic:

Code:
[this is page template]
<b> blabla </b>
{if logged_in} Hello, {user} {else} Please login {endif}
...

I know smarty can do that. Anybody tried to use smarty with CI ? How about speed?
Or is there any other lib which can do that?

Thanks!
miau

ps. I don't want to use CI views with PHP code fragments, because it can easily lead to mess in templates. specially in teams Wink
#2

[eluser]Rick Jolly[/eluser]
Good template engines will "compile" the template into php, so there isn't much of a performance hit. I've used Html_Template_Flexy and phptal with CI. Many others use Smarty.

Html_Template_Flexy is nice because it treats form fields as serializable objects - like asp.net form controls. It will save the form field state automatically, so there is no need to populate/repopulate form data in your views. It also allows you to set form field attributes in the controller in an object oriented way - again, just like asp.net.

Phptal is nice because it is a template attribute language. You'd need to read the documentation to appreciate that, but it allows you to create templates often without the {} syntax - the variables/conditionals/loops can all be defined within html tags. Here is the example from the phptal docs:
Code:
&lt;?xml version="1.0"?&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title tal:content="title"&gt;
      this will be replaced with a dynamic page title
    &lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    <h1 tal:content="title">this will be replaced with a dynamic page title</h1>
    <table>
      <thead>
        <tr>
          <th>name</th>
          <th>phone</th>
        </tr>
      </thead>
      <tbody>
        <tr tal:repeat="item result">
          <td tal:content="item/name">item name</td>
          <td tal:content="item/phone">item phone</td>
        </tr>
        <tr tal:replace="">
          <td>sample name - this row will not be visible when running on the server</td>
          <td>sample phone - this row will not be visible when running on the server</td>
        </tr>
      </tbody>
    </table>
  &lt;/body&gt;
&lt;/html&gt;
#3

[eluser]charlieD[/eluser]
I personally don't like using templates as I think most of them add another pretty useless layer to get used to (is Smarty really that different from PHP tags?) but I heard of Blitz which is one of the fastest as it's written in C as an extension.

http://freshmeat.net/projects/blitz-temp..._id=262959

However, PHPtal looks pretty nice. Jolly, can it handle conditionals too?
#4

[eluser]Rick Jolly[/eluser]
[quote author="charlieD" date="1191999074"]
However, PHPtal looks pretty nice. Jolly, can it handle conditionals too?[/quote]
Some examples:
Code:
<span tal:condition="loggedin">you are logged in</span>
<span tal:condition="not: loggedin">you are not logged in</span>
<span tal:condition="loggedin" tal:content="logged_in_message"/>
#5

[eluser]charlieD[/eluser]
Thanks, will definitely try out PHPTal.

Just noticed that I had originally misread your handle thinking it was just 'Jolly' - sorry about that, sounds kinda rude calling someone by their last name like that!
#6

[eluser]Rick Jolly[/eluser]
CharlieD, you can call me Frank if you like. IMO phptal makes simple things elegant, but some things can get ugly. The syntax for including partial views within a layout view is horrible. Also, working with tag attributes (selecting a form option, or changing a class on a menu link to highlight it) isn't very elegant.
#7

[eluser]esra[/eluser]
[quote author="Rick Jolly" date="1192005891"]CharlieD, you can call me Frank if you like. IMO phptal makes simple things elegant, but some things can get ugly. The syntax for including partial views within a layout view is horrible. Also, working with tag attributes (selecting a form option, or changing a class on a menu link to highlight it) isn't very elegant.[/quote]

These are more like MVC architectural limitations rather than template engine faults.
#8

[eluser]Phil Sturgeon[/eluser]
There is a way to get the funtionality you were looking for, I have done it before using the template class.

You need do do some dirty code such as:

Code:
$data['if_logged_in'] = (isValidUser()) ? array(array()) : array();
$data['if_not_logged_in'] = (!isValidUser()) ? array(array()) : array();

This example is using FAL helper, but similar logic for any. Basically it makes it into a loop, so when you do {if_something...} code {/if_something} {if_not_something}more code{/if_something} it will only show if there is a loop to be itterated through. Remember that you can only use one of these if statements per-page unless you get the improved Parser class with a fixed PREG match.

I dont remember who originally showed me this code, I do not claim credit for the ingenious method.
#9

[eluser]EugeneS[/eluser]
i thought about this a lot of time and have come to idea that any tamplate engine is just useless with the CI and MVC (in general)

just use alternate PHP syntax in your views .... do not use OOP in views, only simple variables, arrays with the "human friendly" names and keys.

with this limitations, as you can see below, view looks exactly like template but works really faster ) more over your php editor vill highlight alternate PHP syntax and this template will be easy to modify.

just adopt that template delimiters are: &lt;? ?&gt;
to show variables delimiters: &lt;?=;?&gt;
logic constructions:
&lt;? if(): ?&gt;
&lt;? else: ?&gt;
&lt;? endif; ?&gt;

its really looks like template engine .... right Wink so what the sense of template engine instead of views Wink

so your view will look like:

Code:
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="padding-left:7px; padding-right:7px">
<tr>
    <td>
        <h1>Корзина</h1>
        <p><a href="&lt;?=$site_url;?&gt;">Начало</a> &raquo; <a href="&lt;?=$site_url;?&gt;/cart/">Корзина</a></p>
        <table width="100%" border="0" cellpadding="0" cellspacing="0">
    &lt;? if(isset($cart_arr) && count($cart_arr) > 0): ?&gt;
        &lt;form action="&lt;?=$site_url;?&gt;/cart/update" method="post"&gt;
        <tr>
            <td><h2>Товар</h2></td>
            <td width="100"><h2>Количество</h2></td>
            <td width="50"><h2>Операция</h2></td>
        </tr>
        <tr>
            <td colspan="3" style="padding:0;">
                <hr>
            </td>
        </tr>
        &lt;? foreach($cart_arr as $prod_id => $prod_details): ?&gt;
        <tr>
            <td class="small">&lt;?=$prod_details['Name'];?&gt;</td>
            <td valign="top">&lt;input type="text" name="item[&lt;?=$prod_id;?&gt;]" value="&lt;?=$prod_details['Number'];?&gt;" size="8" maxlength="6" class="input"&gt;</td>
            <td valign="top"><a href="&lt;?=$site_url;?&gt;/cart/delete/&lt;?=$prod_id;?&gt;" class="podmenu">Выложить</a></td>
        </tr>
        &lt;? endforeach; ?&gt;
        <tr>
            <td colspan="3" style="padding:0;">
                <hr>
            </td>
        </tr>
        <tr>
            <td class="text" colspan="3">
                Количество: <b>&lt;?=$prod_count;?&gt;</b> шт. на сумму: <b>&lt;?=$prod_cost;?&gt;</b> руб.
            </td>
        </tr>
        <tr>
            <td colspan="3" style="padding:0;">
                <hr>
            </td>
        </tr>
        <tr>
            <td style="padding:0;" align="left">
                &lt;? if(isset($referrer)): ?&gt;
                    &lt;input type="button" value="&lt;< Продолжить покупку" onClick="location.href='&lt;?=$referrer;?&gt;'"&gt;
                &lt;? endif; ?&gt;
            </td&gt;
            <td colspan="2" style="padding:0;" align="right">
                &lt;input type="submit" value="Пересчитать"&gt;
            </td>
        </tr>
        &lt;/form&gt;
...............
#10

[eluser]miau[/eluser]
Thanks for answers Smile

Rick: PHPtal looks interesting, but I found this: [a]http://ludo.qix.it/archive/2003/08/PHP_more_PHPTAL.html[/a] telling that it's slow. Speed is very important for me now, so I'll keep looking (however, PHPtal functionality is just that, what I need).

EugeneS: You showed just what I'm trying to avoid. PHP code mixed with HTML. I tried this before and I know this doesn't work well in bigger projects.

I'm still considering using Smarty. I saw some speed comparsions with other engines and it looked quite good. Anyone has been using it?

Greetings,
miau




Theme © iAndrew 2016 - Forum software by © MyBB