[Smarty Engine] Output On Certain Pages Only

WildDisease

Junior Member
Joined
Aug 3, 2011
Messages
109
Reaction score
5
Hello,

I am pretty knowledgeable in PHP even though Im self-taught but Im having trouble with editing my smarty template files..
It is so critical too since it is part of the header template and concerns meta keywords/descriptions for all my pages

Basically Im trying to code my header template file to check what page or URL the viewer is on and output a pair of meta keywords/descriptions hard coded depending on what page it is

In PHP I would use $_SERVER or PHP_SELF variables and if then else to check what page the person is on
1) I cant use the smarty designated {literal} or {php} tags to code it using PHP in the template files because for some reason, they just just skipped over.. although I can paste javascript like adsense right into the template without using {literal} (I cant use JS)
2) If I try to code PHP in the template files without these tags, it blanks the page
3) I tried to use {if {$smarty.server.SCRIPT_NAME}=="index.php"} but nothing actually happens.. I use apache rewrite if that matters
{$smarty.server.SCRIPT_NAME} is the equivalent of PHP SELF
 
Last edited:
Smarty might be the most awful framework ever conceived by man. Like, seriously. I feel for you, brother. Good luck!
 
Hello,

I am pretty knowledgeable in PHP even though Im self-taught but Im having trouble with editing my smarty template files..
It is so critical too since it is part of the header template and concerns meta keywords/descriptions for all my pages

Basically Im trying to code my header template file to check what page or URL the viewer is on and output a pair of meta keywords/descriptions hard coded depending on what page it is

In PHP I would use $_SERVER or PHP_SELF variables and if then else to check what page the person is on
1) I cant use the smarty designated {literal} or {php} tags to code it using PHP in the template files because for some reason, they just just skipped over.. although I can paste javascript like adsense right into the template without using {literal} (I cant use JS)
2) If I try to code PHP in the template files without these tags, it blanks the page
3) I tried to use {if {$smarty.server.SCRIPT_NAME}=="index.php"} but nothing actually happens.. I use apache rewrite if that matters
{$smarty.server.SCRIPT_NAME} is the equivalent of PHP SELF

The way you're "supposed" to do it is that you set all the variables you want for the page via your PHP script and then you just display them and do some basic if then stuff. Smarty is supposed to be about separating any code from the templates so designers can "easily" do them up. Are you not able to change your PHP code?

The other way to do it though is to have your header template as a separate file. Then each of your pages would include that header file.

Code:
{include file="page_header.tpl"}

What I do is set a variable on each page so that any of my included files will know which pages it's for. So each page would basically start off with something like

Code:
{assign var="pagename" value="index"}
{include file="page_header.tpl"}

Then I can do some if then stuff in the header template if required. Of course that doesn't work if the pages are all dynamically generated and you're using one template for all of them. So in that case you really have to set the data from the php script.
 
Thanks OldFatGuy,

I was simply trying to avoid recoding all the pages and instead just code from one but I understand the method you are talking about, the current script does it for titles, so I will just have to tinker with it for metas too

If anyone knows how I can get the if then elses working, let me know as this would be more efficient for now
 
Thanks OldFatGuy,

I was simply trying to avoid recoding all the pages and instead just code from one but I understand the method you are talking about, the current script does it for titles, so I will just have to tinker with it for metas too

If anyone knows how I can get the if then elses working, let me know as this would be more efficient for now

Typically when you can't invoke php in a smarty template, it's because of the libraries security setting.. You could try changing that setting in the smarty class.
 
Back
Top