Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
concatenate strings
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
rubyaryat
Smarty Rookie


Joined: 24 Apr 2003
Posts: 6

PostPosted: Thu Apr 24, 2003 6:14 pm    Post subject: concatenate strings Reply with quote

I can't find how to concatenate strings with smarty.
Have I missed it?
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Apr 24, 2003 6:23 pm    Post subject: Reply with quote

The smarty template syntax does not support direct concatenation, although there are ways around it.

You can embed your vars in double quotes:

"$foo $bar `$blah[0].foo`"

You can use the cat modifier (new in 2.5.0):

{$myvar|cat:"myval"}

You can also catenate the values in PHP before hand and assign them in the fashion you need them, which would keep the template logic simpler.

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


Joined: 25 Apr 2003
Posts: 7

PostPosted: Sun Apr 27, 2003 10:32 am    Post subject: Re: concatenate strings Reply with quote

rubyaryat wrote:
I can't find how to concatenate strings with smarty.
Have I missed it?


If all you want to do is output it, you can just do {$foo}{$bar}. I realize that doesn't help if you really want to combine two separate strings into a single variable, but that seems like application logic (as opposed display logic) that should probably be done in PHP anyway...
_________________
MJS Webmaster Community
Back to top
View user's profile Send private message
TomWitt
Smarty n00b


Joined: 01 May 2003
Posts: 2

PostPosted: Thu May 01, 2003 6:57 pm    Post subject: Reply with quote

I'm brand spankin' new to Smarty (2 days) but I thought I'd share with you how I solved the concatenation issue.

I had a need to generate some titles for action button images; something like
Code:

$button_text = $object[ii]['type'] . " - #" . $object[ii]['id']  . " - " . $object[ii]['name']; 

then I could have titles like "View $button_text", "Edit $button_text", "Copy $button_text")

I used the {capture} built-in function. I used this concatenate the text;
Code:

{capture name="button_text"}{$edit_object.singular} #{$result[ii].id} - {$result[ii].name}{/capture}


And this to reference the concatenated text;
Code:

{$smarty.capture.button_text}


Here's a full blown example
Code:

