View previous topic :: View next topic |
Author |
Message |
zewtastic Smarty Rookie
Joined: 26 Dec 2016 Posts: 10
|
Posted: Mon Dec 26, 2016 10:38 pm 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 |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Mon Dec 26, 2016 11:10 pm Post subject: |
|
|
You gave the YYYY, the DD, and the MM as template variables?
And you want to see, for example: 2016-12-26
Try:
{$YYYY}-{$MM}-{$DD}
Practically speaking, there is no concatenation. Just make what you want to see, then directly replace the sample data with the template variable syntax.
But maybe you want to do this:
{if $DATE1 eq $DATE2}
Usually, I see no need to nest parentheses, but might be necessary to segregate the variable name from its surroundings, or segregate the static values from the variable names (to prevent math functions). So:
{if $DATE1 eq $YYYY{'-'}$MM{'-'}$DD}
Last edited by bsmither on Mon Dec 26, 2016 11:18 pm; edited 1 time in total |
|
Back to top |
|
zewtastic Smarty Rookie
Joined: 26 Dec 2016 Posts: 10
|
Posted: Mon Dec 26, 2016 11:18 pm Post subject: more |
|
|
I am actually trying to assign it to a new var.
{$todaysdate = {$YYYY}-{$MM}-{$DD} }
{$todaysdate}
This does not get me a result that looks like:
2016-12-26
If I echo {$cYear}-{$cMonth}-{$days_date}, it does echo 2016-12-26.
I have tried many variations with no success. Do I need to use {$YYYY}-{$MM}-{$DD} in my other functions as is? |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Mon Dec 26, 2016 11:25 pm Post subject: |
|
|
I posted more to my previous reply.
Let's look at static values (see the docs on Variables).
I think we can get Smarty to consider the dashes as strings as opposed to math operators.
{assign "todaysdate" {$YYYY}{'-'}{$MM}{'-'}{$DD}}
I'm also thinking any whitespace next to the inside of any brace is illegal. |
|
Back to top |
|
zewtastic Smarty Rookie
Joined: 26 Dec 2016 Posts: 10
|
Posted: Mon Dec 26, 2016 11:37 pm Post subject: thank you |
|
|
Thank you for the ideas, I will do some more testing and see if I can solve the problem. |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Mon Dec 26, 2016 11:38 pm Post subject: |
|
|
You mentioned the word 'echo'.
Do you mean to have used this PHP command literally, or as a general concept of trying things in a template resource, then having Smarty display it to the browser? |
|
Back to top |
|
zewtastic Smarty Rookie
Joined: 26 Dec 2016 Posts: 10
|
Posted: Tue Dec 27, 2016 12:40 am Post subject: |
|
|
This worked for my problem.
Thanks again for the help.
{assign var="todaysdate" value="{$cYear}{'-'}{$cMonth}{'-'}{$days_date}"} |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Dec 27, 2016 12:46 am Post subject: |
|
|
Why not just assign that date from your code? |
|
Back to top |
|
zewtastic Smarty Rookie
Joined: 26 Dec 2016 Posts: 10
|
Posted: Tue Dec 27, 2016 1:14 am Post subject: answer |
|
|
The overall code is a bit more complex, looping thru calendar dates and evaluating them for matches to different situations.
I am converting the original php based code to Smarty, of which I am a newbie with. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Dec 27, 2016 3:06 am Post subject: |
|
|
Here's a very simple rule:
If you rely on your template code for your application to work right, you're doing it wrong.
Template code should be strictly visualization in purpose.
If you want to make a bunch of calculations and reformatting for presentation, that's fine as long as it is, indeed, contained to presentational purposes.
Pass as raw a data as possible, and use modifier functions to process it in a template. |
|
Back to top |
|
|