Solved

How to create cookies using Smarty Template Engine?

Posted on 2009-04-03
5
2,706 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 300 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 200 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now