Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to create cookies using Smarty Template Engine?

Posted on 2009-04-03
5
Medium Priority
?
3,037 Views
Last Modified: 2013-11-10
Hi,

I have been working with PHP for a decent amount of time, but I now have to work with Smarty at my office. What I am trying to do is create a cookie when someone first comes to the site and then use that variable to display a phone number throughout the site for their entire visit.

I want to use a cookie so that if they close the window and come back later, I can still display the correct phone number targeted for them.

Unfortunately, I don't know anything about Smarty and can't figure out how to create a cookie or access it either. Should I create the cookie using PHP? Can Smarty access cookie data? Can someone help me?

Thanks!


0
Comment
Question by:aseem1234
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:prokvk
ID: 24062547
Smarty is a template engine which allows to separate application logic from the output data. Due to this fact you can do really just a few basic operations with Smarty, for anything more complex Smarty allows you to use {php}{/php} tags to implement your logic. So, to your question - COOKIEs via Smarty - definately not. Use pure PHP ;)
0
 

Author Comment

by:aseem1234
ID: 24062569
AHHHH So I can use PHP in my Smarty templates by using these {php}{/php} tags? Can Smarty tags be nested inside the PHP tags? And also vice versa?

Thanks!
0
 

Author Comment

by:aseem1234
ID: 24063687
Also, is there any way to set a cookie in PHP and retrieve it on the same page if the cookie never existed before? I am trying to set a new cookie and have it displayed when a page loads, but it shows up as blank. I have to refresh the page and then the value shows up.

Thanks
0
 
LVL 5

Accepted Solution

by:
prokvk earned 1200 total points
ID: 24064981
I'm going to react on your first response. No, you cant use each other tags as you described .. {php}{/php} closure tells smarty to leave this block intact and treat it as a pure php ...

Your second response .. read up on COOKIE manuals, you'll find tons of materials about them over the internet, just google .. ;)
Using COOKIEs is as easy as setting them via setcookie PHP function and later on you can work with them via superglobals $_COOKIE variable ...
0
 
LVL 14

Assisted Solution

by:shobinsun
shobinsun earned 800 total points
ID: 24066190
Hello,

You can use sessions with smarty.

{* display the php session variable "id" ($_SESSION['id']) *}
{$smarty.session.id}

For more about use the following link:

http://cirofeitosa.com/post/smarty-php-always-together

Regards.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question