|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
letimati Smarty n00b
Joined: 10 Dec 2003 Posts: 3 Location: glasgow
|
Posted: Wed Dec 10, 2003 11:07 am Post subject: explode in smarty? |
|
|
Hi there first of all I am new to smarty!!
I have a long string and i want to split it up into an array (like the explode function in php)
is there a simlar function in smarty |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Dec 10, 2003 11:09 am Post subject: |
|
|
You'll be happy to know that you can use php functions as modifiers. Because of the way that the explode function takes its parameters, you have to do it this way, though:
{", "|explode:$var_to_explode}
HOWEVER, why not do that work from you PHP application code?
HTH |
|
Back to top |
|
letimati Smarty n00b
Joined: 10 Dec 2003 Posts: 3 Location: glasgow
|
Posted: Wed Dec 10, 2003 11:24 am Post subject: |
|
|
you mean have a php function like
function format_review($stringreview){
//function that formats the string into an array
return explode(":", $Stringreview);
}
in my php file.
but how do i call it in the tpl file
I get my string through this call in the tpl file
{assign var="nextReview" value=$article->getStory()}
can i just assign an array var and call the function like this
{assign var="arrayReview value=$this->format_array($nextReview)}
?? |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Dec 10, 2003 11:36 am Post subject: |
|
|
Well, not exactly
It looks like you are doing operations in your template that could be considered "application" logic as opposed to display logic. Usually, a good indication of that is trying to model or transpose data in your template.
Without getting into a debate as to what should go where, the syntax I proposed in my first post should work from your template if you do it from an assign (it was only a demonstration of the syntax the way I originally posted it):
{assign var=foo value=", "|explode:$var_to_explode}
Alternatively, I would suggest that you can add a new method or augment your existing one in your $article object to return an array of the proper structure.
However, a common practice would be to do all data modeling assignment work prior to calling your template and use $smarty->assign() to pass the intended structures instead of relying on your template to do so. |
|
Back to top |
|
letimati Smarty n00b
Joined: 10 Dec 2003 Posts: 3 Location: glasgow
|
Posted: Wed Dec 10, 2003 11:51 am Post subject: |
|
|
I understand what you mean about the display logic and the application logic now that i have had a bit of a read at smarty manual.
I was just asked to change the way a (long) string was output to the display and the code that was there used lots and lots of replace which i thought was a bit of a hack, so thought put it in a wee array then output it.
thanks for the help prob write a new method in the $article object (later)
however since your line of code works and I am wanting to get this done and dusted for today i will use that just now.
(and i slaged the last code for beeing a hack!!!)
cheers |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|