CakePHP and Smarty(ies)

Joe Topjian has written a tutorial about using Smarty, a template engine, with CakePHP. If you ever wanted to use Smarty with CakePHP, go for it! But be warned, there are some restrictions (e.g. no access to the html helper).



  1. Yoeri
    Posted August 11, 2006 at 3:49 pm | Permalink

    I wonder why you should use a template engine on top of CakePHP? The V in MVC is just like a template engine, not? At least the design is seperated. (beginner’s question :-))

  2. Posted August 11, 2006 at 4:09 pm | Permalink

    @Yoeri: In my opinion there is no reason to use a template engine on top of CakePHP. But there are people out there who like something like smarty. So if you want to use smarty with CakePHP, you can do it, but personally I don’t recommend it.

  3. masterjail
    Posted October 7, 2006 at 1:47 pm | Permalink

    Hello, when I use smarty I can do $smarty->display(…) to render the view or $smarty->fetch(…) to get the string result in a variable…

    Is there any way to get the string result in a variable with CakwPHP (without using Smarty, of course)

  4. Posted October 8, 2006 at 2:05 pm | Permalink

    @masterjail: I don’t understand what you mean (I am not a Smarty user). Maybe you can explain it in another way what you try to accomplish?

  5. Posted October 30, 2006 at 9:12 pm | Permalink

    masterjail ment the smarty->fetch which will return the compiled template html code (the same code that would end up in your browser). He wants the output in a variable instead of sending it to the browser.

  6. Posted December 23, 2006 at 6:50 am | Permalink

    I have been using this handy Smarty vendor class, which allows the use of all helpers in templates and has a helper function so you can declare arrays in templates:

    It might help if someone updated the post with this.

  7. Posted May 20, 2007 at 8:58 am | Permalink

    cake views can be rendered into a string variable vary easily… using the “requestAction()” function…




  8. Graeme Wicksted
    Posted September 1, 2007 at 7:57 am | Permalink

    Smarty is a lot better than writing php templates because it reduces the template code (which is a lot better if you are working with a web designer).

    If only PHP had a Clay-style templating engine (Facelets is likely going to be Clay-like).

    And for all those who say “smarty is slow” — you are absolutely wrong. Smarty compiles to native php so it is just as fast as writing an actual php file but Smarty is cleaner and usually faster to develop with.

  9. Posted September 1, 2007 at 1:06 pm | Permalink

    @Graeme: I think there are some scenarios where it is useful to use Smarty, like the scenario you mentioned. But if the people know PHP I see no benefit of using Smarty over PHP.

    Anyway, it is probably a matter of taste whether you like Smarty or not ;-)

    Btw: This blog moved to

  10. zoran
    Posted March 9, 2008 at 11:22 am | Permalink

    How to translate this code in smarty?

    View Template:

    $posts = $this->requestAction(‘posts/index’);
    foreach($posts as $post):
    echo $post[‘Post’][‘title’];

  11. Posted March 10, 2008 at 4:11 pm | Permalink

    @zoran: I don’t know how you would write this with smarty, as I don’t use it, but maybe you will find what you are looking for in the smarty manual.

    Anyway, I would think twice before using smarty with CakePHP, as the default way is to use plain PHP.

  12. Posted May 2, 2008 at 7:48 pm | Permalink

    Aren’t smarty templates ‘safer’ than direct PHP? Building a user configurable system that allows site maintainers to upload templates without worrying about rouge code with system calls and embedded database commands.

    It would seem that this Smarty or an advanced template system would be a valuable addition to applications with user configurable views.

    Am I missing something?

  13. Posted May 4, 2008 at 9:29 am | Permalink

    @David: Yes, smarty templates are probably safer in the scenario you described.

    PS: This blog has moved to

  14. Posted October 16, 2008 at 1:29 pm | Permalink

    if you use smarty with cakephp you can use the advanced view caching of smarty with cakephp. good for performance.

  15. Posted October 17, 2008 at 8:28 pm | Permalink

    for zoran:

    $posts = $this->requestAction(’posts/index’);
    foreach($posts as $post):
    echo $post[‘Post’][‘title’];

    from controller you assing $posts to smarty ie. $tpl->assign(“posts”, $this->requestAction(‘posts/index’);

    in the .tpl file:
    {section name=iloop loop=$posts}
    or there abouts, it’s been again a month or so last time i used smarty (not my choice not to use!)

    As for Smarty benefits:
    – It compiles the code
    – Caching engine (huge difference in speed)
    – Simpler templating
    – Less code on templates (less clutter, no spaghetti)
    – Complete disseparation of layout from other parts of the system, and i mean COMPLETE. Which means once again: SIMPLER :)

    here is a very simple piece of PHP & Smarty templating:
    assign(“posts”, $forum->LatestPosts(5) ); //optional argument 1, for count of posts

    {include file=header.tpl}
    {include file=forum_header.tpl}
    {section name=iloop loop=posts}


    {include file=forum_footer.tpl}
    {include file=footer.tpl}

    That isn’t even the simplest possible form ;)

  16. Posted October 17, 2008 at 8:30 pm | Permalink

    dangit, commenting allows to interpret HTML, so here’s wrapped:

    from controller you assing $posts to smarty ie. $tpl->assign(“posts”, $this->requestAction(‘posts/index’);

    in the .tpl file:
    {section name=iloop loop=$posts}

    assign(“posts”, $forum->LatestPosts(5) ); //optional argument 1, for count of posts

    {include file=header.tpl}
    {include file=forum_header.tpl}
    {section name=iloop loop=posts}


    {include file=forum_footer.tpl}
    {include file=footer.tpl}

  17. Rani
    Posted May 30, 2009 at 1:30 pm | Permalink


    I am new to cakephp and good (I can say) in smarty, so plz suggest that I should use smarty with cak or not ?

  18. Posted June 1, 2009 at 9:27 am | Permalink

    @Rani: Hm, difficult to say as I don’t have any experience with Smarty… The “cake way” is to use “normal” PHP in the views, though there are some people using Smarty, see the bakery and/or the cakephp google group.

    Hope that helps!

  19. Anas Mughal
    Posted September 15, 2010 at 8:39 am | Permalink

    It requires experience and maturity to understand the benefits of a templating engine like smarty. Let me try to summarize:

    – Templates Cached
    – Presentation totally independent of PHP structures.
    – With presentation totally separated, now you could generate content for different platforms (e.g. Mobile) by just adding new templates.
    – Simplifies maintaining presentation.

    Hope this helps explain the benefits.

  20. Posted March 25, 2013 at 4:27 am | Permalink

    My brother recommended I may like this web site.
    He was totally right. This submit truly made my day.
    You cann’t

    consider simply how much time I had spent for this info! Thanks!

  21. Posted March 25, 2013 at 8:12 am | Permalink

    Great write-up, I’m normal visitor of one’s website, maintain up the

    excellent operate, and It’s going to be a regular visitor for a lengthy time.

  22. Posted April 27, 2013 at 12:39 am | Permalink

    My wife and i felt very joyous Peter managed to carry out

    his inquiry by way of the precious recommendations he got from
    your own blog. It’s not at all simplistic to just possibly be freely giving things that men and

    women have been trying to sell. And

    now we

    do understand we need the writer

    to thank because of that. All of the explanations

    you made, the straightforward blog

    navigation, the friendships you can give support to engender – it’s got most remarkable,
    and it’s facilitating our son in addition to the family imagine

    that the situation is pleasurable, which is certainly

    especially serious. Thanks for the whole lot!

  23. Posted May 28, 2013 at 3:13 am | Permalink

    Terrific post but I was wondering if you could write a litte more on this
    topic? I’d be very grateful if you could elaborate a little bit further. Thank you!

  24. Posted July 17, 2013 at 10:53 am | Permalink

    An early decision you must make is how you want to access to the stock market.
    If you want to be a passive trader and leave the management to an industry professional, mutual funds are good options that provide automatic portfolio diversficiation.
    If you are more of a do-it-yourselfer, then picking and trading your own
    stocks is possible too. Splitting your investment between both is a choice that some
    do as well.

    I really Love investment stock picks

Post a Comment

Required fields are marked *

%d bloggers like this: