Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Pattern Matching in Smarty

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
jcornelius
Smarty Rookie


Joined: 08 Sep 2003
Posts: 8
Location: CC, TX

PostPosted: Mon Sep 08, 2003 4:55 pm    Post subject: Pattern Matching in Smarty Reply with quote

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
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon Sep 08, 2003 5:04 pm    Post subject: Reply with quote

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
View user's profile Send private message
jcornelius
Smarty Rookie


Joined: 08 Sep 2003
Posts: 8
Location: CC, TX

PostPosted: Mon Sep 08, 2003 5:08 pm    Post subject: Reply with quote

Thanks for the tip, but why would you not recommend this ?
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon Sep 08, 2003 5:33 pm    Post subject: Reply with quote

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 Smile
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Sep 08, 2003 5:40 pm    Post subject: Reply with quote

boots wrote:
Smarty is flexible enough to let you do it as you see fit Smile


or (to modify a quote from the camel-book) "smarty gives you enough rope to hang yourself"

cheers
messju
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon Sep 08, 2003 5:49 pm    Post subject: Reply with quote

messju wrote:
boots wrote:
Smarty is flexible enough to let you do it as you see fit Smile

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. Wink

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? Laughing okay, okay, I kid.

cheers
Back to top
View user's profile Send private message
jcornelius
Smarty Rookie


Joined: 08 Sep 2003
Posts: 8
Location: CC, TX

PostPosted: Mon Sep 08, 2003 6:18 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
mocean
Smarty Rookie


Joined: 21 May 2003
Posts: 30
Location: The Netherlands

PostPosted: Mon Sep 08, 2003 7:55 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website MSN Messenger
jcornelius
Smarty Rookie


Joined: 08 Sep 2003
Posts: 8
Location: CC, TX

PostPosted: Wed Sep 10, 2003 5:51 pm    Post subject: Reply with quote

Yea, but I'm trying to keep certain people out of the PHP code Smile
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Sep 10, 2003 6:18 pm    Post subject: Reply with quote

jcornelius wrote:
Yea, but I'm trying to keep certain people out of the PHP code Smile

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 Wink
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP