Solved

How to create cookies using Smarty Template Engine?

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mysql query for sum() 3 28
Redirect 301 from one address  to another 5 27
php error 27 31
Pull Variable from URL  Use php template 1 12
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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…
The viewer will learn how to count occurrences of each item in an array.
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.

820 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