View previous topic :: View next topic |
Author |
Message |
rubyaryat Smarty Rookie
Joined: 24 Apr 2003 Posts: 6
|
Posted: Thu Apr 24, 2003 6:14 pm Post subject: concatenate strings |
|
|
I can't find how to concatenate strings with smarty.
Have I missed it? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Apr 24, 2003 6:23 pm Post subject: |
|
|
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 |
|
dilvie Smarty Rookie
Joined: 25 Apr 2003 Posts: 7
|
Posted: Sun Apr 27, 2003 10:32 am Post subject: Re: concatenate strings |
|
|
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 |
|
TomWitt Smarty n00b
Joined: 01 May 2003 Posts: 2
|
Posted: Thu May 01, 2003 6:57 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu May 01, 2003 7:13 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu May 01, 2003 7:24 pm Post subject: |
|
|
Why can't this be done in PHP and assign the correct button text for each button?
Monte |
|
Back to top |
|
TomWitt Smarty n00b
Joined: 01 May 2003 Posts: 2
|
Posted: Thu May 01, 2003 8:56 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu May 01, 2003 9:07 pm Post subject: |
|
|
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 |
|
dilvie Smarty Rookie
Joined: 25 Apr 2003 Posts: 7
|
Posted: Thu May 01, 2003 11:13 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat May 03, 2003 1:12 am Post subject: |
|
|
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 |
|
Budda Smarty Regular
Joined: 19 Apr 2003 Posts: 53 Location: Lymm, Cheshire. UK
|
Posted: Mon May 12, 2003 3:41 pm Post subject: |
|
|
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? |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
|
Back to top |
|
Budda Smarty Regular
Joined: 19 Apr 2003 Posts: 53 Location: Lymm, Cheshire. UK
|
Posted: Mon May 12, 2003 4:17 pm Post subject: Eval problems |
|
|
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? 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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Mon May 12, 2003 7:17 pm Post subject: |
|
|
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 |
|
Budda Smarty Regular
Joined: 19 Apr 2003 Posts: 53 Location: Lymm, Cheshire. UK
|
Posted: Tue May 13, 2003 8:11 am Post subject: |
|
|
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. |
|
Back to top |
|
|