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 avoid using {php}...{/php} tags

 
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 -> Tips and Tricks
View previous topic :: View next topic  
Author Message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Tue Nov 23, 2010 11:49 am    Post subject: How to avoid using {php}...{/php} tags Reply with quote

In some old applications that were built on Smarty, we can see some codes like:
Code:
{php}echo date('l, jS F Y'){/php}

Smarty 2.x.x did support this well. But this is how, you can upgrade your same script for new releases of smarty:
Code:
{'l, jS F Y'|date}


The {php}...{/php} tags are no more enabled by default.
The second one produces the exactly same result and works in older versions of Smarty too.
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Nov 23, 2010 2:38 pm    Post subject: Reply with quote

FYI, date_format also supports the date() modifiers as of Smarty 3.

{$smarty.now|date_format:"%Y-%m-%d"} <-- strftime()
{$smarty.now|date_format:"l, jS F Y"} <-- date()

Both modifier formats are accepted.

To throw in another option, Smarty 3 also accepts PHP functions directly:

{date('l, jS F Y')}

However, the plugin approach is recommended.
Back to top
View user's profile Send private message Visit poster's website
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Tue Nov 23, 2010 6:42 pm    Post subject: short and beautiful Reply with quote

Quote:
{date('l, jS F Y')}

Looks, this is a native PHP replacement; short and beautiful.

{'l, jS F Y'|date} is even shorter and less confusing due to single pairs of braces only.

But these both produce a common compiled output.
Is it due to `date` being a native php callable function?

I would definitely go with it.
Back to top
View user's profile Send private message Visit poster's website
TakingSides
Smarty Rookie


Joined: 11 Dec 2010
Posts: 30

PostPosted: Fri Jan 28, 2011 9:04 am    Post subject: Reply with quote

Also, the date_format modifier supports both string dates, and timestamps. So long as the string date is recognised by the strtotime()
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 -> Tips and Tricks 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