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

Concatenating replacement values in regex_replace modifier.

 
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 -> Feature Requests
View previous topic :: View next topic  
Author Message
minorgod
Smarty Rookie


Joined: 30 Apr 2003
Posts: 6

PostPosted: Wed Apr 30, 2003 9:00 pm    Post subject: Concatenating replacement values in regex_replace modifier. Reply with quote

I don't know where to post this since there is no "coding" discussion on this board. It seems like smarty is not capable of concatenating a variable for replacement in the regex_replace modifier. How can I do something like this?

{$sometext|regex_replace:"/product_option/i":"product_option_".$smarty.section.product.index }

This doesn't work. I've tried every syntax variation I can think of and nothing works. How can I do something like this in smarty?
_________________
Nowhere does science promise emancipation.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Apr 30, 2003 9:12 pm    Post subject: Reply with quote

I just had this discussion in another thread Wink

I'm not really answering your question, but re-addressing the problem.

If at all possible, assign the variables to the templates as close to displayable form in the first place. Try to avoid mangling vars with regex's and catenation in the template. Let PHP do all the ugly munging first, let the template focus on presentation.

As I said in another thread, Smarty is a double-edge sword. I supplies a lot of power to the template, enough to tempt doing complex stuff there. Let PHP handle it, then assign your data in a fashion that can basically be looped/displayed.

Monte
Back to top
View user's profile Send private message Visit poster's website
minorgod
Smarty Rookie


Joined: 30 Apr 2003
Posts: 6

PostPosted: Wed Apr 30, 2003 9:36 pm    Post subject: Reply with quote

I understand that, but there's no easy way for me to do it in PHP because of the application logic. I've also tried to do it in straight PHP by using {php} tags, but the variables then can't be displayed by the template outside the {php} tags once I've done a preg_replace inside the {php} tags. Is there a way to display something like this:

Code:
{php}
$a="hello world";
{/php}
{$a}

_________________
Nowhere does science promise emancipation.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Apr 30, 2003 11:59 pm    Post subject: Reply with quote

What's with the application logic that doesn't allow it to be done that way?

Monte
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: Thu May 01, 2003 2:20 am    Post subject: Reply with quote

Yeah, Monte's right! You should make the templates only complicated enough to satisfy your formatting needs. Do the rest in PHP to avoid headaches.

Now lets say you can't change your app structure (??) or that the template designers can't get to your code. This, amazingly, works.

Code:
{php}
$a="hello world";
$this->assign('a', $a);
{/php}
{$a}
Back to top
View user's profile Send private message
minorgod
Smarty Rookie


Joined: 30 Apr 2003
Posts: 6

PostPosted: Thu May 01, 2003 5:36 am    Post subject: Reply with quote

Okay, let me rephrase that...I can't rewrite this part of the application in an amount of time that is acceptable to our budget. Thanks for the code. With that said, I rewrote that part of the application and did it in PHP.
_________________
Nowhere does science promise emancipation.
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Thu May 01, 2003 5:51 am    Post subject: Reply with quote

Glad you got it solved. It is a bit hacky, though, eh? Budgets and deadlines are a pleasure.

I'm guessing that the problem has something to do with a nested template which is being called dynamically, so you have to add application logic to know in advance what will need to get pre-calculated. Perhaps you can wrap your logic into a function plugin or simply register a function via the API. You get the same result without the PHP and Smarty API calls intermixed in your template.

Have Fun!

PS. I think this topic could have went in the General forum.
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Thu May 01, 2003 6:05 am    Post subject: Reply with quote

I just looked at your orignal query again:

You can use the new backtick syntax to do this.

Your example only failed because you are using the dot notation, which means you have to surround the var in backticks.

See: http://smarty.php.net/manual/en/language.syntax.quotes.php

Code:
{assign var="sometext" value="i am here"}
{assign var="more" value=", guy"}

{$sometext|regex_replace:"/here/i":"there`$more`"}


produces:
Quote:
i am there, guy


I'm only using {assign} so that the example works--I assume your vars are already assigned.

There is also a new cat modifier, though I think it a bit unnecessary.

See: http://smarty.php.net/manual/en/language.modifier.cat.php
Back to top
View user's profile Send private message
minorgod
Smarty Rookie


Joined: 30 Apr 2003
Posts: 6

PostPosted: Thu May 01, 2003 6:31 am    Post subject: Reply with quote

Awesome! I saw some documentation about using backticks and I tried it, but I was using them all wrong I think. Next time I run into a similar problem, I'll know how to do it. I did finally manage to solve my problem by doing it in my non-template PHP code. It required a rewrite of some sloppy code that needed rewriting anyway. The main reason I was trying to do this in the template was because all the other auto-incremented form names and elements were incremented there. The only thing I couldn't properly increment were a couple of calls to the dynamically incremented function names and it seemed way easier to figure out the increments as they were displayed in the template since this was already being done for the form and element names. So in this situation, I think you could argue for putting the logic in either place. Thanks for the help.
_________________
Nowhere does science promise emancipation.
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 -> Feature Requests 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