• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using CI with PHPTAL

#1
[eluser]Clowerweb[/eluser]
Hello,

I'd like to start off by pointing out that I have already read the thread located at http://ellislab.com/forums/viewthread/62886/ regarding PHPTAL integration with CI. The problem is that the thread is quite old, and it seems as though some things have changed either with CI or PHPTAL or both, as I could not get what is described there to work.

If anyone could provide me with some useful insight as to getting PHPTAL working with CI, that would be excellent. My current attempted integration:

1.) system/application/libraries - created tal.php as described in the above linked thread, with the copied code.

2.) system/application/config/autoload.php contains the following:
Code:
$autoload['libraries'] = array('database', 'tal');

3.) system/application/controllers/test.php contains the following:
Code:
<?php
    class Test extends Controller {
        public function test() {
            parent::Controller();
            
            $this->load->helper(array('url', 'form'));
        }
        
                public function index() {
                        $news = $this->news->display();
                        $this->tal->title = 'Just another blog...';
                        $this->tal->news  = $news;
                        $this->tal->display('index.html');
                }
        }

This is the example as defined in the above referenced post.

The error I am getting is:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Blog::$news

Filename: controllers/blog.php

Line Number: 13

Fatal error: Call to a member function display() on a non-object in C:\wamp\www\ci\system\application\controllers\blog.php on line 13

I am of course assuming this means that display() is no longer a valid method in CI.

You'll have to excuse my ignorance - I only just began exploring CI 3 days ago, and I am also somewhat new to PHP programming in general, though I do have a thorough understanding of basic PHP/ MySQL, and basic understanding of OOP concepts. I am NOT an experienced or highly knowledgeable programmer, however. My background is mainly in graphic design, (X)HTML/ CSS, design integration with various PHP based content management/ ecommerce systems like Magento, Zen Cart, Drupal, Wordpress, etc., and using JavaScript libraries like jQuery.

I am exploring CodeIgniter as a tool to help me accomplish CRUD tasks without needing to spend a ton of time learning the nitty-gritty of programming, though I do of course understand that at least SOME programming is involved in using it. A templating parser such as PHPTAL would be the icing on the cake for a design-oriented person such as myself, where I spend much of my time living in tag land.

Enough rambling Wink

Any help would be greatly appreciated and please bear in mind that I am very much a layman. Thanks in advance!

EDIT: I just noticed that it was trying to load my blog.php file which I created while following the CI tutorials, however eliminating that page produced the same result (replace blog.php and class Blog with test.php and class Test in the above error).

#2
[eluser]Clowerweb[/eluser]
I got it working - pretty awesome stuff! The problem was with the example somewhere. This was how I got it working:

(Using the same wrapper described in my first post)

Code:
public function index() {
    $this->tal->title = 'Just another blog...';
    $this->tal->display('blog_view.php');
}

Then in views/blog_view.php (the call to the phptal parser is in the <h1>):

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html &gt;
    &lt;head&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
        &lt;title&gt;Test&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        <h1 tal:content="title">Something</h1>
    &lt;/body&gt;
&lt;/html&gt;

And the expected result is output to the screen:

Just another blog...

Very cool Big Grin

I hope this helps someone with the same question I had.

#3
[eluser]Clowerweb[/eluser]
Just a little update - I remade part of the blog tutorial using PHPTAL, and the results are in...

The old blog_view.php:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html &gt;
    &lt;head&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
        &lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        <h1>&lt;?php echo $heading; ?&gt;</h1>
        &lt;?php $rows = $query->result(); ?&gt;
        <div>
            &lt;?php foreach ($rows as $row) { ?&gt;
            <h3>&lt;?php echo $row->title; ?&gt;</h3>
            <p>&lt;?php echo $row->body; ?&gt;</p>
            <p>&lt;?php echo anchor('blog/comments/'.$row->id, 'Comments'); ?&gt;</p>
            <hr />
            &lt;?php } ?&gt;
        </div>
    &lt;/body&gt;
&lt;/html&gt;

The new blog_view.php:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html &gt;
    &lt;head&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
        &lt;title tal:content="title"&gt;Title&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        <h1 tal:content="heading">Heading</h1>
        <div tal:repeat="entry entries">
            <h3 tal:content="entry/title">Title</h3>
            <p tal:content="entry/body">Body</p>
            <a href="blog/comments/${entry/id}">Comments</a>
            <hr />
        </div>
    &lt;/body&gt;
&lt;/html&gt;

Both methods produce identical output.

NOTE: I know some people have used arguments against using templating engines, such as "use php short tags in templates instead, it's almost just as clean without the performance hit", and "use php alternate syntax for your loops, such as foreach (): enforeach;".

I'm not a fan of either of these methods for a few reasons:
1.) It is said that PHP is going to depreciate alternate syntax
2.) Short tags is bad practice because it's not very portable (and might end up depreciated as well)
3.) I know this isn't a good argument when I'm using a templating engine, but I have heard that short tags and alternate syntax cause a performance hit on the parser.
4.) Both still look fugly in the html in my opinion, even though they may look a little better than traditional methods

All that said, I am finding CodeIgniter to be a very pleasurable experience thus far Smile, and PHPTAL is a very nice addition to it for me.

Now I need to get to work learning both and making good use of them!

#4
[eluser]n0xie[/eluser]
I've always been against template parsers, since PHP is in itself a template language. Also it means you now have to know 2 languages, PHP as well as the template language, which means that whenever someone new looks at the code he first has to learn that specific template language.

So maybe you can convince me why I should use a template language?

#5
[eluser]Clowerweb[/eluser]
[quote author="n0xie" date="1254492308"]I've always been against template parsers, since PHP is in itself a template language. Also it means you now have to know 2 languages, PHP as well as the template language, which means that whenever someone new looks at the code he first has to learn that specific template language.

So maybe you can convince me why I should use a template language?[/quote]

I doubt I could 'convince' anyone of anything. That's not really what I was setting out to do anyway. It's really a matter of personal preference. As for learning 2 different languages, I agree in part that this is one drawback, however PHPTAL is very simple, with only less than 15 different various syntax rules (a few of which would only be rarely used, if ever, in most cases):

1.) tal:define (define a variable - constant or globals possible too)
2.) tal:condition (IF statement replacement)
3.) tal:repeat (foreach loop replacement - handles arrays, associative arrays and iterator objects)
4.) tal:omit-tag (ignore the element's open and close tags)
5.) tal:replace (replace entire tag with a value or nothing if no value is given)
6.) tal:content (replace the content within a tag with the evaluation of its expression)
7.) tal:attributes (change tag attribute values)
8.) tal:on-error (what to display if tal/ php/ mysql encounters and error)
9.) tal:block (a syntactic sugar for elements which contain many TAL attributes which are not to be echoed)
10.) metal: (define and call XML/ XHTML macros)
11.) i18n: (only used for internationalization/ translation)
12.) php: (use the above along with php code to extend them if a tal alone can't do everything you need)
13.) phptal:tales (change the behavior of of a tal, such as one of the ones listed above)

So you can see there isn't much to learn about it. It's very simple and straightforward, and anyone should be able to learn and understand the majority of it in under an hour. It's not equivalent to learning a complete framework or new programming language, or even a complete markup language. There are only about 10 different things in that list that might need to be used in most cases.

My primary reason of course is just to keep the HTML code cleaner and achieve further separation between presentational code and server side code where MVC alone isn't enough.

I'd also like to note that the performance hit that comes as a result of the template parser is completely unnoticeable. I have seen no difference whatsoever, though I haven't actually benchmarked it. CodeIgniter is a VERY fast framework anyway, especially compared to most others, so the additional couple of nanoseconds or milliseconds that may be used to parse the template don't bother me. I'll take cleanliness and readability over speed any day, and it seems to make the HTML more easily maintainable.

#6
[eluser]esra[/eluser]
[quote author="Clowerweb" date="1254637190"][quote author="n0xie" date="1254492308"]I'd also like to note that the performance hit that comes as a result of the template parser is completely unnoticeable. I have seen no difference whatsoever, though I haven't actually benchmarked it. CodeIgniter is a VERY fast framework anyway, especially compared to most others, so the additional couple of nanoseconds or milliseconds that may be used to parse the template don't bother me. I'll take cleanliness and readability over speed any day, and it seems to make the HTML more easily maintainable.[/quote]

This is because PHPTAL methods are embedded as attributes in XML elements and are parsed with the rest of the HTML. Any speed decrease should be in nanoseconds.

#7
[eluser]taviroquai[/eluser]
Sorry unburry this topic but I have to say that PHPTAL is a must!

Three reasons for using it (portability, maintenance and speed):

1. Web Designers can work in there xhtml files with NO PHP inside - open index.html and it just works! Try to do this with a normal CI view... :p
2. PHPTAL is very easy... i can do almost anything with 4 keywords:
tal:content,
tal:attributes,
tal:repeat,
tal:condition.
3. Templates are cached. No more interpreting php views for every request. Its fast.

Thats it!

#8
[eluser]sunset11[/eluser]
Totaly agree with tavoroquai.

That's the best template engine at the market place and I would add :

- full i18n support
- cross-scripting proof
- well formed HTML constraint
- powerfull encapsulation of any level of your layout

I wish long life to PHPTAL and CI !


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.