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
zewtastic
Smarty Rookie


Joined: 26 Dec 2016
Posts: 10

PostPosted: Mon Dec 26, 2016 10:38 pm    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
bsmither
Smarty Elite


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

PostPosted: Mon Dec 26, 2016 11:10 pm    Post subject: Reply with quote

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
View user's profile Send private message
zewtastic
Smarty Rookie


Joined: 26 Dec 2016
Posts: 10

PostPosted: Mon Dec 26, 2016 11:18 pm    Post subject: more Reply with quote

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
View user's profile Send private message
bsmither
Smarty Elite


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

PostPosted: Mon Dec 26, 2016 11:25 pm    Post subject: Reply with quote

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
View user's profile Send private message
zewtastic
Smarty Rookie


Joined: 26 Dec 2016
Posts: 10

PostPosted: Mon Dec 26, 2016 11:37 pm    Post subject: thank you Reply with quote

Thank you for the ideas, I will do some more testing and see if I can solve the problem.
Back to top
View user's profile Send private message
bsmither
Smarty Elite


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

PostPosted: Mon Dec 26, 2016 11:38 pm    Post subject: Reply with quote

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
View user's profile Send private message
zewtastic
Smarty Rookie


Joined: 26 Dec 2016
Posts: 10

PostPosted: Tue Dec 27, 2016 12:40 am    Post subject: Reply with quote

This worked for my problem.

Thanks again for the help. Smile

{assign var="todaysdate" value="{$cYear}{'-'}{$cMonth}{'-'}{$days_date}"}
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Dec 27, 2016 12:46 am    Post subject: Reply with quote

Why not just assign that date from your code?
Back to top
View user's profile Send private message
zewtastic
Smarty Rookie


Joined: 26 Dec 2016
Posts: 10

PostPosted: Tue Dec 27, 2016 1:14 am    Post subject: answer Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Dec 27, 2016 3:06 am    Post subject: Reply with quote

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
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