|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
moet36 Smarty n00b
Joined: 14 Mar 2018 Posts: 1
|
Posted: Wed Mar 14, 2018 2:23 am Post subject: How to concatenate variables? Text? text and variables? |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Mar 14, 2018 3:10 am Post subject: |
|
|
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 |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Wed Mar 14, 2018 6:29 am Post subject: |
|
|
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 |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Wed Mar 14, 2018 6:50 am Post subject: |
|
|
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 |
|
|
|
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
|