View previous topic :: View next topic |
Author |
Message |
jcornelius Smarty Rookie
Joined: 08 Sep 2003 Posts: 8 Location: CC, TX
|
Posted: Mon Sep 08, 2003 4:55 pm Post subject: Pattern Matching in Smarty |
|
|
Is it possible to do any basic regex style pattern matching inside smarty templates? i'd like to do something like this:
Code: |
{if $var ~= "findme"}
execute this
{else}
execute that
{/if}
|
Any ideas ?
Thanks a lot,
- J. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Sep 08, 2003 5:04 pm Post subject: |
|
|
I don't think that this would be a recommended use.
Never-the-less, Smarty's {if} construct allows the use of standard PHP functions as long as security mode is not enabled. If it is enabled, then allowed functions must be explicitly declared. So you can technically do something like:
{if preg_match(....)}
{else}
{/if} |
|
Back to top |
|
jcornelius Smarty Rookie
Joined: 08 Sep 2003 Posts: 8 Location: CC, TX
|
Posted: Mon Sep 08, 2003 5:08 pm Post subject: |
|
|
Thanks for the tip, but why would you not recommend this ? |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Sep 08, 2003 5:33 pm Post subject: |
|
|
Well, pattern processing seems like the sort of thing that should be in your application opposed to the display logic. You may have a situation where it is warranted, but off the top of my head, I can't think of an example where I would need pattern matching in a template to satisfy display logic.
I try to limit my templates to the most brain dead things: variable replacement, collection looping, sub-template inclusion. This may also imply validity checks within the template, but the way I have it setup, I do as much as possible to only pass processed data to the templates which then need only implement the display of those data structures.
That's me. Smarty is flexible enough to let you do it as you see fit |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Mon Sep 08, 2003 5:40 pm Post subject: |
|
|
boots wrote: | Smarty is flexible enough to let you do it as you see fit |
or (to modify a quote from the camel-book) "smarty gives you enough rope to hang yourself"
cheers
messju |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Sep 08, 2003 5:49 pm Post subject: |
|
|
messju wrote: | boots wrote: | Smarty is flexible enough to let you do it as you see fit |
or (to modify a quote from the camel-book) "smarty gives you enough rope to hang yourself"
|
I was actually going to say it that way (which I think I have elsewhere on the forum) but I suspect some folks have different tolerances in regards to overall neck tightness than I do.
I always have this image in my mind of an old farce film I once saw on tv as a kid in which one character was talking emphatically on the phone to a cohort over a plan that was going awry. He was very concerned because as he explained to his phone partner: "my neck is stretched out ten miles on this one!"
Speaking of camels and stretching one's neck further than it should be: how about a poor man's perl mode for Smarty? okay, okay, I kid.
cheers |
|
Back to top |
|
jcornelius Smarty Rookie
Joined: 08 Sep 2003 Posts: 8 Location: CC, TX
|
Posted: Mon Sep 08, 2003 6:18 pm Post subject: |
|
|
I agree for the most part. This particular situation calls for different ordering of images based on a value in the $_SESSION. It's less code to do a simple {if} statement then to do about anything else (that I could think of) |
|
Back to top |
|
mocean Smarty Rookie
Joined: 21 May 2003 Posts: 30 Location: The Netherlands
|
Posted: Mon Sep 08, 2003 7:55 pm Post subject: |
|
|
jcornelius wrote: | I agree for the most part. This particular situation calls for different ordering of images based on a value in the $_SESSION. It's less code to do a simple {if} statement then to do about anything else (that I could think of) |
One could do the preg_match check in PHP before template inclusion and set a variable (true or false) to be used in the {if} construct. _________________ Visit My Smarty Enabled Website (Dutch Only) at www.habiforum.nl
and read how it works. |
|
Back to top |
|
jcornelius Smarty Rookie
Joined: 08 Sep 2003 Posts: 8 Location: CC, TX
|
Posted: Wed Sep 10, 2003 5:51 pm Post subject: |
|
|
Yea, but I'm trying to keep certain people out of the PHP code |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Sep 10, 2003 6:18 pm Post subject: |
|
|
jcornelius wrote: | Yea, but I'm trying to keep certain people out of the PHP code |
That's the idea, of course. Yet it sounds like it is a case of out of the frying pan (PHP) and into the fire (regex). I think Mocean's point is that you want to abstract out all of those decisions from the display. It boils down to developing a set of interfaces and in this case, one of the interface parameters would be the desired sort scheme.
The reason to think like this is that one day down the road when it is necessary to re-evaluate how ordering schemes are decided, it is obvious where to look: your application code. I do agree though that there is a fine-line here since some might say that ordering is a display logic. I'm in the camp that suggests that deciding which ordering scheme to use is an application related logic.
You're right though--this doesn't necessarily lead to the least amount of overall code, but it does lead to a clear delineation of responsibility and since it forces you to use an interface methodology, it can help you improve your overall code in terms of testing, reusability, compliance and a host of other things that may or may not be relevant to you |
|
Back to top |
|
|