Yet Another Smarty Thread |
[eluser]xtramix[/eluser]
Integration of CI and Smarty has been discussed many times on this forum, yet I've decided to post here my own solution, in the hopes that someone might find it useful. The only advantage this particular approach offers over other suggested solutions, is its ability to incorporate CI and Smarty code in the same template, rather than forcing you to choose between the two. If your templates contain pure Smarty code, you can load/parse them in a usual fashion, e.g.: Code: $this->smarty_parser->parse("template.tpl", $data); Code: $this->smarty_parser->parse("ci:template.tpl", $data); The steps necessary to set up Smarty under CI in this case are very similar to those suggested by other authors: 1. Rename Smarty "libs" folder to "smarty", and move it to CI's application/libraries directory. 2. Create Smarty_parser.php file in the same directory and paste the following code: Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 3. Create Smarty_parser.php file in the application/config directory: Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 4. In your controller, load Smarty library and parse the template, as such: Code: // uncomment the line below to load the library under the name "smarty", if desired Hope this helps.
[eluser]esra[/eluser]
Nice solution. You might consider placing your entire post on the wiki with a url link back to here.
[eluser]ahmad furqon[/eluser]
yeah, this is cool... thanks a lot for this solution
[eluser]bijon[/eluser]
Hi , This is really nice. I am also think that you should write it to wiki .
[eluser]jargo[/eluser]
hi! could there be a problem with this smarty wrapper and the new CI version 1.6.0? I tried to upgrade my application from v1.5.4 to v1.6.0, but all i get is: Quote:A PHP Error was encountered the welcome.html exists, the specified path seems to be right: Quote:/var/www/localhost/htdocs/homepage/system/application/views I also tried to create a simple new application only with out-of-the-box CI-1.6.0 and the smarty wrapper above. Same problem. :down: any idea about a solution? jargo
[eluser]xtramix[/eluser]
jargo, try changing line 7 in your config/Smarty_parser.php file to: $config['template_dir'] = BASEPATH . 'application/views' ; Please note that you will need to revert the changes if you decide to move your smarty templates to the web app root. - x
[eluser]jargo[/eluser]
hi! thanks for your reply. I found the problem/solution. my smarty_parser.php in application/config was written with capital "s". CI 1.6.0 doesn't like it ![]() Perhaps change this line in your tutorial: Code: 3. Create Smarty_parser.php file in the application/config directory Code: 3. Create smarty_parser.php file in the application/config directory
[eluser]MABUS[/eluser]
hey guys.I'm kinda new to smarty. I've used it with some project, but that one didn't have CI in it. Now, I'd like to ask where shall I put my .tpl files? inside the "views" folder? is it supposed to be named as "xxx.tpl" ??? Another thing, this tutorial shows how to make a call to your smarty library, but doesn't show what $data variable is? . I am used to just passing an array to a view file, and use the values of that as variables on the view file. But this time , since smarty is playing along, how do you suppose doing it using these methods discussed here? does that $data array contain an array ? if so, where do we put the values , or the output from that smarty function call? and last question, is "call_by_pass_reference" really needed to get CI and smarty going together? atleast for this method
[eluser]Avatar[/eluser]
Nice. I like your class. While using it thus far I've found that changing the parse function calls second param to have a default value makes it even nicer. function parse($template, $data='', $return = FALSE) notice I added ="" after data param. Thanks alot. It's just what I was looking for.
[eluser]Avatar[/eluser]
I very much like this class. I use it with modular extensions. see reference below: http://ellislab.com/forums/viewthread/72580 This way you can load module views in smarty templates to get a themed templated site which is also modulated. I would very much like to hear more on this solution. CI is the greatest framework I've ever found. My cousin works with prado, I've looked at it. but I don't see it comparing with ci. maybe if combining the two would be even more effecting. You see this would be even nicer. because prado is php and asp.net combined. Just have a sys and a prado directory for your frameworks and mix and match the templates and multiple systems. also this maybe overdoing it a little much. ![]() |
Welcome Guest, Not a member yet? Register Sign In |