Ralle wrote:I've grown a lot after last time I visited here.. I am much better at php after you solved my problems.. but here I come with a problem again...
I am trying to learn how to make templates (the smarty ones)
and I can't find out how to do this:
Let's say I have a variable $name, it's an array:
Code: Select all
$name = array('lalala','birdie','cow');
so now I want to put it all into my {Name} so I can say {Name.1} and {Name.2}, so how do I do it???
and inside my template, I want it to echo ALL the {Name.s} like if I pulled data out of the database.
If you are using the actual smarty template engine - (
http://smarty.php.net)
your code might be like this:
Code: Select all
$array = array('apple','banana','orange');
$smarty->assign('fruit',$array);
Your template may be like this:
Code: Select all
<bunch of html code here>
{section name=a loop=$fruit}
Fruit Name{$smarty.section.index} : {$fruit[a]}
{/section}
that's roughly it- I wrote a short tutorial myself on the subject for a few people that asked me...
http://www.crazybri.net/test_html/index.phps -> the code showing the bare basics
http://www.crazybri.net/test_html/templates/index.tpl -> the raw template code
http://www.crazybri.net/test_html/index.php -> the finished product
Along with this, plus smarty documentation online
pretty much anyone with a basic knowledge of php can quickly and easily learn the smarty template system.
Keep in mind, it isnt the best application for every situation - it does use up memory and resources, slowing page load times slightly (apachebench on a raw php page, with embedded html immediately following 10,000 hits/page loads in 31 seconds, the identical page built using Smarty = 10,000 page loads in 83 seconds - a very minimal difference on a low traffic site, but when you have a site getting 1 million +plus+ page views a day (like some I work with) that little bit of difference can scale up to be a crippling load...
So.. smarty is great also for rapid development and deployment, or for having a website with multiple designs/layouts/templates (you can switch between blue and red and black templates by simply changing a variable)
and for smaller websites and CMS's, etc...
but I wouldnt recommend it for a (very) high traffic production webserver unless you've got some heavy duty (and expensive) cpu and clustering on hand
Hopefully this helps a bit
