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

How to concatenate variables? Text? text and variables?

 
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
moet36
Smarty n00b


Joined: 14 Mar 2018
Posts: 1

PostPosted: Wed Mar 14, 2018 2:23 am    Post subject: How to concatenate variables? Text? text and variables? Reply with quote

I have a couple variables that I am trying to concatenate with text, to create a date string.

I cannot find an example showing if this is possible.

Trying to create a date string: YYYY-MM-DD

I have the YYYY, MM, DD.

But need to concatenate the "-" between the variables to do a comparison to another date.

I could not even get two text values to concatenate.

Is this possible.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Mar 14, 2018 3:10 am    Post subject: Reply with quote

Smarty is not a programming language. It's a presentation language.
You don't want to "concatenate" anything, as you are ALREADY in a text context. Just write stuff where you need it.
If you want to compare something - compare original values, not strings that represent them.
In case of dates - compare the timestamps or DateTime's directly.
If you are not passing dates as dates to template - fix your PHP code, it's broken.
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Wed Mar 14, 2018 6:29 am    Post subject: Reply with quote

Given that you have this:
$YYYY is "2018"
$MM is "03"
$DD is "18"

Code:

We may want to first load these into an array, adding dashes:
{$foo[] = $YYYY}
{$foo[] = "-"}
{$foo[] = $MM}
{$foo[] = "-"}
{$foo[] = $DD}

Then string them together using PHP's join function:
{$val1 = $foo|join}

Now, {$val1} should equal "2018-03-18" which can be used as an expression in a comparison.
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Wed Mar 14, 2018 6:50 am    Post subject: Reply with quote

As was mentioned, we are in a text context. The problem is how to distinguish what are variables and what is adjacent text.

Not really discussed in the documentation is backticks.

Try this:
Code:

{$val2 = "2018-03-18"}
{if $val2 eq "`$YYYY`-`$MM`-`$DD`"}Equal{else}Not Equal{/if}
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