{if $page_mode eq 'list'}  {* if we're in list mode *}
  {if NULL != $result}  {* and we have stuff to list *}
    <table border="1" cellspacing="2" cellpadding="3" width="100%">
      <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Description</th>
        <th>Actions</th>
      </tr>
  {section name=ii loop=$result} {* loop over results set *}
  {capture name="button_text"}{$edit_object.singular} #{$result[ii].id} - {$result[ii].name}{/capture}
      <tr class="{cycle values="a,b"}">
        <td id="idcol">{$result[ii].id}</td>
        <td>{$result[ii].name}</td>
        <td>{$result[ii].description}</td>
        <td nowrap id="actioncol">
          <div class="action-items">
          <a href="{$self_url}?a=view&id={$result[ii].id}" class="view"
          title="View {$smarty.capture.button_text}"><span class="alt">[view]</span></a>
          <a href="{$self_url}?a=edit&id={$result[ii].id}" class="edit"
          title="Edit {$smarty.capture.button_text}"><span class="alt">[edit]</span></a>
          <a href="{$self_url}?a=copy&id={$result[ii].id}" class="copy"
          title="Copy {$smarty.capture.button_text}"><span class="alt">[copy]</span></a>
          <a href="{$self_url}?a=del&id={$result[ii].id}" class="delete"
          title="Delete {$smarty.capture.button_text}"><span class="alt">[delete]</span></a>
          <input type="checkbox" name="val[]" value="{$result[ii].id}"
          title="Batch operation on {$smarty.capture.button_text}">
          </div action-items>
        </td>
      </tr>
  {/section}
     </table>
  {else} {* we're in list mode but we have no data *}
         <div class="infomessage">
          <p><strong>No matching records</strong></p>
        <p>There are no {$edit_object.plural} which match the current criteria</p>
         </div>
  {/if}{* end stuff to list *}
{/if}{* end list mode *}   


Again, I'm only 2 days into smarty so this may not be the best way to do this but it's working for me

Hope it helps.

-- Tom
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Thu May 01, 2003 7:13 pm    Post subject: Reply with quote

TomWitt:

I used to do the same thing you suggested. Now I save some syntax using the backtick method that mohrt suggested.

Instead of:
Code:
{capture name="button_text"}{$edit_object.singular} #{$result[ii].id} - {$result[ii].name}{/capture}
{$smarty.capture.button_text}


The slightly less wordy:
Code:
{assign var="button_text" value="`$edit_object.singular` #`$result[ii].id` - `$result[ii].name`"}
{$button_text}
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu May 01, 2003 7:24 pm    Post subject: Reply with quote

Why can't this be done in PHP and assign the correct button text for each button?

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


Joined: 01 May 2003
Posts: 2

PostPosted: Thu May 01, 2003 8:56 pm    Post subject: Reply with quote

mohrt wrote:
Why can't this be done in PHP and assign the correct button text for each button?


It very easily could be, I suppose.

I thought the whole idea about using smarty was to separate presentation details from logic details.

I would think that the PHP logic part would be to get the data from the database in it's most minimal yet complete form then hand it off to smarty to render the output.

If the button text data can be derived from the data handed off from the PHP database retrieval, it becomes a presentation detail.

-- Tom
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu May 01, 2003 9:07 pm    Post subject: Reply with quote

You do keep presentation and application logic separated. I still don't see why this stuff needs to be catentated in the template. Maybe I'm not seeing the whole picture. What does the final output of the buttons end up looking like, and what parts need to be kept configurable?

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


Joined: 25 Apr 2003
Posts: 7

PostPosted: Thu May 01, 2003 11:13 pm    Post subject: Reply with quote

TomWitt wrote:
mohrt wrote:
Why can't this be done in PHP and assign the correct button text for each button?


It very easily could be, I suppose.

I thought the whole idea about using smarty was to separate presentation details from logic details.

I would think that the PHP logic part would be to get the data from the database in it's most minimal yet complete form then hand it off to smarty to render the output.

If the button text data can be derived from the data handed off from the PHP database retrieval, it becomes a presentation detail.

-- Tom


The way I make the distinction is this: If I can't handle it with:
{$foo}{$bar} -- it's not simple presentation logic, and it should probably be done in the PHP code. The fact that you need it in variable form implies that what you're doing is at least one logic level removed from the actual presentation. The fact that it is also much easier to concatenate strings in PHP suggests (from a pragmatic perspective) that it is a better way to do it anyhow.

Generally, the most complicated logic I have in my smarty code is iteration through arrays. IMO, it's a good rule of thumb to keep it that way, especially if you ever need to pass off your smarty templates to designers who are not also php programmers.

- Eric
_________________
MJS Webmaster Community
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Sat May 03, 2003 1:12 am    Post subject: Reply with quote

mohrt wrote:
Why can't this be done in PHP and assign the correct button text for each button?


I should note that I sometimes use Smarty as a caching data templating language. A little bit of application logic means that some of Smarty's funcitonality can be used to simplify (and speedup) application data requests.
Back to top
View user's profile Send private message
Budda
Smarty Regular


Joined: 19 Apr 2003
Posts: 53
Location: Lymm, Cheshire. UK

PostPosted: Mon May 12, 2003 3:41 pm    Post subject: Reply with quote

I'm having trouble using the backtick operator to get my strings together. I think the argument between doing string joins in PHP vs Smarty doesn't apply here. I'll explain what I'm doing.

I'm using a Smarty plugin function. One of its attributes is a string, called 'messages'. This string must contain a static piece of text PLUS an error message defined by me. In order to allow for other languages I've put all the strings into a seperate Smarty config file which is locally loaded at the top of the template.

Code:
{input name="FirstName" size="50" type="text" value="" validation="min_length=2" messages="min_length=`#L_completeFirstName#`"}


My problem is that the 'messages' attribute doesn't get the correct value. The value of #L_completeFirstName# is not returned, and instead I just get the actual variable name displayed as the message!

I need a way of joining the text 'min_length=' and the content of the variable #L_completeFirstName#, then i can pass the joined string to as the value for 'messages' in the above Smarty function.

I don't see how this can, or why it should, be done in PHP rather than Smarty? Confused
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
boots
Administrator


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

PostPosted: Mon May 12, 2003 3:45 pm    Post subject: Reply with quote

Backticks don't work on config vars. You can still do this:

See: Eval in Chapter 8 of the Smarty Manual.

There has also been a feature request:

http://www.phpinsider.com/smarty-forum/viewtopic.php?t=120
Back to top
View user's profile Send private message
Budda
Smarty Regular


Joined: 19 Apr 2003
Posts: 53
Location: Lymm, Cheshire. UK

PostPosted: Mon May 12, 2003 4:17 pm    Post subject: Eval problems Reply with quote

Using eval, I did the following:

{eval var=#L_completeFirstName# assign="firstname_error"}
{$firstname_error}
{input name="FirstName" size="50" type="text" value="" validation="min_length=2" messages="min_length=`$firstname_error`"}

I still get the same problem? Crying or Very sad I can {$firstname_error} to display the output fine on a page (so it has been evaluated okay), but the error message still displays as the variable name rather than the contents of the variable.

I'm using the latest Smarty 2.5.0 release too.
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
messju
Administrator


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

PostPosted: Mon May 12, 2003 7:17 pm    Post subject: Reply with quote

do you want to do sth. like
Code:

...messages="min_length=`$smarty.config.L_completeFirstName`"...


???

(edit: indicated, that this is not the full statement)
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Budda
Smarty Regular


Joined: 19 Apr 2003
Posts: 53
Location: Lymm, Cheshire. UK

PostPosted: Tue May 13, 2003 8:11 am    Post subject: Reply with quote

Using the $smarty variable just gives the error
Quote:
syntax error: $smarty.config is an unknown reference


I checked in the documentation and it appears to be correct. Confused
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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