Page 1 of 1

Using Smarty tags in User Defined Tags

Posted: Wed May 16, 2007 6:45 pm
by okparrothead
Hello SMS Gurus,

Have mercy on a Noob.

I'm adding some php code in a user defined tag, but I want to use a smarty page variable as a control.

This doesn't work:

switch ({title}){
case: ......

Any ideas?

Thanks for all your help!

Re: Using Smarty tags in User Defined Tags

Posted: Wed May 16, 2007 8:23 pm
by alby
okparrothead wrote: Hello SMS Gurus,

Have mercy on a Noob.

I'm adding some php code in a user defined tag, but I want to use a smarty page variable as a control.

This doesn't work:

switch ({title}){
case: ......

Any ideas?

Thanks for all your help!
Try with:

Code: Select all

global $gCms;
$smarty =& $gCms->GetSmarty();

switch ($smarty->get_template_vars('title')){
 case: ......
Alby

Re: Using Smarty tags in User Defined Tags

Posted: Wed May 16, 2007 10:32 pm
by okparrothead
Thanks Alby,

No go.

I put it all in the user defined tag, but same result.

Should I be putting the

global $gCms;
$smarty =& $gCms->GetSmarty();

part in the template?

Thanks for all your help

Re: Using Smarty tags in User Defined Tags

Posted: Thu May 17, 2007 11:59 am
by alby
okparrothead wrote: Thanks Alby,

No go.
On, my mistake.
This work:
UDT -> test

Code: Select all

global $gCms;
$smarty =& $gCms->GetSmarty();

echo '<br />page = '.$smarty->get_template_vars('page');
echo '<br />page_id = '.$smarty->get_template_vars('page_id');
echo '<br />page_name = '.$smarty->get_template_vars('page_name');
echo '<br />page_alias= '.$smarty->get_template_vars('page_alias');
In template:

Code: Select all

{test}
Alby

Re: Using Smarty tags in User Defined Tags

Posted: Thu May 17, 2007 2:57 pm
by okparrothead
Thanks Alby,
So close!! The code you gave me works great!

I could echo the value of title, but I can't use it in the UDT code.

Let me explain further.

I have a series of product pages with different colors for each product. I've written a flat file of all the colors and a script that will get the correct colors depending on product.

I can make my script work fine if I plug in a variable for any one product. I could copy the same script for each product, with different product variables, and rename them as different UDTs. But -- I want to use a PHP switch so the same UDT would work for any product. I decided to use the page {title} (also the product title) as the switch variable.

How can I use the {title} of the page as a PHP variable in my UDT?

I read the user handbook and came up with these attempts:
Unless I had unknown spelling errors,

switch({title}) didn't work.

I tried {colors product={title}} in the template (to pass {title} as a variable named product for the UDT)  and  switch($params['product']) in the UDT named colors, but that didn't work.

I also tried {title assign=thiscolor} {colors product="$thiscolor"} in the template (to assign the value of {title} to $thiscolor as a variable for the UDT,) and swith($params['product']) in the UDT named colors, and that didn't work.

Do you know where I go from here?

Any help on this would be great.

Re: Using Smarty tags in User Defined Tags

Posted: Thu May 17, 2007 3:35 pm
by alby
okparrothead wrote: Thanks Alby,
So close!! The code you gave me works great!

I could echo the value of title, but I can't use it in the UDT code.

Let me explain further.

I have a series of product pages with different colors for each product. I've written a flat file of all the colors and a script that will get the correct colors depending on product.

I can make my script work fine if I plug in a variable for any one product. I could copy the same script for each product, with different product variables, and rename them as different UDTs. But -- I want to use a PHP switch so the same UDT would work for any product. I decided to use the page {title} (also the product title) as the switch variable.

How can I use the {title} of the page as a PHP variable in my UDT?
Try with (but remember title isn't unique, alias or id yes):

Code: Select all

global $gCms;
$smarty =& $gCms->GetSmarty();

echo '<br />page = '.$smarty->get_template_vars('page');
echo '<br />page_id = '.$smarty->get_template_vars('page_id');
echo '<br />page_name = '.$smarty->get_template_vars('page_name');
echo '<br />page_alias= '.$smarty->get_template_vars('page_alias');

$pageinfo = &$gCms->variables['pageinfo'];
$title = $pageinfo->content_title;
echo '<br />title= '.$title;
okparrothead wrote: I tried {colors product={title}} in the template (to pass {title} as a variable named product for the UDT)  and  switch($params['product']) in the UDT named colors, but that didn't work.

I also tried {title assign=thiscolor} {colors product="$thiscolor"} in the template (to assign the value of {title} to $thiscolor as a variable for the UDT,) and swith($params['product']) in the UDT named colors, and that didn't work.
In template try with:

Code: Select all

{capture name=titlecapture}{title}{/capture}

{colors product=$titlecapture}
Alby

Re: Using Smarty tags in User Defined Tags

Posted: Thu May 17, 2007 4:34 pm
by okparrothead
Thanks Alby,

Mil Grazie!

You are the best!

You're right of course (Doh!) the title isn't necessarily unique, so I should  change that. The alias is better.

Anyway, here's what worked:

global $gCms;
$smarty =& $gCms->GetSmarty();
$pageinfo = &$gCms->variables['pageinfo'];

switch($pageinfo->content_title)

in the UDT.

If I was there, I'd buy you some beer!! Thank you so much.

May there be a new Ferrari in your near future!

Peace,

OKParrothead

Re: Using Smarty tags in User Defined Tags

Posted: Thu May 17, 2007 7:21 pm
by alby
okparrothead wrote: Thanks Alby,

Mil Grazie!

You are the best!

You're right of course (Doh!) the title isn't necessarily unique, so I should  change that. The alias is better.

Anyway, here's what worked:

global $gCms;
$smarty =& $gCms->GetSmarty();
$pageinfo = &$gCms->variables['pageinfo'];

switch($pageinfo->content_title)

in the UDT.

If I was there, I'd buy you some beer!! Thank you so much.

May there be a new Ferrari in your near future!
I'm glad to see that I've helped some people  ;D

Alby